def distance(a,b) (a[0] - b[0]).abs + (a[1] - b[1]).abs end M = 4.times.map{gets.split.map(&:to_i)} S = Array.new(16) 4.times do |y| 4.times do |x| n = M[y][x].to_i S[n] = [y,x] end end loop do z = S[0] n = (z[0] * 4 + z[1] + 1) % 16 if distance(S[n], S[0]) == 1 S[0] = S[n] S[n] = z else break end end puts (1 .. 15).all?{|n| S[n][0] * 4 + S[n][1] + 1 == n } ? "Yes" : "No"