#include <bits/stdc++.h>
#define fi first
#define si second
#define For(i,a,b) for (int i = (a), _b =(b); i <= _b; ++i)
#define all(v) v.begin(), v.end()
#define Unique(v) v.erase(unique(all(v)), v.end())
#define MASK(i) (1LL << (i))
#define bit(i,n) (((n)>>(i)) & 1)
#define Vii vector<pair<int,int>>
#define setpr(x) cout<<setprecision(x)<<fixed
#define Prior priority_queue<pair<int,int>, Vii, greater<pair<int,int>>>
using namespace std;
const int Mod = 1E9 + 7;
const long long INF = 4E18;
const int N = 100 + 1;
int d[6][2] = {{1, 2}, {2, 3}, {6, 7}, {9, 10}, {8, 9}, {4, 5}};
queue<pair<array<int, 11>, int>> q;
array<int, 11> arr;
map<array<int, 11>, bool> vis;
signed main() {
For(i, 1, 10) cin >> arr[i];
q.push({arr, 0});
while (!q.empty()) {
pair<array<int, 11>, int> node = q.front();
q.pop();
bool ok = true;
For(i, 1, 3) if (node.fi[i] != i) ok = false;
if (node.fi[5] != 0 || node.fi[6] != 0 || node.fi[4] != 8 || node.fi[7] != 4) ok = false;
if (node.fi[8] != 7 || node.fi[9] != 6 || node.fi[10] != 5) ok = false;
if (ok) {
cout << node.si << '\n';
return 0;
}
if (vis[node.fi]) continue;
vis[node.fi] = true;
for (int i = 0; i <= 1; i++) {
array<int, 11> a = node.fi;
int tmp = a[d[5][i]];
for (int j = 0; j < 6; j++) {
int k = d[j][i];
swap(a[k], tmp);
}
if (!vis[a]) q.push({a, node.si + 1});
}
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiAKI2RlZmluZSBmaSAgICAgICAgICAgZmlyc3QgCiNkZWZpbmUgc2kgICAgICAgICAgIHNlY29uZCAKI2RlZmluZSBGb3IoaSxhLGIpICAgZm9yIChpbnQgaSA9IChhKSwgX2IgPShiKTsgaSA8PSBfYjsgKytpKQojZGVmaW5lIGFsbCh2KSAgICAgICB2LmJlZ2luKCksIHYuZW5kKCkKI2RlZmluZSBVbmlxdWUodikgICAgdi5lcmFzZSh1bmlxdWUoY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PSA4IHx8IG5vZGUuZmlbN10gIT0gNCkgb2sgPSBmYWxzZTsKICAgICAgICBpZiAobm9kZS5maVs4XSAhPSA3IHx8IG5vZGUuZmlbOV0gIT0gNiB8fCBub2RlLmZpWzEwXSAhPSA1KSBvayA9IGZhbHNlOwogICAgICAgIGlmIChvaykgewogICAgICAgICAgICBjb3V0IDw8IG5vZGUuc2kgPDwgJ1xuJzsKICAgICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIGlmICh2aXNbbm9kZS5maV0pIGNvbnRpbnVlOwogICAgICAgIAogICAgICAgIHZpc1tub2RlLmZpXSA9IHRydWU7CiAgICAgICAgCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gMTsgaSsrKSB7ICAgCiAgICAgICAgICAgIGFycmF5PGludCwgMTE+IGEgPSBub2RlLmZpOyAKICAgICAgICAgICAgaW50IHRtcCA9IGFbZFs1XVtpXV07IAogICAgICAgICAgICAKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCA2OyBqKyspIHsgICAKICAgICAgICAgICAgICAgIGludCBrID0gZFtqXVtpXTsKICAgICAgICAgICAgICAgIHN3YXAoYVtrXSwgdG1wKTsgCiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKCF2aXNbYV0pIHEucHVzaCh7YSwgbm9kZS5zaSArIDF9KTsKICAgICAgICB9CiAgICB9Cn0K