#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
class PalindromePath {
public :
int shortestLength( int , vector < int > , vector < int > , string) ;
} ;
char am[ 25 ] [ 25 ] ;
bool visit[ 25 ] [ 25 ] ;
int memo[ 25 ] [ 25 ] ;
int dp( int a, int b)
{
visit[ a] [ b] = true ;
if ( am[ a] [ b] ! = 'N' )
return 1 ;
if ( a == b)
return 0 ;
int ans = - 1 ;
for ( int i = 0 ; i < 25 ; i++ )
{
for ( int j = 0 ; j < 25 ; j++ )
{
if ( ! visit[ i] [ j] && am[ a] [ i] ! = 'N' && am[ b] [ j] ! = 'N' && am[ a] [ i] == am[ b] [ j] )
{
int len = dp( i,j) ;
if ( len ! = - 1 )
{
if ( ans == - 1 )
ans = len+ 2 ;
else
ans = min( ans, len+ 2 ) ;
}
}
}
}
return ans;
}
int PalindromePath:: shortestLength ( int n, vector < int > a, vector < int > b, string c) {
for ( int i = 0 ; i < 25 ; i++ )
{
for ( int j = 0 ; j < 25 ; j++ )
{
am[ i] [ j] = 'N' ;
visit[ i] [ j] = false ;
}
}
for ( int i = 0 ; i < a.size ( ) ; i++ )
{
am[ a[ i] ] [ b[ i] ] = c[ i] ;
am[ b[ i] ] [ a[ i] ] = c[ i] ;
}
return dp( 0 ,1 ) ;
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGxpc3Q+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxkZXF1ZT4KI2luY2x1ZGUgPHN0YWNrPgojaW5jbHVkZSA8Yml0c2V0PgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KI2luY2x1ZGUgPG51bWVyaWM+CiNpbmNsdWRlIDx1dG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