#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>

using namespace std;

int find_locate(int node,int num) {
    int ans=0;
    while(ans < 8) {
        int a, b, c;
        int keta_a=1, keta_b=1;
        for (int j=0; j<ans; j++) {
            keta_a = keta_a*10;
            keta_b = keta_b*10;
        }
        keta_a = keta_a*10;
        a = node % keta_a;
        b = node % keta_b;
        c = a-b;
        c = c/keta_b;

        if (c==num) {
            break;
        }
        ans++;
    }
    return ans;
}

int insert(int node, int keta, int num) {
    int a,b,c,d;
    int keta_a=1, keta_b=1;
    int ans;
    for (int i=0; i<keta; i++) {
        keta_a = keta_a*10;
        keta_b = keta_b*10;
    }
    keta_a = keta_a*10;
    a = node % keta_a;
    b = node % keta_b;
    c = a-b;
    d = num * keta_b;
    ans = node - c;
    ans = ans + d;
    return ans;
}

int look(int node, int num) {
    int a, b, c;
    int keta_a=1, keta_b=1;
    for (int j=0; j<num; j++) {
        keta_a = keta_a*10;
        keta_b = keta_b*10;
    }
    keta_a = keta_a*10;
    a = node % keta_a;
    b = node % keta_b;
    c = a-b;
    c = c/keta_b;

    return c;
}

int proceed_adj(int node, int dirc) {
    int loc_zero = find_locate(node, 0);
    int swap;
    if (dirc == 0) { 
        // go down
        if (loc_zero <= 3) {
            return 0;
        }
        swap = look(node, loc_zero-4);
        node = insert(node, loc_zero-4, 0);
        node = insert(node, loc_zero, swap);

        return node;
    }
    if (dirc == 1) {
        // go up
        if (loc_zero >= 4) {
            return 0;
        }
        swap = look(node, loc_zero+4);
        node = insert(node, loc_zero+4, 0);
        node = insert(node, loc_zero, swap);

        return node;
    }
    if (dirc == 2) {
        // go left
        if (loc_zero == 3 || loc_zero == 7) {
            return 0;
        }
        swap = look(node, loc_zero+1);
        node = insert(node, loc_zero+1, 0);
        node = insert(node, loc_zero, swap);

        return node;
    }
    if (dirc == 3) {
        // go right
        if(loc_zero == 0 || loc_zero == 4) {
            return 0;
        }
        swap = look(node, loc_zero-1);
        node = insert(node, loc_zero-1, 0);
        node = insert(node, loc_zero, swap);

        return node;
    }
} 

/*
int main(void) {
    int node1 = 12034567;
    int node2 = 12345067;
    printf("node1 is: %d\n", node1);
    for (int i=0; i<4; i++) {
        printf("i = %d, then adj node is %d\n", i, proceed_adj(node1, i));
    }
    printf("\n");
    printf("node2 is: %d\n", node2);
    for (int i=0; i<4; i++) {
        printf("i = %d, then adj node is %d\n", i, proceed_adj(node2, i));
    }
    return 0;
}
*/

int main(void) {
    int n = 76543210;

    vector<int> visited(n,0);
    vector<int> distance(n,n+1);
    queue<int> q;

    int input;
    int node, adj_node;

    while(scanf("%d", &input) != EOF) {

        
        for (int i=0; i<n; i++) {
            visited[i] = 0;
            distance[i] = n+1;
        }
        visited[input] = 1;
        distance[input] = 0;
        q.push(input);


        while(true) {
            node = q.front(); q.pop();
            if (node == 1234567) {
                break;
            }

            for (int i=0; i<4; i++) {
                adj_node = proceed_adj(node, i);
                if (adj_node == 0) { continue; }
                if (visited[adj_node] == 1) { continue; }
                visited[adj_node] = 1;
                distance[adj_node] = distance[node]+1;
                q.push(adj_node);
            }
        }
        printf("%d\n", distance[node]);

    }
}