class Puzzle
  attr_accessor :mat, :step, :cost
  def initialize(mat, step)
    @mat = mat
    @step = step
    @cost = distance
  end
  def >(other)
    return self.cost + self.step < other.cost + other.step ? true : false
  end
  def <(other)
    return self.cost + self.step > other.cost + other.step ? true : false
  end
  def distance
    goal = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0]
    d = 0
    16.times{|n|
      m = @mat.index(goal[n])
      y1, x1 = n.divmod(4)
      y2, x2 = m.divmod(4)
      d += (y1-y2).abs + (x1-x2).abs
    }
    return d
  end
end

class Heep
  def initialize
    @node = []
  end
  def insert(num)
    i = @node.size
    @node[i] = num
    p = (i+1)/2-1
    while i > 0 && @node[p] < @node[i]
      @node[i], @node[p] = @node[p], @node[i]
      i = p
      p = (i+1)/2-1
    end
  end
  def extract
    ret = @node[0]
    if @node.size > 1
      @node[0] = @node.pop()
      maxHeapify(0)
    else
      @node = []
    end
    return ret
  end
  def maxHeapify(i)
    h = @node.size
    largest = i
    l = 2 * i + 1
    largest = l if l < h && @node[l] > @node[largest]
    r = 2 * i + 2
    largest = r if r < h && @node[r] > @node[largest]
    if largest != i then
      @node[i], @node[largest] = @node[largest], @node[i]
      maxHeapify(largest)
    end
  end
  def size
    return @node.size
  end
  def contain(node)
    if @node.index(node) == nil
      return false
    else
      return true
    end
  end
end

mat = []
4.times{|i|
  mat[4*i],mat[4*i+1],mat[4*i+2],mat[4*i+3] = gets.split.map(&:to_i)
}

ini = Puzzle.new(mat, 0)
q = Heep.new
q.insert(ini)

done = {}
loop do
  break if q.size == 0
  it = q.extract
  if it.cost == 0 then
    p it.step
    exit
  else
    done[it.mat.join(" ")] = true
  end
  idx = it.mat.index(0)
  y, x = idx.divmod(4)
  #Down
  unless y == 0 then
    mat = it.mat.clone
    mat[idx], mat[idx-4] = mat[idx-4], mat[idx]
    unless done[mat.join(" ")]
      tmp = Puzzle.new(mat, it.step+1)
      q.insert(tmp)
    end
  end

  #Up
  unless y == 3 then
    mat = it.mat.clone
    mat[idx], mat[idx+4] = mat[idx+4], mat[idx]
    tmp = Puzzle.new(mat, it.step+1)
    unless done[mat.join(" ")]
      tmp = Puzzle.new(mat, it.step+1)
      q.insert(tmp)
    end
  end
  
  #Left
  unless x == 0 then
    mat = it.mat.clone
    mat[idx], mat[idx-1] = mat[idx-1], mat[idx]
    tmp = Puzzle.new(mat, it.step+1)
    unless done[mat.join(" ")]
      tmp = Puzzle.new(mat, it.step+1)
      q.insert(tmp)
    end
  end

  #Right
  unless x == 3 then
    mat = it.mat.clone
    mat[idx], mat[idx+1] = mat[idx+1], mat[idx]
    tmp = Puzzle.new(mat, it.step+1)
    unless done[mat.join(" ")]
      tmp = Puzzle.new(mat, it.step+1)
      q.insert(tmp)
    end
  end
  
end