#include<iostream>
#include<map>
#include<string>
#include<queue>
#include<cstring>
using namespace std;
const int N = 400000;
#define state pair<int, int>
#define hash first
#define pos second
/*
1 3 5 1 2 3
2 4 6 4 5 6
7 8 . 7 8 .
4 6 7 5 6 7
5 8 1 3 8 1
2 3 . 4 2 .
*/
string t[9] = {
".12345678", "1.2345678", "12.345678",
"123.45678", "1234.5678", "12345.678",
"123456.78", "1234567.8", "12345678."
};
/*
. 1 2
3 4 5
6 7 8
1 . 2
3 4 5
6 7 8
*/
int factor[10];
int step[9][N];
int st[9][N];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int cantor(const string &s){
int res = 0;
for(int i = 0; i < s.size(); i ++){
int cnt = 0;
for(int j = i + 1; j < s.size(); j ++){
if(s[j] < s[i]) cnt ++;
}
res += cnt * factor[8 - i];
}
return res;
}
string decantor(int hash){
string t = ".12345678";
int len = 9;
string s = "";
for(int i = 8; i >= 0; i --){
int l = hash / factor[i];
hash %= factor[i];
s += t[l];
for(int j = l + 1; j < len; j ++) t[j - 1] = t[j];
len --;
}
return s;
}
void bfs(const state &s, int idx){
queue<state> q;
q.push(s);
st[idx][s.hash] = 1;
while(q.size()){
state h = q.front();
q.pop();
string t = decantor(h.hash);
int x = h.pos / 3, y = h.pos % 3;
for(int i = 0; i < 4; i ++){
int a = x + dx[i], b = y + dy[i];
if(a < 0 || b < 0 || a >= 3 || b >= 3) continue;
int new_pos = a * 3 + b;
swap(t[h.pos], t[new_pos]);
int k = cantor(t);
swap(t[h.pos], t[new_pos]);
if(st[idx][k]) continue;
q.push({k, new_pos});
st[idx][k] = 1;
step[idx][k] = step[idx][h.hash] + 1;
}
}
}
void init(){
factor[0] = 1;
for(int i = 1; i < 10; i ++)
factor[i] = factor[i - 1] * i;
for(int i = 0; i < 9; i ++){
int k = cantor(t[i]);
bfs({k, i}, i);
}
}
int main(){
init();
string start, aim;
cin >> start >> aim;
map<char, char> m;
int pos = 0;
for(int i = 0; i < start.size(); i ++)
if(start[i] == '.'){
pos = i;
break;
}
for(int i = 0; i < start.size(); i ++) m[start[i]] = t[pos][i];
string new_aim;
for(int i = 0; i < aim.size(); i ++) new_aim += m[aim[i]];
cout << step[pos][cantor(new_aim)];
return 0;
}