#include <iostream>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define INF 0x7f7f7f7f
#define PI acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define lowbit(x)   (x)&(-x)
#define Mid(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define swap(x, y) {int tmp = x; x = y; y = tmp;}
#define CLR(arr, val)    memset(arr, val, sizeof(arr))

using namespace std;

int num[10];

int search(int x) {
	for (int i = x; i < 8; i++) {
		if (num[i] == x)	return i;
	}
}

void order(int pos, int target) {
	if (pos > target)
		for (int i = pos; i > target; i--) {
			swap(num[i], num[i - 1]);
		}
	else
		for (int i = pos; i < target; i++) {
			swap(num[i], num[i + 1]);
		}
}

int main() {

	while (~scanf("%d", &num[0])) {
		int ans = 0;

		for (int i = 1; i < 8; i++)
			scanf("%d", &num[i]);
		for (int i = 0; i < 7; i++) {
			int pos = search(i);
			ans += abs(pos - i);
			order(pos, i);
		}
		cout << ans << endl;
	}
	return 0;
}