require 'set'

def click(a, i, j)
    a[i][j] += 1
    if a[i][j] == 5
        a[i][j] = 0
        blow(a, [ [i, j, 1, 0], [i, j, -1, 0], [i, j, 0, 1], [i, j, 0, -1]])
    end
end

def blow(a, arrows)
    new_arrows = []
    arrows.each do |i, j, di, dj|
        i += di
        j += dj
        next if i < 0 or i > 3 or j < 0 or j > 3
        if a[i][j] > 0
            a[i][j] += 1
        else
            new_arrows << [i, j, di, dj]
        end
    end
    (0..3).each do |i|
        (0..3).each do |j|
            if a[i][j] >= 5
                a[i][j] = 0
                new_arrows += [ [i, j, 1, 0], [i, j, -1, 0], [i, j, 0, 1], [i, j, 0, -1] ]
            end
        end
    end
    return if new_arrows.empty?
    blow(a, new_arrows)
end

def encode(a)
    a.flatten.inject {|sum, x| (sum << 2) + x}
end

start = (1..4).map { gets.split.map(&:to_i) }
if encode(start) == 0
    puts 0
    exit
end
reached = Set.new
reached << encode(start)

queue = [start]
(1..5).each do |c|
    new_queue = []
    until queue.empty?
        a = queue.shift
        (0..3).each do |i|
            (0..3).each do |j|
                b = Marshal.load(Marshal.dump(a))
                click(b, i, j)
                code = encode(b)
                if code == 0
                    puts c
                    exit
                end
                next if reached.include?(code)
                reached << code
                new_queue << b
            end
        end
    end
    queue = new_queue
end

puts -1