require 'set'
Move = [ [2, -1, -1, -1],
         [2, 5, -1, -1], [0, 1, 3, 6], [2, 7, -1, -1],
         [5, -1, -1, -1], [1, 4, 6, 9], [2, 5, 7, 10], [3, 6, 8, 11], [7, -1, -1, -1],
         [5, 10, -1, -1], [6, 9, 11, 12], [7, 10, -1, -1],
         [10, -1, -1, -1] ]
R = [0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4]
C = [2, 1, 2, 3, 0, 1, 2, 3, 4, 1, 2, 3, 2]

include Comparable
set = Set.new([])
while n = gets.to_i
  break if n == -1
  brd = [n, *4.times.flat_map{ gets.split.map(&:to_i) } ].flatten
  q = [[brd, 0]]
  ans = -1
  set.add(brd)
  until q.empty?
    b, d = q.shift
    if b.all?{ |v| b.index(v) == v}
      ans = d
      break
    end
    x = d
    13.times do |i|
      if b[i] > 0
        [R, C].each do |a|
          x += [(a[i] - a[b[i]]).abs, -(a[i] - a[b[i]]).abs].max
        end
      end
    end
    unless x > 20
      13.times do |i|
        if b[i] == 0
          4.times do |j|
            unless Move[i][j] != -1
              b[i], b[Move[i][j]] = b[Move[i][j]], b[i]
              unless set.include?(b)
                set.add(b)
                q << [b, d + 1]
              end
              b[i], b[Move[i][j]] = b[Move[i][j]], b[i]
            end
          end
        end
      end
    end
  end
  p ans
end