//config = { input: 'tmp', newline: '\r\n' }; // win
config = { input: '/dev/stdin', newline: '\n' }; // linux

const assert = require('assert');

const SIZE = 16;

init = require('fs').readFileSync(config.input, 'ascii')
  .trim()
  .split(config.newline)
  .map(function (line) {
    return line.split(' ').map(function (num) {
      return parseInt(num, 10).toString(16);
    }).join('');
  })
  .join('')
  .toUpperCase();

goal = '123456789ABCDEF0';

cnt = 0;
step = {};
next = [];
function register_string(str, next, cnt) {
  if (!step.hasOwnProperty(str)) {
    step[str] = cnt;
    next.push(str);
  }
}
register_string(init, next, cnt);

function switch_array(ary, i, j) {
  var tmp = ary[i];
  ary[i] = ary[j];
  ary[j] = tmp;
}

function register(ary, next, i, j, cnt) {
  switch_array(ary, i, j);
  register_string(ary.join(''), next, cnt);
  switch_array(ary, i, j);
}

near = [
  [1,4], [0,2,5], [1,3,6], [2,7],
  [0,5,8], [1,4,6,9], [2,5,7,10], [3,6,11],
  [4,9,12], [5,8,10,13], [6,9,11,14], [7,10,15],
  [8,13], [9,12,14], [10,13,15], [11,14] ];

function widen(cur) {
  var next = [], i, ary, idx0;
  cnt++;
  for (i in cur) {
    ary = cur[i].split('');
    idx0 = ary.indexOf('0');
    for (j in near[idx0])
      register(ary, next, idx0, near[idx0][j], cnt);
  }
  return next;
}

while (next.indexOf(goal) === -1)
  next = widen(next);
console.log(cnt);