#!/usr/bin/ruby
n=gets.to_i
a=n.times.map{gets.split}
perm=gets.split.map{|e|e.to_i-1}
a=perm.map{|e|a[e]}
cur=a[0].min
puts (1...n).all?{|i|
	cur=a[i].select{|e|e>=cur}.min
} ? :YES : :NO

n=gets.to_i
a=Array.new(n)
for i in 0...n
	a[i]=gets.chomp.split.sort
end
cur,ans='','YES'
for i in gets.chomp.split.map(&:to_i)
	if a[i-1][0] > cur
		cur = a[i-1][0]
	elsif a[i-1][1] > cur
		cur = a[i-1][1]
	else
		ans = 'NO'
	end
end
puts ans

def can_p(p, names, use_n)
  can
end

n = gets.to_i
names = (1..n).map{gets.split(" ")}
p = gets.split(" ").map(&:to_i)

can_n = true
can_sn = true

(1..n-1).each do |i|
  prev_n, prev_sn = names[p[i-1] - 1]
  cur_n, cur_sn = names[p[i] - 1]
  can_n_next = can_n && cur_n > prev_n || can_sn && cur_n > prev_sn
  can_sn_next = can_n && cur_sn > prev_n || can_sn && cur_sn > prev_sn
  can_n, can_sn = can_n_next, can_sn_next
  break if ! can_n && ! can_sn
end

ans = can_n || can_sn ? "YES" : "NO"
puts ans

n = gets.to_i
a = n.times.map{gets.split.sort}.unshift(nil)
p = gets.split.map(&:to_i)
s = ''
ans = p.each do |i|
  if a[i][0] > s
    s = a[i][0]
  elsif a[i][1] > s
    s = a[i][1]
  else
    break
  end
end
puts ans ? 'YES' : 'NO'

n = gets.to_i
a = [[]] + n.times.map{gets.split.sort}
p = gets.split.map(&:to_i)
s = []
ans = true
p.each do |i|
  if i == p[0] || a[i][0] > s.last[0]
    s << a[i]
  elsif a[i].reverse[0] > s.last[0]
    s << a[i].reverse
  else
    ans = false
    break
  end
end
puts ans ? "YES" : "NO"

a = []
(n = gets.to_i).times {
  a << gets.chomp.split
}
order = gets.chomp.split.map { |c| c.to_i - 1 }
last = a[order[0]]
(1...n).each { |k|
  cur = []
  a[order[k]].each { |x|
    cur << x if last.any? { |v| x > v }
  }
  if cur.empty?
    puts 'NO'
    exit
  end
  last = cur
}
puts 'YES'

n=gets.to_i
h=[0]
n.times{h<<gets.split}
p=gets.split.map &:to_i
b=""
r="YES"
p.each do |i|
  if h[i].min > b
    b=h[i].min
  elsif h[i].max > b
    b=h[i].max
  else
    r="NO"
    break
  end
end
puts r

def can_p(p, names, use_n)
  can
end

n = gets.to_i
names = (1..n).map{gets.split(" ")}
p = gets.split(" ").map(&:to_i)

puts "NAMES: #{names}"
puts "P: #{p}"

can_n = true
can_sn = true

(1..n-1).each do |i|
  prev_n, prev_sn = names[p[i-1] - 1]
  cur_n, cur_sn = names[p[i] - 1]
  can_n_next = can_n && cur_n > prev_n || can_sn && cur_n > prev_sn
  can_sn_next = can_n && cur_sn > prev_n || can_sn && cur_sn > prev_sn
  can_n, can_sn = can_n_next, can_sn_next
  break if ! can_n && ! can_sn
end

ans = can_n || can_sn ? "YES" : "NO"
puts ans

n = gets.to_i
a = n.times.map{gets.chomp}
p = gets.split.map(&:to_i)
def change a
	name1, name2 = a.split
	tmp = name1
	return [name2, tmp].join(" ")
end
def compare i, j, a
	3.times do |k|
		if a[i-1] > a[j-1]
			if k == 0
				a[j-1] = change a[j-1]
			elsif k == 1
				a[i-1] = change a[i-1]
			elsif k == 2
				a[j-1] = change a[j-1]
			end
		elsif a[i-1] < a[j-1]
			return true
			break
		end
	end
end
n.times do |i|
	if compare(p[i], p[i+1], a) == false
		puts "NO"
		break
	elsif i == p.size - 2
		puts "YES"
		break
	end
end

a = []
(n = gets.to_i).times {
  a << gets.chomp.split
}
order = gets.chomp.split.map { |c| c.to_i - 1 }
(0...n - 1).each { |k|
  i, j = order[k, k + 2]
  unless a[i][0] < a[j][0] || a[i][0] < a[j][1] || a[i][1] < a[j][0] || a[i][1] < a[j][1]
    puts 'NO'
    exit
  end
}
puts 'YES'


t=gets.chomp.to_i
t.times do
    r = gets.chomp.to_i
    if(r>=1900)
        puts "Division 1"
    end
    if(r>=1600 && r<=1899)
        puts "Division 2"
    end
    if(r>=1400 && r<=1599)
        puts "Division 3"
    end
    if(r<=1399)
        puts "Division 4"
    end
end

def division?(n)
  if n <= 1399
    puts "Division 4"
  elsif n >= 1400 && n <= 1599
    puts "Division 3"
  elsif n >= 1600 && n <= 1899
    puts "Division 2"
  else
    puts "Division 1"
  end
end

t = gets.chomp.to_i

input = []
(0..t-1).each do |i|
  input.push(gets.chomp.to_i)
end

input.each do |i|
  division?(i)
end

input1 = gets.to_i
input1.times do
  output = ["Division"]
  case (input = gets.to_i)
  when (..1399) then output << "4"
  when (1400..1599) then output << "3"
  when (1600..1899) then output << "2"
  when (1900..) then output << "1"
  end
  puts output.join(" ")
end

gets.to_i.times do
  n = gets.to_i
  x = if n <= 1399
      4
    elsif n <= 1599
      3
    elsif n <= 1899
      2
    else
      1
    end
  puts "Division #{x}"
end

t = gets.chomp().to_i

for i in 1 .. t
    rating = gets.chomp().to_i
    if 1900 <= rating
        puts "Division 1"
    elsif 1600 <= rating and rating <= 1899
        puts "Division 2"
    elsif 1400 <= rating and rating <= 1599
        puts "Division 3"
    elsif rating <= 1399
        puts "Division 4"
    end
end

gets.to_i.times do
  a = gets.to_i
  if a>= 1900
    puts "Division 1"
  elsif a >= 1600
    puts "Division 2"
  elsif a >= 1400
    puts "Division 3"
  else
    puts "Division 4"
  end
end

tests = gets.to_i

tests.times do

    i = gets.to_i

    if i >= 1900
        puts "Division 1"
    elsif 1600 <= i and i <= 1899
        puts "Division 2"
    elsif 1400 <= i and i <= 1599
        puts "Division 3"
    else
        puts "Division 4"
    end
end


#! ruby
# try Codeforces Round 325 div2
# author: Leonardone @ NEETSDKASU

n = gets.to_i
*a, b = readlines.map{|x| x.chomp.split.map(&:to_i)}

f1 = [[:H2S, n - 1, 1, 0, 0]]

sp = 100 * 100
ans = 100 * 100
while !f1.empty?
	f2 = []
	f1.each do |f, x, y, c, r|
		case f
		when :H2S
			if x > 0
				nc = c + a[y][x - 1]
				if x - 1 == 0 && y == 0
					if nc < sp
						sp = nc
						f2 << [:S2H, 0, 0, nc, r]
					end
				else
					f2 << [f, x - 1, y, nc, r]
				end
			end
			if y == 1
				nc = c + b[x]
				if x == 0
					if nc < sp
						sp = nc
						f2 << [:S2H, 0, 0, nc, x]
					end
				else
					f2 << [f, x, 0, nc, x]
				end
			end
		when :S2H
			if x < n - 1
				nc = c + a[y][x]
				if x + 1 == n - 1 && y == 1
					ans = [ans, nc].min
				else
					f2 << [f, x + 1, y, nc, r]
				end
			end
			if y == 0 && r != x
				nc = c + b[x]
				if x == n - 1
					ans = [ans, nc].min
				else
					f2 << [f, x, 1, nc, r]
				end
			end
		end
	end
	f1 = f2
end
puts ans

n=gets.to_i
cross2=gets.split(" ").map(&:to_i)
cross1=gets.split(" ").map(&:to_i)
crossav=gets.split(" ").map(&:to_i)

t=999999999999999
for i in 0...n
	for j in 0...n
		next if i == j

		cost = 0
		cost += (cross1[i..-1].reduce(:+)||0) + crossav[i] + (cross2[0...i].reduce(:+)||0)
		cost += (cross1[j..-1].reduce(:+)||0) + crossav[j] + (cross2[0...j].reduce(:+)||0)

		t = [t, cost].min
	end
end

p t

n, first_row_traffic, second_row_traffic, avenue_traffic, sums =
	gets.chomp.to_i,
	gets.chomp.split(" ").map(&:to_i).unshift(0),
	gets.chomp.split(" ").map(&:to_i).push(0),
	gets.chomp.split(" ").map(&:to_i),
	[]

(n-1).downto 0 do |i|
	sum = 0

	sum += avenue_traffic[i]

	i.downto 0 do |j|
		sum += first_row_traffic[j]
	end

	i.upto (n-1) do |j|
		sum += second_row_traffic[j]
	end

	sums << sum
end
sums.sort!
puts (sums[0] + sums[1])

n = gets.chomp.to_i
top = gets.split.map{ |x| x.to_i }
bot = gets.split.map{ |x| x.to_i }
over = gets.split.map{ |x| x.to_i }
cost = []
n.times do |i|
  sum = over[i]
  (0...i).each { |j| sum += top[j] }
  (i...(n-1)).each { |j| sum += bot[j] }
  cost << sum
end

max= top.inject(:+) + bot.inject(:+) + over.inject(:+)
r1 = max
b1 = 0
n.times do |i|
  if cost[i] < r1
    r1 = cost[i]
    b1 = i
  end
end
r2 = max
n.times do |i|
  if cost[i] < r2 and i != b1
    r2 = cost[i]
  end
end

puts r1+r2

n = gets.chomp.to_i
as = []
2.times do
  as << gets.chomp.split(' ').map(&:to_i)
end
bs = gets.chomp.split(' ').map(&:to_i)

cs = []
for i in 0..1
  c = [0]
  for j in 1..n - 1
    c << c[j-1] + as[i][j-1]
  end
  cs << c
end

ds = []
for i in 0..n-1
  ds << cs[0][i] + bs[i] + cs[1][n-1] - cs[1][i]
end

ds.sort!
puts ds[0] + ds[1]

# coding; utf-8

h = gets.strip.to_i
fr = gets.strip.split(' ').map(&:to_i)
sr = gets.strip.split(' ').map(&:to_i)
av = gets.strip.split(' ').map(&:to_i)

times = [ ]
h.times do |i|
  t = av[i]
  0.upto(i - 1) { |j| t += fr[j] }
  i.upto(sr.length - 1) { |j| t += sr[j] }

  times.push(t)
end

times.sort!
puts (times[0] + times[1]).to_s


def run
  n, x = gets.chomp.split(" ").map(&:to_i)
  a = gets.chomp.split(" ").map(&:to_i)
  rich = a.select{|el| el >= x}
  poor = a.select{|el| el < x}.sort_by{|c| -c}
  count = rich.count
  surplus = rich.reduce(0) {|acc, el| acc + (el > x ? el - x : 0)}
  poor.each do |p|
    if surplus >= x - p
      surplus -= (x - p)
      count += 1
    else
      break
    end
  end
  puts count
end

def main
  t = gets.chomp.to_i

  t.times do
    run
  end
end

main

t = gets.to_i
t.times do
  n, x = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i).sort.reverse
  count = 0
  savings_sum = 0
  a.each_with_index do |v, i|
    savings_sum += v
    if savings_sum >= x * (i + 1)
      count += 1
    else
      break
    end
  end
  puts count
end

1.upto(gets.to_i) do
  n, x = gets.split(' ').map(&:to_i)
  a = gets.split(' ').map(&:to_f).sort.reverse

  s = 0
  n = a.count { |c| c >= x }
  max = n

  a.each_with_index do |e, i|
    s += e

    n -= 1 if e >= x

    break if s/(i+1) < x

    max = [max, n + i + 1].max
  end

  puts max
end


n = gets.to_i
a = gets.split.map(&:to_i).sort
if n == 1
  p a[0]**2
else
  x = a[0,n/2].inject(:+)
  y = a[n/2,(n+1)/2].inject(:+)
  p x**2 + y**2
end

# Author : Fuad Ashraful Mehmet
#Date: 18th April

def solve()
    n=gets.to_i
    ar=gets.split.map(&:to_i).sort
    if n==1
        return ar[0]**2
    else
        xcor=ar[0,n/2].inject(:+)
        ycor=ar[n/2,(n+1)/2].inject(:+)

        return xcor**2+ycor**2
    end
end

ans=solve()
puts ans

n = gets.to_i
a = gets.split.map(&:to_i).sort
if n == 1
  p a[0]
else
  x = a[0,n/2].inject(:+)
  y = a[n/2,(n+1)/2].inject(:+)
  p x**2 + y**2
end


#!/usr/bin/ruby1.9

def display_array(matrix)
	return
	matrix.each do |row|
		puts row.join(' ')
	end
	puts
end

def swap_row(matrix, a, b, step, lines)
	if step == 1
		lines.push('%d %d %d' % [step, a+1, b+1])
	else
		n = matrix.length
		lines.push('%d %d %d' % [step, n-a, n-b])
	end
	row = matrix[a]
	matrix[a] = matrix[b]
	matrix[b] = row
end

def sift_down(m, step, lines)
	n = m.length
	counts = n.times.collect {|r| [0, 0, r] }
	n.times do |r|
		n.times do |c|
			if m[r][c] == 1
				counts[r][0] += 1
				counts[r][1] = -c
			end
		end
	end
	counts.sort!

	target2found = Array.new(n, 0)
	found2target = Array.new(n, 0)
	n.times do |target|
		found = counts[target][2]
		target2found[target] = found
		found2target[found] = target
	end
	n.times do |target|
		found = target2found[target]
		next if found == target
		target2found[found2target[target]] = found
		found2target[found] = found2target[target]
		found2target[target] = target
		swap_row(m, found, target, step, lines)
	end
end

n = STDIN.readline.to_i
m = n.times.collect { Array.new(n, 0) }
(n-1).times do
	r, c = STDIN.readline.split.collect {|s| s.to_i }
	m[r-1][c-1] = 1
end
display_array(m)

lines = []

sift_down(m, 1, lines)
display_array(m)

m = m.transpose.reverse
#display_array(m)

sift_down(m, 2, lines)
#display_array(m)
display_array(m.reverse.transpose)

puts lines.length
puts lines.join("\n")

def swap_cols(mat,i,j)
	aux = 0
	n = mat.length
	(0..(n-1)).each {|k|
		aux = mat[k][i]
		mat[k][i] = mat[k][j]
		mat[k][j] = aux
	}
end

n = gets.chomp.to_i
mat = []
(1..n).each {|pp|
  line = []
  (1..n).each {|qq|
	line.push(0)
  }
  mat.push(line)
}

(1..(n-1)).each { |k|
   i,j = gets.chomp.split(" ").collect! {|x| x.to_i}
   mat[i-1][j-1] = 1
}

actions = []
arr = []
pos = []
m = 0
aaux = []
(0..(n-1)).each { |k|
	p = m
	q = m
	while mat[k][p]==1
	  q +=1
	  p +=1
	  m +=1
	end
	while q<n
	  if mat[k][q]==1
	    #mat[k][p] = 1
		#mat[k][q] = 0
		swap_cols(mat,p,q)
		actions.push("2 #{p+1} #{q+1}")
		p += 1
		m +=1

	  end
	  q +=1
	end

	pos.push(k)
}

(0..(n-1)).each {|k|
  st = n-1
  while mat[k][st]==0 and st>=0
   st -=1
  end
  arr.push(st)
}
p = 0
min = arr[0]
while p<n
 min = arr[p]
 pos = p
 q = p+1
 while q<n
	if min > arr[q]
		min = arr[q]
		pos = q
	end
	q +=1
 end
 if p != pos
 arr[pos] = arr[p]
 arr[p] = min
 actions.push("1 #{p+1} #{pos+1}")
 end
 p += 1
end

puts actions.length
actions.each { |t|
 puts t
}

#!/usr/bin/ruby1.9

def display_array(matrix)
	return
	matrix.each do |row|
		puts row.join(' ')
	end
	puts
end

def swap_row(matrix, a, b, step, lines)
	if step == 1
		lines.push('%d %d %d' % [step, a+1, b+1])
	else
		n = matrix.length
		lines.push('%d %d %d' % [step, n-a, n-b])
	end
	row = matrix[a]
	matrix[a] = matrix[b]
	matrix[b] = row
end

def sift_down(m, step, lines)
	n = m.length
	counts = n.times.collect {|r| [0, 0, r] }
	n.times do |r|
		n.times do |c|
			if m[r][c] == 1
				counts[r][0] += 1
				counts[r][1] = -c
			end
		end
	end
	counts.sort!

	permute = Array.new(n, 0)
	n.times do |new_position|
		old_position = counts[new_position][2]
		permute[old_position] = new_position
	end

	n.times do |pos|
		while permute[pos] != pos
			swap_row(m, pos, permute[pos], step, lines)
			t = permute[pos]
			permute[pos] = permute[t]
			permute[t] = t
		end
	end
end

n = STDIN.readline.to_i
m = n.times.collect { Array.new(n, 0) }
(n-1).times do
	r, c = STDIN.readline.split.collect {|s| s.to_i }
	m[r-1][c-1] = 1
end
display_array(m)

lines = []

sift_down(m, 1, lines)
display_array(m)

m = m.transpose.reverse

sift_down(m, 2, lines)
display_array(m.reverse.transpose)

puts lines.length
puts lines.join("\n")

#!/usr/bin/ruby1.9

def display_array(matrix)
	matrix.each do |row|
		puts row.join(' ')
	end
	puts
end

def swap_row(matrix, a, b, step, lines)
	if step == 1
		lines.push('%d %d %d' % [step, a+1, b+1])
	else
		n = matrix.length
		lines.push('%d %d %d' % [step, n-a, n-b])
	end
	row = matrix[a]
	matrix[a] = matrix[b]
	matrix[b] = row
end

def sift_down(m, step, lines)
	n = m.length
	counts = n.times.collect {|r| [0, 0, r] }
	n.times do |r|
		n.times do |c|
			if m[r][c] == 1
				counts[r][0] += 1
				counts[r][1] = -c
			end
		end
	end
	counts.sort!

	permute = Array.new(n, 0)
	n.times do |new_position|
		old_position = counts[new_position][2]
		permute[old_position] = new_position
	end

	n.times do |pos|
		while permute[pos] != pos
			swap_row(m, pos, permute[pos], step, lines)
			t = permute[pos]
			permute[pos] = permute[t]
			permute[t] = t
		end
	end
end

n = STDIN.readline.to_i
m = n.times.collect { Array.new(n, 0) }
(n-1).times do
	r, c = STDIN.readline.split.collect {|s| s.to_i }
	m[r-1][c-1] = 1
end
display_array(m)

lines = []

sift_down(m, 1, lines)
display_array(m)

m = m.transpose.reverse

sift_down(m, 2, lines)
display_array(m.reverse.transpose)

puts lines.length
puts lines.join("\n")

#!/usr/bin/ruby1.9

def display_array(matrix)
	return
	matrix.each do |row|
		puts row.join(' ')
	end
	puts
end

def swap_row(matrix, a, b, step, lines)
	if step == 1
		lines.push('%d %d %d' % [step, a+1, b+1])
	else
		n = matrix.length
		lines.push('%d %d %d' % [step, n-a, n-b])
	end
	row = matrix[a]
	matrix[a] = matrix[b]
	matrix[b] = row
end

def sift_down(m, step, lines)
	n = m.length
	counts = n.times.collect {|r| [0, 0, r] }
	n.times do |r|
		n.times do |c|
			if m[r][c] == 1
				counts[r][0] += 1
				counts[r][1] = -c
			end
		end
	end
	counts.sort!

	n.times do |target_r|
		found_r = counts[target_r][2]
		next if found_r <= target_r
		swap_row(m, found_r, target_r, step, lines)
	end
end

n = STDIN.readline.to_i
m = n.times.collect { Array.new(n, 0) }
(n-1).times do
	r, c = STDIN.readline.split.collect {|s| s.to_i }
	m[r-1][c-1] = 1
end
display_array(m)

lines = []

sift_down(m, 1, lines)
display_array(m)

m = m.transpose.reverse
#display_array(m)

sift_down(m, 2, lines)
#display_array(m)
display_array(m.reverse.transpose)

puts lines.length
puts lines.join("\n")

#!/usr/bin/ruby1.9

def display_array(matrix)
	return
	puts
	matrix.each do |row|
		puts row.join(' ')
	end
end

def swap_row(matrix, a, b, step, lines)
	lines.push('%d %d %d' % [step, a+1, b+1])
	row = matrix[a]
	matrix[a] = matrix[b]
	matrix[b] = row
end

def sift_down(m, step, lines)
	n = m.length
	counts = n.times.collect {|r| [0, 0, r] }
	n.times do |r|
		n.times do |c|
			if m[r][c] == 1
				counts[r][0] += 1
				counts[r][1] = -c
			end
		end
	end
	counts.sort!

	n.times do |target_r|
		found_r = counts[target_r][2]
		next if found_r <= target_r
		swap_row(m, found_r, target_r, step, lines)
	end
end

n = STDIN.readline.to_i
m = n.times.collect { Array.new(n, 0) }
(n-1).times do
	r, c = STDIN.readline.split.collect {|s| s.to_i }
	m[r-1][c-1] = 1
end
display_array(m)

lines = []

sift_down(m, 1, lines)
display_array(m)

m = m.transpose.reverse
#display_array(m)

sift_down(m, 2, lines)
#display_array(m)
display_array(m.reverse.transpose)

puts lines.length
puts lines.join("\n")

def swap_cols(mat,i,j)
	aux = 0
	n = mat.length
	(0..(n-1)).each {|k|
		aux = mat[k][i]
		mat[k][i] = mat[k][j]
		mat[k][j] = aux
	}
end

n = gets.chomp.to_i
mat = []
(1..n).each {|pp|
  line = []
  (1..n).each {|qq|
	line.push(0)
  }
  mat.push(line)
}

(1..(n-1)).each { |k|
   i,j = gets.chomp.split(" ").collect! {|x| x.to_i}
   mat[i-1][j-1] = 1
}

actions = []
arr = []
pos = []
m = 0
aaux = []
(0..(n-1)).each { |k|
	p = m
	q = m
	while mat[k][p]==1
	  q +=1
	  p +=1
	  m +=1
	end
	while q<n
	  if mat[k][q]==1
	    #mat[k][p] = 1
		#mat[k][q] = 0
		swap_cols(mat,p,q)
		actions.push("2 #{p+1} #{q+1}")
		p += 1
		m +=1

	  end
	  q +=1
	end

	pos.push(k)
}

(0..(n-1)).each {|k|
  st = n-1
  while mat[k][st]==0 and st>=0
   st -=1
  end
  arr.push(st)
}
pi = 0
pj = 0
while pi<n-1
 pj = pi+1
 while pj<n
	if arr[pi]>arr[pj]
		aux = arr[pi]
		arr[pi] = arr[pj]
		arr[pj] = aux

		aux = pos[pi]
		pos[pi] = pos[pj]
		pos[pj] = aux

		actions.push("1 #{pi+1} #{pj+1}")
	end
	pj +=1
 end
 pi += 1
end

puts actions.length
actions.each { |t|
 puts t
}

def swap_cols(mat,i,j)
	aux = 0
	n = mat.length
	(0..(n-1)).each {|k|
		aux = mat[k][i]
		mat[k][i] = mat[k][j]
		mat[k][j] = aux
	}
end

n = gets.chomp.to_i
mat = []
(1..n).each {|pp|
  line = []
  (1..n).each {|qq|
	line.push(0)
  }
  mat.push(line)
}

(1..(n-1)).each { |k|
   i,j = gets.chomp.split(" ").collect! {|x| x.to_i}
   mat[i-1][j-1] = 1
}

actions = []
arr = []
pos = []
m = 0
aaux = []
(0..(n-1)).each { |k|
	p = m
	q = m
	while mat[k][p]==1
	  q +=1
	  p +=1
	  m +=1
	end
	while q<n
	  if mat[k][q]==1
	    #mat[k][p] = 1
		#mat[k][q] = 0
		swap_cols(mat,p,q)
		actions.push("2 #{p+1} #{q+1}")
		p += 1
		m +=1

	  end
	  q +=1
	end

	pos.push(k)
}

(0..(n-1)).each {|k|
  st = n-1
  while mat[k][st]==0 and st>=0
   st -=1
  end
  arr.push(st)
}
pi = 0
pj = 0
while pi<n-1
 pj = pi+1
 while pj<n
	if arr[pi]>arr[pj]
		aux = arr[pi]
		arr[pi] = arr[pj]
		arr[pj] = aux

		aux = pos[pi]
		pos[pi] = pos[pj]
		pos[pj] = aux

		actions.push("1 #{pos[pi]+1} #{pos[pj]+1}")
	end
	pj +=1
 end
 pi += 1
end

puts actions.length
actions.each { |t|
 puts t
}


#!/usr/bin/ruby
n=gets.to_i
h=Hash.new
n.times{|i|
  x=s=gets.chomp
  if !h[s]
    h[s]=0
    puts "OK"
  else
    h[s]+=1
    puts s+h[s].to_s
  end
}

bd = {}
gets.to_i.times do
	name = gets.chomp
	if bd.has_key?(name)
		bd[name] += 1
		puts name + bd[name].to_s
	else
		bd[name] = 0
		puts "OK"
	end
end

users = Hash.new

n = gets.to_i
n.times do
	name = gets.chomp
	if(users[name]==nil) then
		puts("OK")
		users[name] = 1
	else
		puts("#{name}#{users[name]}")
		users[name] += 1
	end
end

n = gets.to_i

base = {}

answer = []

n.times do |i|
    z = gets.chomp
    if base[z] then
        answer << (z + base[z].to_s)
        base[z] += 1
    else
        answer << "OK"
        base[z] = 1
    end
end

answer.map{|i| puts i}

n = gets.chomp.to_i

db = Hash.new
for i in 0..n-1
	s = gets.chomp
	if db.has_key?(s)
		idx = db[s] + 1
		while true
			ss = s + idx.to_s
			if not db.has_key?(ss)
				db[s] = idx
				db[ss] = 0
				puts ss
				break
			else
				idx += 1
			end
		end
	else
		db[s] = 0
		puts "OK"
	end
end

n = gets.to_i
hash = Hash.new
n.times do
	str = gets.chomp!
	if hash.key?(str)
		hash[str] += 1
		puts str + hash[str].to_s
	else
		hash[str] = 0
		puts 'OK'
	end
end

n=STDIN.gets.chomp.to_i
db={}
db.default=-1
n.times() {
  name=STDIN.gets.chomp
  db[name]+=1
  if 0<db[name]
    print(name+db[name].to_s,"\n")
  else
    print("OK\n")
  end
}

require 'set'
n = gets.to_i
a = Hash.new
n.times do
  name = gets.strip
  if a.include? name
    puts name + a[name].to_s
    a[name]+=1
  else
    puts "OK"
    a[name]=1
  end
end

n = gets.to_i
names = {}
n.times do
    name = gets.strip
    if names.has_key? name then
        names[name] += 1
        puts name + names[name].to_s
    else
        names[name] = 0
        puts 'OK'
    end
end

gets
h={}
puts$<.map{|x|x.chomp!
(h[x]=1
next :OK) if !h[x]
i=h[x]
i+=1while h[x+i.to_s]
h[x]=i
h[x+i.to_s]=x+i.to_s}

n = gets.to_i
names  = Array.new
logins = Hash.new
for i in 1..n
    name = gets.chomp
    if not logins[name]
        logins[name] = 1
        names << "OK"
    else
        names << "#{name}#{logins[name]}"
        logins[name] += 1
    end
end

for login in names
    puts login
end

n = gets.to_i
names  = Array.new
logins = Hash.new
for i in 1..n
    name = gets.chomp
    names << name
end

for name in names
    if not logins[name]
        logins[name] = 1
        puts "OK"
    else
        puts "#{name}#{logins[name]}"
        logins[name] += 1
    end
end

using = Hash.new
(gets.to_i).times do
	s = gets.chomp
	puts using.key?(s) ? s+using[s].to_s : "OK"
	using[s] = using[s].to_i+1
end

class Database
  def initialize
    @size = 0
    @records = {}
    @names = []
  end

  def insert name
    if @records[name].nil?
      @records[name] = [name]
      @names <<  "OK"
    else
      id = @records[name].length
      @records[name] << "#{name}#{id}"
      @names << "#{name}#{id}"
    end
  end

  def print
    @names.each { |name| puts name }
  end
end

db = Database.new
amount_of_users = gets.chomp.to_i
users = []
while 0 < amount_of_users do
  amount_of_users -= 1
  user = gets.chomp
  users << user
end

users.each do |user|
  db.insert(user)
end

db.print

n = STDIN.readline
lines = STDIN.readlines
h = {}

lines.each do |l|
  l = l.chomp
  if !h[l]
    puts 'OK'
  else
    puts "#{l}#{h[l]}"
  end

  h[l] ||= 0
  h[l] += 1
end

N = gets.chomp.to_i
memo=Hash.new(-1)
N.times do
  name=gets.chomp
  memo[name]+=1
  if memo[name]==0
    puts "OK"
  else
    puts name+memo[name].to_s
  end
end

a = {}
n = gets.to_i
n.times do
    s = gets.chomp.to_s
    if a[s] then
        puts s + a[s].to_s
        a[s] += 1
    else
        a[s] = 1
        puts "OK"
    end
end

h=Hash.new(0)
n=gets.to_i
n.times{
	s=gets.chomp
	if h[s]==0 then
		puts "OK"
	else
		puts s+h[s].to_s
	end
	h[s]+=1
}

n,m = gets.chomp.to_i
o = {}
n.times.each do |i|
  s = gets.chomp
  if o[s].nil?
    o[s] = 1
    puts "OK"
  else
    puts s+o[s].to_s
    o[s] += 1
  end
end

n = gets.to_i
h = Hash.new { |hash, key| hash[key] = 0  }
n.times do |i|
	x = gets.chomp
	h[x] += 1
	puts h[x] > 1 ? "#{x}#{h[x]-1}" : 'OK'
end

c = gets

map = Hash.new

c.to_i.times {

  name = gets.strip

  if map[name] == nil
    puts "OK"
    map[name] = 0
  else
    map[name] += 1
    puts name + map[name].to_s
  end

}

n = gets.to_i
data = Hash.new
n.times do
  name = gets.chop!
  v = data[name]
  if(v == nil) then v = 0; puts "OK"
  else puts name + v.to_s end
  data[name] = v + 1
end

n= gets.to_i
res=[]
names ={}
while n>0
na=gets.chomp
if names[na]
 names[na]=names[na]+1
 res.push (na+names[na].to_s)
else
 res.push "OK"
 names[na]=0
end
n=n-1
end

res.each{|a| puts a.to_s  }

n = Integer gets
names = Hash.new
for i in 0...n
	str = gets.chomp
	if names.has_key? str
	  print str, names[str], "\n"
	  names[str] += 1
  else
    puts "OK"
    names[str] = 1
  end
end

a={}
gets.to_i.times{q=a[s=gets.chop];puts q ?s+q.to_s: "OK";a[s]=q.to_i+1}

n = gets.to_i

database = {}

n.times do
	username = gets.chomp
	if database[username].nil?
		database[username] = 1
		puts "OK"
	else
		puts username + database[username].to_s
		database[username] += 1
	end
end

#Made By Hasbi Hamza
n=gets.chomp.to_i
hash={}
lines=[]
for i in 1..n
  lines[i]=gets.chomp.to_s
  hash[lines[i]]=0
end
for i in 1..n
  if hash[lines[i]]==0 then puts "OK"
  else puts("#{lines[i]}#{hash[lines[i]]}")
end
   hash[lines[i]]+=1
end

n = gets.to_i
a = Hash.new(0)
n.times do
  s = gets.chomp
  if a[s] == 0
    a[s] = 1
    puts 'OK'
  else
    puts s + a[s].to_s
    a[s] += 1
  end
end

n = gets.chomp.to_i
h = {}
n.times do
  s = gets.chomp
  i = h[s]
  if i.nil?
    puts 'OK'
    h[s] = 1
  else
    puts "#{s}#{h[s]}"
    h[s] += 1
  end
end

class Array
  def r
    self.map { |q| q.to_i }
  end
end

def read_line
  STDIN.gets.split(" ")
end

def read_to_array
  read_line.to_a
end

def read_numbers_to_array
  read_to_array.r
end

a = Hash.new(0)

n = STDIN.gets.to_i
n.times do
  str = read_to_array[0]
  a[str] += 1
  if a[str] > 1
    print "#{str}#{a[str] - 1}"
    puts
  else
    puts "OK"
  end
end

n = gets.to_i
a = Hash.new(0)
n.times do
  s = gets.chomp
  puts a[s] >= 1 ? "#{s}#{a[s]}" : "OK"
  a[s] += 1
end

n = gets.to_i
a = Hash.new(0)
n.times do
  s = gets.chomp
  if a[s] >= 1
    puts s + a[s].to_s
    a[s] += 1
  else
    a[s] = 1
    puts 'OK'
  end
end

n = gets.to_i
a = Hash.new(false)
n.times do
  s = gets.chomp
  if a[s]
    a[s] += 1
    puts s + a[s].to_s
  else
    a[s] = 0
    puts 'OK'
  end
end

n = gets.chomp.to_i
h = {}
n.times do
  s = gets.chomp
  if h[s].nil? then
    puts 'OK'
    h[s] = 1
  else
    puts s + h[s].to_s
    h[s] += 1
  end
end

names = {}
noNames = gets.chomp.to_i
(1..noNames).each do |name|
  name = gets.chomp
  if names[name] == nil
    names[name] = 1
    puts "OK"
  else
    newName = "#{name}#{names[name]}"
    names[name] += 1
    names[newName] = 1
    puts newName
  end
end

numOfInputs = gets.to_i
inputHash = {}
numOfInputs.downto(1) do
    input = gets.chomp
    if (inputHash.has_key?(input))
        numOfOccurence = inputHash[input]

        puts "#{input}#{numOfOccurence}"

        numOfOccurence = numOfOccurence + 1
        inputHash[input] = numOfOccurence
    else
        inputHash[input] = 1
        puts "OK"
    end
end

n = gets.to_i
db = {}

while n > 0 do
  u = gets[0..-2]
  if db.key?(u)
    printf("%s%d\n", u, db[u])
    db[u] += 1
  else
    puts "OK"
    db[u] = 1
  end
  n -= 1
end

n = gets.to_i
db = {}

while n > 0 do
  u = gets.chomp
  if db.has_key?(u)
    puts u + db[u].to_s
    db[u] += 1
  else
    puts "OK"
    db[u] = 1
  end
  n -= 1
end

n = gets.to_i
db = {}

while n > 0 do
  u = gets.chomp
  if db.has_key?(u)
    printf("%s%d\n", u, db[u])
    db[u] += 1
  else
    puts "OK"
    db[u] = 1
  end
  n -= 1
end

h=Hash.new
gets
$<.map{|n|
	s=n.chop
    if(h.has_key?(s))
        h[s]+=1
        puts s+h[s].to_s
    else
    	h[s] = 0
        puts "OK"
    end
}

n = gets.chomp.to_i

hash = {}

n.times do |i|
    name = gets.chomp
    if hash[name] == nil
        hash[name] = 1
        puts "OK"
    else
        puts "#{name}#{hash[name]}"
        hash[name] += 1
    end
end

n = gets.to_i

cache = Hash.new { 0 }

1.upto(n) do
  name = gets.chomp

  puts cache[name].zero? ? 'OK' : "#{name}#{cache[name]}"

  cache[name] += 1
end

a = {}
for _ in 1..gets.chomp.to_i
  name = gets.chomp
  if a.has_key? name
    a[name] += 1
    puts "#{name}#{a[name]}"
  else
    puts 'OK'
    a[name] = 0
  end
end

n = gets.chomp.to_i

hash = {}

1.upto(n) do
	name = gets.chomp
	if hash[name] == nil
		hash[name] = 1
		puts "OK"
	else
		puts "#{name}#{hash[name]}"
		hash[name] += 1
	end
end

n, db = gets.to_i, Hash.new(-1)
n.times do
	name = gets.chomp.to_sym
	db[name] += 1
	if db[name] > 0
		print name, db[name], "\n"
	else
		puts :OK
	end
end

accountNum = gets.chomp.to_i

hash = {}
numberHash = {}

accountNum.times do
	account = gets.chomp

	if hash.has_key? account
		numberHash[account] += 1
		newAccount = account + numberHash[account].to_s
		puts "#{newAccount}"
	else
		puts 'OK'
		hash[account] = 1
		numberHash[account] = 0
	end
end

n=STDIN.gets.chomp.to_i
db={}
db.default=0
n.times() {
  name=STDIN.gets.chomp
  db[name]+=1
  if 1<db[name]
    print(name+db[name].to_s,"\n")
  else
    print("OK\n")
  end
}

gets
h={}
puts$<.map{|x|x.chomp!
next h[x]=:OK if !h[x]
i=x=~/\d+$/ ? (x=$`
$1.to_i) : 1
i+=1while h[x+i.to_s]
h[x]=x+i.to_s}

gets
h={}
puts$<.map{|x|x.chomp!
next h[x]=:OK if !h[x]
i=1
i+=1while h[x+i.to_s]
h[x]=x+i.to_s}

using = Hash.new(0)
(gets.to_i).times do
	s = gets.chomp
	id = using.key?(s) ? using[s].to_s : "";
	puts s+id
	using[s] = id.to_i+1
end

class Database
  def initialize
    @size = 0
    @records = {}
  end

  def insert name
    if @records[name].nil?
      @records[name] = [name]
      return "OK"
    else
      id = @records[name].length
      @records[name] << "#{name}#{id}"
    end
  end

  def print
    @records.each do |k, v|
      v.each do |user|
        if user == k
          puts "OK"
        else
          puts user
        end
      end
    end
  end
end

db = Database.new
amount_of_users = gets.chomp.to_i
users = []
while 0 < amount_of_users do
  amount_of_users -= 1
  user = gets.chomp
  users << user
end

users.each do |user|
  db.insert(user)
end

db.print

n = gets.to_i

database = Hash.new(0)

n.times do
	username = gets.chomp
	if database[username].nil?
		puts "OK"
		database[username] = 1
	else
		puts username + database[username].to_s
		database[username] += 1
	end
end

n = gets.to_i

database = {}

n.times do
	username = gets.strip
	count = 0
	if database[username].nil?
		database[username] = 1
		puts "OK"
	else
		test = username
		while database[test]
			count += 1
			test = username + count.to_s
		end
		username = test
		database[username] = 1
		puts username
	end
	print database
	puts
end

n = gets.to_i

database = Hash.new(0)

n.times do
	username = gets.chomp
	count = 0
	if database[username].nil?
		puts "OK"
		database[username] = 1
	else
		puts username + database[username].to_s
		database[username] += 1
	end
end

gets; names, db = STDIN.read.split("\n"), {}
names.each do |i|
	i = i.to_sym
	if db.keys.include? i
		n = db[i] + 1
		puts "#{i}#{n}"
	else
		db[i] = 0
		puts :OK
	end
end


n = gets.chomp.to_i
data = []
n.times do
  a, b = gets.chomp.split(" ").map(&:to_i)
  data.push([a,b])
end
d = {}
(0...data.length).each do |i|
  d[i] = 0
end

(0...data.length).each do |i|
  a, b = data[i]
  (0...data.length).each do |j|
    candidate_a, candidate_b = data[j]
    if a == candidate_b && i != j
      d[i] = 1
      break
    end
  end
end

total = 0
d.each do |key, val|
  total += 1 if val == 0
end
puts total

n = gets.to_i
a = []
b = []
n.times do
  s = gets.split.map(&:to_i)
  a << s[0]
  b << s[1]
end
x = []
n.times do |i|
  n.times do |j|
    x << j if (i!=j and a[j]==b[i])
  end
end
puts n - x.uniq.size

n=gets.to_i
a=[]
b=[]
n.times{
    s,t=gets.split.map(&:to_i)
    a<<s
    b<<t
}

x=[]
n.times{|i|
    n.times{|j|
        x<<j if (i!=j and a[j]==b[i])
    }
}
p n-x.sort.uniq.size

def main()
	n=gets.chomp.to_i
	b1=[]
	b2=[]
	ans=0
	for i in 0...n
		ini=gets.chomp.split(" ")
		ini=ini.map{|i| i.to_i}
		ai=ini[0]
		bi=ini[1]
		b1 << ai
		b2 << bi

	end
	for i in 0...(b1.size)
		ind=b2.index(b1[i])
		if ind.nil?
			ans+=1
		elsif ind==i
			ind2=b2.rindex(b1[i])
			if ind2.nil? || ind2==i
				ans+=1
			end
		end
	end
	puts ans
end

main

array = Array.new(gets.to_i){ gets.split }
p array.select.with_index{ |(i,j),k|
    (array[0...k]+array[k+1..-1]).none?{ |l,m| m==i }
}.size

require 'set'

def run
  n    = $stdin.gets.to_i
  a, b = $stdin.readlines.map {|l| l.split.map(&:to_i)}.transpose

  r = []
  n.times do |i|
    n.times do |j|
      r[j] = true if i != j and b[i] == a[j]
    end
  end

  puts n - r.count(true)
end

run if $0 == __FILE__

require 'set'

def run
  a = []
  b = []
  n = $stdin.gets.to_i
  $stdin.readlines.each do |line|
    ai, bi = line.split.map(&:to_i)
    a << ai
    b << bi
  end

  r = []
  n.times do |i|
    n.times do |j|
      r[j] = true if i != j and b[i] == a[j]
    end
  end

  puts n - r.count(true)
end

run if $0 == __FILE__

n = gets.to_i
a = []
b = []
n.times do
  s = gets.split.map(&:to_i)
  a << s[0]
  b << s[1] if s[0]!= s[1]
end
x = []
n.times do |i|
  n.times do |j|
    x << j unless i==j || a[i]==b[i]
  end
end
puts n - x.uniq.size

n = gets.to_i
a = []
b = []
n.times do
  s = gets.split.map(&:to_i)
  a << s[0]
  b << s[1]
end
x = []
n.times do |i|
  n.times do |j|
    x << j if (i!=j and a[i]!=b[i])
  end
end
puts n - x.sort.uniq.size

n = gets.to_i
a = []
b = []
n.times do
  s = gets.split.map(&:to_i)
  a << s[0]
  b << s[1] if s[0]!= s[1]
end
x = []
n.times do |i|
  n.times do |j|
    x << j unless i==j or a[i]==b[i]
  end
end
puts n - x.uniq.size

n = gets.to_i
a = []
b = []
n.times do
  s = gets.split.map(&:to_i)
  a << s[0]
  b << s[1] if s[0]!= s[1]
end
x = []
n.times do |i|
  n.times do |j|
    x << j if i!=j || a[i]!=b[i]
  end
end
puts n - x.uniq.size

n=gets.to_i
a=[]
x=[]
n.times{
    s,t=gets.split.map(&:to_i)
    a<<t if s != t
    x<<s
    x<<t
}
a.sort!.uniq!
x.sort!.uniq!
puts x.size-a.size

n=gets.to_i
a=[]
x=[]
n.times{
    s,t=gets.split.map(&:to_i)
    a<<t if s != t
    x<<s
    x<<t
}
a.sort.uniq
x.sort.uniq
puts x.size-a.size

n=gets.to_i
a=[]
n.times{
    s,t=gets.split.map(&:to_i)
    a<<t if s != t
}
a.sort.uniq
puts n-a.size

n=gets.to_i
a=[]
n.times{
    s,t=gets.split.map(&:to_i)
    a<<t if s != t
}
a.sort.uniq
puts a.size

def main()
	n=gets.chomp.to_i
	b1=[]
	b2=[]
	ans=0
	for i in 0...n
		ini=gets.chomp.split(" ")
		ini=ini.map{|i| i.to_i}
		ai=ini[0]
		bi=ini[1]
		b1 << ai
		b2 << bi

	end
	for i in 0...(b1.size)
		ind=b2.index(b1[i])
		if ind.nil? || ind==i
			ans+=1
		end
	end
	puts ans
end

main

def main()
	n=gets.chomp.to_i
	ans=0
	for i in 0...n
		ini=gets.chomp.split(" ")
		ini=ini.map{|i| i.to_i}
		ai=ini[0]
		bi=ini[1]
		if ai==bi
			ans+=1
		end
	end
	puts ans
end
main

def main()
	n=gets.chomp.to_i
	b1=[]
	b2=[]
	check=[]
	ans=0
	for i in 0...n
		ini=gets.chomp.split(" ")
		ini=ini.map{|i| i.to_i}
		ai=ini[0]
		bi=ini[1]
		if ai==bi
			ans+=1
			check << bi
		else
			b1 << ai
			b2 << bi
		end
	end
	for i in check
		bx=i;
		ind=b2.index(bx)
		if !ind.nil?
			ans-=1
		end
	end
	puts (b2-b1).size+ans
end
main

def main()
	n=gets.chomp.to_i
	b1=[]
	b2=[]
	check=[]
	ans=0
	for i in 0...n
		ini=gets.chomp.split(" ")
		ini=ini.map{|i| i.to_i}
		ai=ini[0]
		bi=ini[1]
		if ai==bi
			ans+=1
			check << bi
		else
			b1 << ai
			b2 << bi
		end
	end
	for i in check
		bx=i;
		ind=b2.index(bx)
		if !ind.nil?
			ans-=1
		end
	end
	b2=b2.uniq
	b1=b1.uniq
	if !(b2.empty? && b1.empty?)
		if b2.size > b1.size
			puts ans
		else
			if (b2-b1).empty?
				puts ans
			else
				puts ((b2-b1).size)+ans
			end
		end
	else
		puts ans
	end
end

main

def main()
	n=gets.chomp.to_i
	b1=[]
	b2=[]
	ans=0
	for i in 0...n
		ini=gets.chomp.split(" ")
		ini=ini.map{|i| i.to_i}
		ai=ini[0]
		bi=ini[1]
		if ai==bi
			ans+=1
		end
		b1 << ai
		b2 << bi
	end
	puts (b2-b1).size+ans
end
main

def main()
	n=gets.chomp.to_i
	ans=0
	hs={}
	for i in 0...n
		ini=gets.chomp.split(" ")
		ini=ini.map{|i| i.to_i}
		ai=ini[0]
		bi=ini[1]
		hs[ai]=bi;
	end
	keys=hs.keys
	for i in keys
		b=i;
		v=hs[b]
		if v==b
			ans+=1;
		elsif !hs.has_value?(v)
			ans+=1
		end
	end
	puts ans
end
main

def main()
	n=gets.chomp.to_i
	b1=[]
	b2=[]
	check=[]
	ans=0
	for i in 0...n
		ini=gets.chomp.split(" ")
		ini=ini.map{|i| i.to_i}
		ai=ini[0]
		bi=ini[1]
		if ai==bi
			ans+=1
			check << bi
		else
			b1 << ai
			b2 << bi
		end
	end
	for i in check
		bx=i;
		ind=b2.index(bx)
		if !ind.nil?
			ans-=1
		end
	end
	puts ans
end
main

def main()
	nm=gets.chomp.split(" ")
	n=nm[0].to_i
	m=nm[1].to_i
	ar=gets.chomp.split(" ")
	ar=ar.map{|i| i.to_i}
	inc=0
	for i in 0...m
		stop=gets.chomp
		if stop[0].chr=="3"
			inop=stop.split(" ")
			inop=inop.map{|i| i.to_i}
			puts ar[inop[1]-1]+inc
		elsif stop[0].chr=="2"
			inop=stop.split(" ")
			inop=inop.map{|i| i.to_i}
			inc+=inop[1]
		else
			inop=stop.split(" ")
			inop=inop.map{|i| i.to_i}
			ar[inop[1]-1]=inop[2]-inc
		end

	end
end
main

def main()
	n=gets.chomp.to_i
	b1=[]
	b2=[]
	check=[]
	ans=0
	for i in 0...n
		ini=gets.chomp.split(" ")
		ini=ini.map{|i| i.to_i}
		ai=ini[0]
		bi=ini[1]
		if ai==bi
			ans+=1
			check << bi
		else
			b1 << ai
			b2 << bi
		end
	end
	for i in check
		bx=i;
		ind=b2.index(bx)
		if !ind.nil?
			ans-=1
		end
	end
	b1=b1.uniq
	b2=b2.uniq
	puts (b1-b2).size+ans
end
main

n = gets.chomp.to_i
brand_to_count = {}
brand_to_can_open = {}
brand_to_same_count = {}
n.times do
  a, b = gets.chomp.split(" ").map(&:to_i)
  if a == b
    brand_to_same_count[a] = brand_to_same_count.fetch(a, 0) + 1
  else
    brand_to_can_open[b] = brand_to_can_open.fetch(b, 0) + 1
    brand_to_count[a] = brand_to_count.fetch(a, 0) + 1
  end
end
total_unopenable = 0
brand_to_same_count.each do |brand, count|
  if count == 1
    total_unopenable += 1
  end
end
brand_to_count.each do |brand, count|
  # get count
  # see how many are openable from brand_to_can_open
  openable = brand_to_can_open.fetch(brand, 0)
  unopenable = [count - openable, 0].max
  total_unopenable += unopenable
end
puts total_unopenable

array = Array.new(gets.to_i){ gets.split }
p (array.map(&:first) - array.select{ |i,j| i != j }.map(&:last)).size

require 'set'

def run
  a = []
  s = Set.new
  n = $stdin.gets.to_i
  $stdin.readlines.each do |line|
    ai, bi = line.split.map(&:to_i)
    a << ai
    s.add(bi) if ai != bi
  end

  cnt = 0
  a.each do |ai|
    cnt += 1 unless s.include?(ai)
  end
  puts cnt
end

run if $0 == __FILE__

require 'set'

def run
  s = Set.new
  n = $stdin.gets.to_i
  $stdin.readlines.each do |line|
    a, b = line.split.map(&:to_i)
    s.add(b) if a != b
  end

  puts n - s.size
end

run if $0 == __FILE__

require 'set'

def run
  a = []
  b = []
  n = $stdin.gets.to_i
  $stdin.readlines.each do |line|
    ai, bi = line.split.map(&:to_i)
    a << ai
    b << bi
  end

  r = []
  n.times do |i|
    n.times do |j|
      r[i] = true if i != j and b[i] == a[j]
    end
  end

  puts n - r.count(true)
end

run if $0 == __FILE__

n = gets.to_i
a = []
b = []
n.times do
  s = gets.split.map(&:to_i)
  a << s[0]
  b << s[1] if s[0]!= s[1]
end
puts n - a.count{|i| b.include?(i)}

n = gets.to_i
a = []
b = []
n.times do
  s = gets.split.map(&:to_i)
  a << s[0]
  b << s[1] if s[0]!= s[1]
end
x = []
n.times do |i|
  n.times do |j|
    x << j if i!=j && a[i]!=b[i]
  end
end
puts n - x.uniq.size

n = gets.to_i
a = []
b = []
n.times do
  s = gets.split.map(&:to_i)
  a << s[0]
  b << s[1]
end
x = []
n.times do |i|
  n.times do |j|
    x << j if (i!=j and a[i]!=b[i])
  end
end
puts n - x.uniq.size


t = gets.to_i
alphabet = " abcdefghijklmnopqrstuvwxyz"
f=->s{s.chars.sum{alphabet.index(_1)}}
t.times do |cas|
  s = gets.chomp
  if s.size%2==0
    puts "Alice %d"%[f[s]]
  else
    a = [f[s[1..]], f[s[..-2]]].max
    b = f[s] - a
    if a > b
      puts "Alice %d"%(a-b)
    else
      puts "Bob %d"%(b-a)
    end
  end
end

# (a+b+c)
# (bc + ac + ab) + ad + bd + cd = 0
# d(a+b+c) + e(a+b+c) = -(bc + ac + ab)
# d + e = -(bc + ac + ab + de) /(a+b+c)

gets.to_i.times do
  a = gets.chomp.split('').map{|c| c.ord - 'a'.ord + 1}
  s = a.inject(:+)
  if a.size % 2 == 0 then
    alice = s
    bob = 0
  else
    if a.size == 1 then
      bob = s
      alice = 0
    else
      bob = [a[0],a[a.size-1]].min
      alice = s - bob
    end
  end
  if alice > bob then
    puts "Alice #{alice-bob}"
  else
    puts "Bob #{bob-alice}"
  end
end

gets.to_i.times do
  s = gets.chomp
  chars = s.chars.map { |ch| ch.ord - 97 + 1 }
  alice, bob = if s.size.even?
      [chars.sum, 0]
    else
      min = [chars[0], chars[-1]].min
      [chars.sum - min, min]
    end
  puts "#{alice > bob ? "Alice" : "Bob"} #{(alice - bob).abs}"
end

gets.to_i.times do
  a = gets.chomp.split('').map{|c| c.ord - 'a'.ord + 1}
  s = a.inject(:+)
  puts s
  if a.size % 2 == 0 then
    puts "Alice #{s}"
  else
    if a.size == 1 then
      puts "Bob #{s}"
    else
      puts "Alice #{s - 2*[a[0],a[a.size-1]].min}"
    end
  end
end


def combi2(m)
  return m * (m-1) / 2
end

n = gets.to_i
students = {}
for i in 0...n do
  initial = gets.chomp[0]
  if students.include?(initial)
    students[initial] += 1
  else
    students[initial] = 1
  end
end
sum = 0
students.each_value { |num|
  sum += combi2((num + 1) / 2) + combi2(num / 2)
}

puts sum

def combinations(n)
  Math.gamma(n + 1) / (Math.gamma(3) * Math.gamma((n - 2) + 1))
end

students_count = gets.chomp
students = []
groups = {}

(1..students_count.to_i).each do |_n|
  name = gets.chomp
  students << name
  groups[name[0]] = groups[name[0]] ? groups[name[0]] + 1 : 1
end

min = 0
groups.each do |_char, count|
  next if count <= 2
  min += count.even? ? combinations(count / 2) * 2 : combinations(count / 2) + combinations((count / 2) + 1)
end

print min.to_i

def sum(m)
	s = 0
	for i,k in m
		temp = k / 2
		s += temp * (temp-1) / 2
		s += (k-temp) * (k-temp-1) /2
	end
	return s
end

n = gets.to_i
m = Hash.new
m.default = 0
n.times do
	t = gets
	m[t[0]] += 1
end
puts(sum(m))

n = gets.to_i
cnt = Hash.new(0)
n.times do
  s = gets.chomp
  cnt[s[0]] += 1
end
ans = 0
cnt.each do |k,v|
  x = v/2 + v%2
  ans += x * (x-1) / 2
  ans += (v-x) * (v-x-1) /2
end
p ans

n = gets.to_i
a = Hash.new(0)
n.times do
    a[gets[0]] += 1
end
ans = 0
a.values.each do |i|
    ans += i/2*(i/2-1)/2+(i-i/2)*(i-i/2-1)/2
end
puts ans

def combinations(n)
  Math.gamma(n + 1) / (Math.gamma(3) * Math.gamma((n - 2) + 1))
end

students_count = gets.chomp
groups = {}

(1..students_count.to_i).each do |_n|
  name = gets.chomp
  groups[name[0]] = groups[name[0]] ? groups[name[0]] + 1 : 1
end

min = 0
groups.each_value do |count|
  next if count <= 2
  min += count.even? ? combinations(count / 2) * 2 : combinations(count / 2) + combinations((count / 2) + 1)
end

print min.to_i

def combi2(m)
  return m * (m-1) / 2
end

n = gets.to_i
students = {}
for i in 0...n do
  initial = gets.chomp[0]
  if students.include?(initial)
    students[initial] += 1
  else
    students[initial] = 1
  end
end
max = 0
students.each_value { |num|
  max = num if max < num
}

puts combi2((max + 1) / 2) + combi2(max / 2)


pa=Array.new(105,0)
pb=Array.new(105,0)
pc=Array.new

str=gets.to_s
pc=str.split
n=pc[0].to_i
m=pc[1].to_i

k=n*m
str=gets.to_s
pc=str.split
b=pc[0].to_i
for i in 1..b
  pa[pc[i].to_i]=1
end
str=gets.to_s
pc=str.split
b=pc[0].to_i
for i in 1..b
  pb[pc[i].to_i]=1
end

for i in 0..k*2
  a=i%n
  b=i%m
  if pa[a]==1 || pb[b]==1
    pa[a]=pb[b]=1
  end
end

#for i in 0..n-1
#  puts pa[i]
#end
#puts
#for i in 0..m-1
#  puts pb[i]
#end

h=0
for i in 0..n-1
  if pa[i]==0
    h=1
    break
  end
end

for i in 0..m-1
  if pb[i]==0 || h==1
    h=1
    break
  end
end

if h==1
  puts "No"
elsif h==0
  puts "Yes"
end

#!/usr/bin/env ruby

n, m = gets.split.map(&:to_i)
_, *xs = gets.split.map(&:to_i)
_, *ys = gets.split.map(&:to_i)

x_happy = Array::new(n, false)
y_happy = Array::new(m, false)

xs.each { |i| x_happy[i] = true }
ys.each { |i| y_happy[i] = true }

(n*m*2).times do |i|
  if x_happy[i%n] || y_happy[i%m]
    x_happy[i%n] = y_happy[i%m] = true
  end
end

if x_happy.all? && y_happy.all?
  puts "Yes"
else
  puts "No"
end

def g;gets.split.map(&:to_i)end
UF=Hash.new{|_,k|k}
def u a,b;UF[f a]=UF[f b]end
def f c;c==UF[c]?c:UF[c]=f(UF[c])end
n,m=g
b,*x=g
c,*y=g
happy = x+y.map{|i|i+n}
if happy==[]
  puts "No"
else
  if happy.size>1
    happy[1..-1].each{|i|u happy[0],i}
  end
  10000.times{|i|u i%n,n+i%m}
  puts (0..n+m-1).map{|i|f i}.uniq.size==1?"Yes":"No"
end

#!/usr/bin/ruby

#read data
params = gets.split(' ').map(&:to_i)
n = params[0].to_i
m = params[1].to_i

params = gets.split(' ').map(&:to_i)
b = params.shift
happy_b = params

params = gets.split(' ').map(&:to_i)
g = params.shift
happy_g = params

#puts happy_b.to_s
#puts happy_g.to_s

boys = Array.new(size=n, default=0)
girls = Array.new(size=m, default=0)

happy_b.each do |e|
    boys[e] = 1;
end

happy_g.each do |e|
    girls[e] = 1;
end

#puts boys.to_s
#puts girls.to_s

res = false;
times_to_run  = n.lcm m #mua ha ha
counter = 0;
#puts times_to_run
while (counter < times_to_run && !res) do
    if (boys[counter % n] == 1 || girls[counter % m] == 1)
        if (boys[counter % n] == 1 && girls[counter % m] == 0)
            girls[counter % m] = 1;
            g = g + 1;
        elsif (boys[counter % n] == 0 && girls[counter % m] == 1)
            boys[counter % n] = 1;
            b = b + 1;
        end
    end

 #   puts 'Iteration ' + counter.to_s
 #   puts boys.to_s
 #   puts girls.to_s
 #   puts b,n,g,m
    counter = counter + 1
    if (b == n || g == m)
        res = true;
        break
    end
end

if res
    puts 'Yes'
else
    puts 'No'
end

n, m = gets.split.map(&:to_i)
lbs = gets.split.map(&:to_i).drop(1)
lgs = gets.split.map(&:to_i).drop(1)
ul = n + m - (lbs.count + lgs.count)
bs = Array.new(n, false)
lbs.each {|i| bs[i] = true}
gs = Array.new(m, false)
lgs.each {|i| gs[i] = true}
lasti = 0
for i in 0..Float::INFINITY do
  bi, gi = i%n, i%m
  if bs[bi] || gs[gi]
    ul -= 1 unless bs[bi]
    ul -= 1 unless gs[gi]
    lasti = i unless bs[bi] && gs[gi]
    bs[bi] = gs[gi] = true
  end
  break if ul == 0
  break if i - lasti > m*n
end
puts ul == 0 ? "Yes" : "No"

def create_list(n, hippies)
    result = []
    n.times do |i|
        result << hippies.include?(i)
    end
    result
end

nm = gets.chomp.split(' ').map{|i| i.to_i }
n = nm[0]
m = nm[1]
happy_ar = gets.chomp.split(' ').map{|i| i.to_i }
happy_amount = happy_ar[0]
happy_who_b = if happy_amount > 0 then happy_ar[1..-1] else [] end
boys = create_list(n, happy_who_b)

happy_ar = gets.chomp.split(' ').map{|i| i.to_i }
happy_amount = happy_ar[0]
happy_who_g = if happy_amount > 0 then happy_ar[1..-1] else [] end
girls = create_list(m, happy_who_g)

def check(n, m, boys, girls)
    # gcd = n.gcd(m)
    (n * m * 2).times do |i|
        if boys[i % boys.length] or girls[i % girls.length]
            boys[i % boys.length] = true
            girls[i % girls.length] = true
        end
    end
    return (boys.all? and girls.all?)
end

puts check(n, m, boys, girls) ? 'Yes' : 'No'

n, m = gets.split.map(&:to_i)
a =  Array.new(n).map{0}
b =  Array.new(m).map{0}
a1 = gets.split.map(&:to_i)
b1 = gets.split.map(&:to_i)
a1.each_with_index{|x, i|
if i != 0
  a[x] = 1
end
}
b1.each_with_index{|x, i|
  if i != 0
    b[x] = 1
  end
}
k1 = 0
k2 = 0
f1 = 0
f2 = 0
for i in 0..3000000
  it1 = i % n
  it2 = i % m
  #print(" it1 #{it1}    it2 #{it2} ")
  if(a[it1] == 1 || b[it2] == 1)
    if(a[it1] == 0)
      a[it1] = 1
      k1 += 1
    end
    if(b[it2] == 0)
      b[it2] = 1
      k2 += 1
    end
  end
    if(n - (a1[0] + k1) == 0)
   f1 = 1
  end
  if(m - (b1[0] + k2) == 0)
   f2 = 1
  end
  if f2 == 1 && f1 == 1
    break
  end
end
  if f2 == 1 && f1 == 1
    puts("Yes")
  else
    puts("No")
  end

n, m = gets.strip.split.map(&:to_i)
a = gets.strip.split.map(&:to_i)
b = gets.strip.split.map(&:to_i)

a.delete_at(0)
b.delete_at(0)

h1 = []
h2 = []

(0..(n+m)).each {|x|
  h1 << 0
  h2 << 0
}

a.each { |x| h1[x] = 1 }
b.each { |x| h2[x] = 1 }

(0..(n*m*2)).each {|x|
  if h1[x % n] == 1 || h2[x % m] == 1
    h1[x % n] = 1
    h2[x % m] = 1
  end
}

res = 0
(0..(n-1)).each {|x| res = 1 if h1[x] == 0}
(0..(m-1)).each {|x| res = 1 if h2[x] == 0}

if res == 1
  puts "No"
else
  puts "Yes"
end

#!/usr/bin/ruby
# coding: utf-8

a, b = gets.split.map &:to_i
c = a.gcd b
data = Array.new(c, nil)
x = gets.split.map &:to_i
x.shift
x.each {|_|
  data[_ % c] = true
}
x = gets.split.map &:to_i
x.shift
x.each {|_|
  data[_ % c] = true
}
puts data.all? ? "Yes" : "No"

#!/usr/bin/env ruby

n, m = gets.split.map(&:to_i)
xs = gets.split.map(&:to_i)
ys = gets.split.map(&:to_i)

x_happy = Array::new(n, false)
y_happy = Array::new(m, false)

xs[1, xs[0]].each do |x|
  x_happy[x] = true
end

ys[1, ys[0]].each do |y|
  y_happy[y] = true
end

g = n.gcd(m)

(n*m*2).times do |i|
  if x_happy[i%n] && y_happy[i%m]
    x_happy[i%n] = y_happy[i%m] = true
  end
end

if x_happy.all? && y_happy.all?
  puts "Yes"
else
  puts "No"
end

#!/usr/bin/env ruby

n, m = gets.split.map(&:to_i)
xs = gets.split.map(&:to_i)
ys = gets.split.map(&:to_i)

x_happy = Array::new(n, false)
y_happy = Array::new(m, false)

xs[1, xs[0]].each do |x|
  x_happy[x] = true
end

ys[1, ys[0]].each do |y|
  y_happy[y] = true
end

g = n.gcd(m)

[x_happy.length, y_happy.length].min.times {
  x_happy.length.times do |i|
    y_happy.length.times do |j|
      if (i - j).abs % g == 0 && (x_happy[i] || y_happy[j])
        x_happy[i] = y_happy[j] = true
      end
    end
  end
}

if x_happy.all? && y_happy.all?
  puts "Yes"
else
  puts "No"
end

#!/usr/bin/ruby

#read data
params = gets.split(' ').map(&:to_i)
n = params[0].to_i
m = params[1].to_i

params = gets.split(' ').map(&:to_i)
b = params.shift
happy_b = params

params = gets.split(' ').map(&:to_i)
g = params.shift
happy_g = params

boys = Array.new(size=n, default=0)
girls = Array.new(size=m, default=0)

happy_b.each do |e|
    boys[e] = 1;
end

happy_g.each do |e|
    girls[e] = 1;
end

#puts boys.to_s
#puts girls.to_s

res = false;
times_to_run  = n.lcm m #mua ha ha
counter = 0;
#puts times_to_run
while (counter < times_to_run && !res) do
    if (boys[counter % n] == 1 || girls[counter % m] == 1)
        if (boys[counter % n] == 1 && girls[counter % m] == 0)
            girls[counter % m] = 1;
            g = g + 1;
        elsif (boys[counter % n] == 0 && girls[counter % m] == 1)
            boys[counter % m] = 1;
            b = b + 1;
        end
    end

   # puts 'Iteration ' + counter.to_s
   # puts boys.to_s
   # puts girls.to_s
    counter = counter + 1
    if (b == n || g == m)
        res = true;
    end
end

if res
    puts 'Yes'
else
    puts 'No'
end

n, m = gets.split.map(&:to_i)
lbs = gets.split.map(&:to_i).drop(1)
lgs = gets.split.map(&:to_i).drop(1)
ul = n*m - (lbs.count + lgs.count)
bs = Array.new(n, false)
lbs.each {|i| bs[i] = true}
gs = Array.new(m, false)
lgs.each {|i| gs[i] = true}
for i in 0...n*m do
  bi, gi = i%n, i%m
  break if i > 0 && bi == 0 && gi == 0
  if bs[bi] || gs[gi]
    ul -= 1 unless bs[bi]
    ul -= 1 unless gs[gi]
    bs[bi] = gs[gi] = true
  end
  break if ul == 0
end
puts ul == 0 ? "Yes" : "No"

n, m = gets.split.map(&:to_i)
lbs = gets.split.map(&:to_i).drop(1)
lgs = gets.split.map(&:to_i).drop(1)
ul = n + m - (lbs.count + lgs.count)
bs = Array.new(n, false)
lbs.each {|i| bs[i] = true}
gs = Array.new(m, false)
lgs.each {|i| gs[i] = true}
for i in 0...n*m do
  bi, gi = i%n, i%m
  if bs[bi] || gs[gi]
    ul -= 1 unless bs[bi]
    ul -= 1 unless gs[gi]
    bs[bi] = gs[gi] = true
  end
  break if ul == 0
end
puts ul == 0 ? "Yes" : "No"

n, m = gets.split.map(&:to_i)
lbs = gets.split.map(&:to_i).drop(1)
lgs = gets.split.map(&:to_i).drop(1)
ul = n*m - (lbs.count + lgs.count)
bs = Array.new(n, false)
lbs.each {|i| bs[i] = true}
gs = Array.new(m, false)
lgs.each {|i| gs[i] = true}
for i in 0...n*m do
  bi, gi = i%n, i%m
  if bs[bi] || gs[gi]
    ul -= 1 unless bs[bi]
    ul -= 1 unless gs[gi]
    bs[bi] = gs[gi] = true
  end
  break if ul == 0
end
puts ul == 0 ? "Yes" : "No"

n, m = gets.split.map(&:to_i)
lbs = gets.split.map(&:to_i).drop(1)
lgs = gets.split.map(&:to_i).drop(1)
ul = n + m - (lbs.count + lgs.count)
bs = Array.new(n, false)
lbs.each {|i| bs[i] = true}
gs = Array.new(m, false)
lgs.each {|i| gs[i] = true}
for i in 0...n*m do
  bi, gi = i%n, i%m
  break if i > 0 && bi == 0 && gi == 0
  if bs[bi] || gs[gi]
    ul -= 1 unless bs[bi]
    ul -= 1 unless gs[gi]
    bs[bi] = gs[gi] = true
  end
  break if ul == 0
end
puts ul == 0 ? "Yes" : "No"

def create_list(n, hippies)
    result = []
    n.times do |i|
        result << hippies.include?(i)
    end
    result
end

nm = gets.chomp.split(' ').map{|i| i.to_i }
n = nm[0]
m = nm[1]
happy_ar = gets.chomp.split(' ').map{|i| i.to_i }
happy_amount = happy_ar[0]
happy_who_b = if happy_amount > 0 then happy_ar[1..-1] else [] end
boys = create_list(n, happy_who_b)

happy_ar = gets.chomp.split(' ').map{|i| i.to_i }
happy_amount = happy_ar[0]
happy_who_g = if happy_amount > 0 then happy_ar[1..-1] else [] end
girls = create_list(m, happy_who_g)

def check(n, m, boys, girls, hboys, hgirls)
    # special case: if Drazil have the same number of girls and boys then
    # true would be only if in pairs at least one of them is happy
    if n == m
        n.times do |i|
            return false if not boys[i] and not girls[i]
        end
        return true
    end
    if (n-m).abs % 2 == 1
        return true
    end
    even = hboys.any?{|x| x % 2 == 0} or hgirls.any?{|x| x % 2 == 0}
    odd = hboys.any?{|x| x % 2 == 1} or hgirls.any?{|x| x % 2 == 1}
    return (even and odd)
end

puts check(n, m, boys, girls, happy_who_b, happy_who_g) ? 'Yes' : 'No'

def create_list(n, hippies)
    result = []
    n.times do |i|
        result << hippies.include?(i)
    end
    result
end

nm = gets.chomp.split(' ').map{|i| i.to_i }
n = nm[0]
m = nm[1]
happy_ar = gets.chomp.split(' ').map{|i| i.to_i }
happy_amount = happy_ar[0]
happy_who_b = if happy_amount > 0 then happy_ar[1..-1] else [] end
boys = create_list(n, happy_who_b)

happy_ar = gets.chomp.split(' ').map{|i| i.to_i }
happy_amount = happy_ar[0]
happy_who_g = if happy_amount > 0 then happy_ar[1..-1] else [] end
girls = create_list(m, happy_who_g)

def check(n, m, boys, girls)
    gcd = n.gcd(m)
    ((boys.length * girls.length) / gcd).times do |i|
        if boys[i % boys.length] or girls[i % girls.length]
            boys[i % boys.length] = true
            girls[i % girls.length] = true
        end
    end
    return (boys.all?{|x| x} and girls.all?{|x| x})
end

puts check(n, m, boys, girls) ? 'Yes' : 'No'

def create_list(n, hippies)
    result = []
    n.times do |i|
        result << hippies.include?(i)
    end
    result
end

nm = gets.chomp.split(' ').map{|i| i.to_i }
n = nm[0]
m = nm[1]
happy_ar = gets.chomp.split(' ').map{|i| i.to_i }
happy_amount = happy_ar[0]
happy_who_b = if happy_amount > 0 then happy_ar[1..-1] else [] end
boys = create_list(n, happy_who_b)

happy_ar = gets.chomp.split(' ').map{|i| i.to_i }
happy_amount = happy_ar[0]
happy_who_g = if happy_amount > 0 then happy_ar[1..-1] else [] end
girls = create_list(m, happy_who_g)

def check(n, m, boys, girls)
    gcd = n.gcd(m)
    if (n - m).abs % gcd != 0
        return true
    end
    ((boys.length * girls.length) / gcd).times do |i|
        if boys[i % boys.length] or girls[i % girls.length]
            boys[i % boys.length] = true
            girls[i % girls.length] = true
        end
    end
    return (boys.all?{|x| x} and girls.all?{|x| x})
end

puts check(n, m, boys, girls) ? 'Yes' : 'No'

def create_list(n, hippies)
    result = []
    n.times do |i|
        result << hippies.include?(i)
    end
    result
end

nm = gets.chomp.split(' ').map{|i| i.to_i }
n = nm[0]
m = nm[1]
happy_ar = gets.chomp.split(' ').map{|i| i.to_i }
happy_amount = happy_ar[0]
happy_who_b = if happy_amount > 0 then happy_ar[1..-1] else [] end
boys = create_list(n, happy_who_b)

happy_ar = gets.chomp.split(' ').map{|i| i.to_i }
happy_amount = happy_ar[0]
happy_who_g = if happy_amount > 0 then happy_ar[1..-1] else [] end
girls = create_list(m, happy_who_g)

def check(n, m, boys, girls)
    # gcd = n.gcd(m)
    (boys.length * girls.length).times do |i|
        if boys[i % boys.length] or girls[i % girls.length]
            boys[i % boys.length] = true
            girls[i % girls.length] = true
        end
    end
    return (boys.all?{|x| x} and girls.all?{|x| x})
end

puts check(n, m, boys, girls) ? 'Yes' : 'No'

pa=Array.new(105,0)
pb=Array.new(105,0)
pc=Array.new

str=gets.to_s
pc=str.split
n=pc[0].to_i
m=pc[1].to_i

k=n*m
str=gets.to_s
pc=str.split
b=pc[0].to_i
for i in 1..b
  pa[pc[i].to_i]=1
end
str=gets.to_s
pc=str.split
b=pc[0].to_i
for i in 1..b
  pb[pc[i].to_i]=1
end

for i in 0..k
  a=i%n
  b=i%m
  if pa[a]==1 || pb[b]==1
    pa[a]=pb[b]=1
  end
end

h=0
for i in 0..n-1
  if pa[i]==0
    h=1
    break
  end
end

for i in 0..m-1
  if pb[i]==0 || h==1
    h=1
    break
  end
end

if h==1
  puts "No"
elsif h==0
  puts "Yes"
end

pa=Array.new(105,0)
pb=Array.new(105,0)
pc=Array.new

str=gets.to_s
pc=str.split
n=pc[0].to_i
m=pc[1].to_i

k=n*m
str=gets.to_s
pc=str.split
b=pc[0].to_i
for i in 1..b
  pa[pc[i].to_i]=1
end
str=gets.to_s
pc=str.split
b=pc[0].to_i
for i in 1..b
  pb[pc[i].to_i]=1
end

for i in 0..n-1
  puts pa[i]
end
for i in 0..m-1
  puts pb[i]
end

for i in 0..k
  a=i%n
  b=i%m
  if pa[a]==1 || pb[b]==1
    pa[a]=pb[b]=1
  end
end

h=0
for i in 0..n-1
  if pa[i]==0
    h=1
    break
  end
end

for i in 0..m-1
  if pb[i]==0 || h==1
    h=1
    break
  end
end

if h==1
  puts "No"
elsif h==0
  puts "Yes"
end

n, m = gets.strip.split.map(&:to_i)
a = gets.strip.split.map(&:to_i)
b = gets.strip.split.map(&:to_i)

a.delete_at(0)
b.delete_at(0)

h1 = []
h2 = []

(0..(n+m)).each {|x|
  h1 << 0
  h2 << 0
}

a.each { |x| h1[x] = 1 }
b.each { |x| h2[x] = 1 }

(0..(n*m)).each {|x|
  if h1[x % n] == 1 || h2[x % m] == 1
    h1[x % n] = 1
    h2[x % m] = 1
  end
}

res = 0
(0..(n-1)).each {|x| res = 1 if h1[x] == 0}
(0..(m-1)).each {|x| res = 1 if h2[x] == 0}

if res == 1
  puts "No"
else
  puts "Yes"
end


def run(input = STDIN.read)
  periods = input.split("\n")[1..-1].map { |row| row.split.map(&:to_i) }

  periods.sort_by!(&:first)

  current_max = periods.first.last
  res = 0
  periods[1..-1].each do |period|
    res += 1 if period.last < current_max
    current_max = period.last if period.last > current_max
  end

  puts res
end

run

#f = File.open("c.in", "r")
class C <
	Struct.new(:ai, :bi)
end

n = gets.to_i
a = Array.new

for i in 0...n
	l = gets.split
	c = C.new
	c.ai = l[0].to_i
	c.bi = l[1].to_i
	a.push(c)
end

a = a.sort {|o, p| o[:ai] <=> p[:ai]}
m = r = 0

for i in 0...n
	m = [m, a[i].bi].max
	if a[i].bi < m
		r += 1
	end
end

puts r

require "set"
n = gets.chomp.to_i
events = []
events2 = []
n.times do
  e = gets.chomp.split(" ").map {|e| e.to_i}
  events.push e
end

total = 0
events = events.sort_by {|e| e[0]}
largest = 0

events.each do |e|
  if largest > e[1]
    total += 1
  end
  if e[1] > largest
    largest = e[1]
  end
end

puts total

n = gets.to_i
arr = []
n.times do
	arr << gets.split(" ").map(&:to_i)
end

arr.sort!#_by!{|x| [x[0],-x[1]]}

count = 0
max  = -1
arr.map(&:last).each do |a|

	if a < max
		count += 1
	else
		max = a
	end
end
puts count

def history
  coord = []
  STDIN.gets.to_i.times { coord << gets.split(' ').map{|i| i.to_i} }
  coord.sort!{|x, y| y[1] <=> x[1] }

  if coord.size < 2
    puts 0
    return
  end

  tot = 0
  min = coord[0][0]
  (1...coord.size).each do |ind|
    c = coord[ind]
    if c[0] > min
      tot += 1
    else
      min = c[0]
    end
  end
  p tot
end

history

def history
  coord = []
  STDIN.gets.to_i.times { coord << gets.split(' ').map{|i| i.to_i} }
  coord.sort!{|x, y| y[1] <=> x[1] }

  if coord.size < 2
    puts 0
    return
  end

  ind = 1
  max = coord[0][1]
  ind += 1 while coord[ind][1] >= max
  puts coord.size-ind
end

history

n = gets.to_i
arr = []
n.times do
	arr << gets.split(" ").map(&:to_i)
end

arr.sort_by!{|x| [x[0],-x[1]]}

range_arr = []
count = 0
arr.each do |a|
	found = false
	if range_arr.any?{ |b|  a[0] > b[0] && a[1] < b[1] }
		found = true
		count += 1
		break
	end
	range_arr << a unless found
end
puts count


gets
$<.map do
  x,y=_1.split.map &:to_i
  if (x+y).odd?
    puts "-1 -1"
  elsif x.even?
    puts [x/2,y/2]*" "
  else
    puts (x>y ? [x/2+1,y/2]*" " : [x/2,y/2+1]*" ")
  end
end

main = -> {
  t = int
  t.times do
    x, y = ints
    if (x + y).odd?
      puts "-1 -1"
    else
      a, b = x.div_ceil(2), y / 2
      puts "#{a} #{b}"
      # STDERR.puts "#{a+b} == #{(x - a).abs + (y - b).abs}"
    end
  end
}

DEBUG = true
MOD = 10**9+7
YESNO = %w(No Yes)
INF = 10**9

require "set"
require "prime"

def int; gets.to_s.to_i end
def ints; gets.to_s.split.map { |s| s.to_i } end
def int1s; gets.to_s.split.map { |s| s.to_i - 1 } end
def float; gets.to_s.to_f end
def floats; gets.to_s.split.map { |s| s.to_f } end
def array_of(&convert); gets.to_s.split.map(&convert) end
def string; gets.to_s.chomp end
def rep(n, &b); Array.new(n, &b) end
def yes; puts YESNO[1] end
def no; puts YESNO[0] end
def yesno t; puts YESNO[t] end
def zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end
def max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end
def min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end
def minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end
def gcd(*xs); xs.inject(0, :gcd) end
def matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end
def debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end
def debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join("") } if DEBUG; grid end
def if_debug; yield if DEBUG end

module Boolean
  def coerce(other); [other, to_i] end
  def +@; to_i end
  def to_int; to_i end
  def *(other); to_i * other end
end

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

class Integer
  def div_ceil(y); (self + y - 1) / y end
  def mod_inv(mod = MOD); pow(mod-2, mod) end
  def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end
  def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end
  def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster
  def bitbrute(&block); (1<<self).times(&block) end
  def nCr(r); x = 1; (1..r).each { |i| x *= self + 1 - i; x /= i }; x; end
  def each_divisor; return Enumerator.new { |y| each_divisor { |k| y << k } } unless block_given?; k = 1; while k * k < self; if self % k == 0; yield k; yield self / k end; k += 1; end; yield k if k * k == self end
  def divisors; each_divisor.to_a end
end

class Range
  def end_open; exclude_end? ? self.end : self.end + 1 end
  def end_close; exclude_end? ? self.end - 1 : self.end end
  def upper_bound; ac, wa = self.begin, self.end_open; while wa - ac > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end
  def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end
end

class Array
  def power(&block); (0 ... 1 << size).each(&block) end
  def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end
  def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end
  def cumdiff(range); self[range.end_open] - self[range.begin]; end
end

module Enumerable
  def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end
  def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end
end

main[]

gets.to_i.times do
  x, y = gets.split.map(&:to_i)
  diff = (x - y).abs
  if diff.even?
    if x > y
      puts "#{diff / 2} #{y}"
    else
      puts "#{x} #{diff / 2}"
    end
  else
    puts "-1 -1"
  end
end

gets
$<.map do
  x,y=_1.split.map &:to_i
  if (x+y).odd?
    puts "-1 -1"
  elsif x.even?
    puts [x/2,y/2]*" "
  else
    x,y=y,x if x<y
    puts [x/2+1,y/2]*" "
  end
end

gets.to_i.times do
  x, y = gets.split.map(&:to_i)
  min, max = [x, y].min, [x, y].max
  diff = max - min
  if diff.even?
    puts "#{min} #{diff / 2}"
  else
    puts "-1 -1"
  end
end


puts 'Impossible' if
[*$<].map(&:chomp).permutation(6) do |a,b,c,d,e,f|
    if a[0]==d[0] && a[-1]==e[0] &&
      c[0]==e[-1] && c[-1]==f[-1] &&
      b[0]==d[-1] && b[-1]==f[0] &&
      b[a.size-1]==e[d.size-1] &&
      a.size+c.size-1==b.size &&
      d.size+f.size-1==e.size
        puts a.ljust(b.size,'.')
        d[1...-1].chars.zip(e[1...-1].chars).map do |i,j|
            s = '.'*b.size
            s[0] = i
            s[a.size-1] = j
            puts s
        end
        puts b
        f[1...-1].chars.zip(e[d.size...-1].chars).map do |i,j|
            s = '.'*b.size
            s[a.size-1] = j
            s[-1] = i
            puts s
        end
        puts c.rjust(b.size,'.')
        break
    end
end


n = gets.to_i
if n % 2 == 0
    puts -1
else
    a = []
    b = []
    c = []
    for i in 0...n
        a.push i
        b.push i
        c.push (i + i) % n
    end
    [a, b, c].each do |arr| puts arr.join(' ') end
end

n=gets.to_i
if n.odd?
    a=[*0..n-1]
    puts a*' '
    puts (b=a.rotate)*' '
    c=[]
    a.each_with_index {|v, i| c<<(v+b[i])%n }
    puts c*' '
else
    puts -1
end

n = gets.to_i

a = (0...n).to_a

if n%2==0
  puts -1
elsif n%3!=0
  b = a.map {|x| (2*x)%n}
  c = a.map {|x| (x+(2*x)%n)%n}
  puts a.join(" ")
  puts b.join(" ")
  puts c.join(" ")
else
  b = a.map {|x| x%n}
  c = a.map {|x| (x*2)%n}
  puts a.join(" ")
  puts b.join(" ")
  puts c.join(" ")
end

n = gets.to_i

a = (0...n).to_a
b = a.map {|x| (2*x)%n}
c = a.map {|x| (x+(2*x)%n)%n}

if n%2==0
  puts -1
else
  puts a.join(" ")
  puts b.join(" ")
  puts c.join(" ")
end


n=gets.to_i
deg,s,que,sum=[],[],[],0
(0..n-1).each do |i|
  deg[i],s[i]=gets.split.map(&:to_i)
  sum+=deg[i]
  if deg[i]==1
    que.push i
  end
end
puts sum/2
while que[-1]
  f=que[-1]
  que.pop
  if deg[f]==0
    next
  end
  print f,' ',s[f],"\n"
  deg[s[f]]-=1
  deg[f]-=1
  s[s[f]]^=f
  if deg[s[f]]==1
    que.push s[f]
  end
end

n,deg,s,que,sum=gets.to_i,[],[],[],0
(0...n).each do |i|
  deg[i],s[i]=gets.split.map &:to_i
  sum+=deg[i]
  que.push i if deg[i]==1
end
p sum/2
while que[0]
  f=que[-1]
  que.pop
  next if deg[f]==0
  puts [f,s[f]].join' '
  deg[s[f]]-=1
  s[s[f]]^=f
  que.push s[f] if deg[s[f]]==1
end

n=gets.to_i
deg,s,que,sum=[],[],[],0
(0..n-1).each do |i|
  deg[i],s[i]=gets.split.map &:to_i
  sum+=deg[i]
  if deg[i]==1
    que.push i
  end
end
p sum/2
while que[0]
  f=que[-1]
  que.pop
  if deg[f]==0
    next
  end
  puts [f,s[f]].join' '
  deg[s[f]]-=1
  s[s[f]]^=f
  if deg[s[f]]==1
    que.push s[f]
  end
end

n=gets.to_i
deg,s,que,sum=[],[],[],0
(0..n-1).each do |i|
  deg[i],s[i]=gets.split.map(&:to_i)
  sum+=deg[i]
  if deg[i]==1
    que.push i
  end
end
p sum/2
while que[-1]
  f=que[-1]
  que.pop
  if deg[f]==0
    next
  end
  print f,' ',s[f],' '
  deg[s[f]]-=1
  deg[f]-=1
  s[s[f]]^=f
  if deg[s[f]]==1
    que.push s[f]
  end
end

n=gets.to_i
deg,s,que,sum=[],[],[],0
(0..n-1).each{|i| deg[i],s[i]=gets.split.map(&:to_i)}
deg.each_with_index do |i,id|
  if(i==1)
    que.push id
  end
  sum+=i
end
puts sum/2
while que[-1]
  f=que[-1]
  que.pop
  if deg[f]==0
    next
  end
  print f,' ',s[f]
  puts
  deg[s[f]]-=1
  deg[f]-=1
  s[s[f]]^=f
  if deg[s[f]]==1
    que.push s[f]
  end
end


m,n=gets.chomp.split.map(&:to_i)
a=gets.chomp.split.map(&:to_i).sort
t,i=0,0
while n!=0
	t+=a[i] if a[i]<0
	n-=1
	i+=1
end
puts -t

n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort.find_all {|x| x < 0 }
puts (sc = a.first([a.size, m].min).inject(:+)).nil? ? 0 : -sc

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}.sort
ans=0
m.times{|i|
	ans-=x[i] if x[i]<=0
}
puts ans

#=================================
if ARGV.include?("ONLINE_JUDGE")
	$stdin = File.open(ARGV[0], "r")
	ARGV.delete("ONLINE_JUDGE")
end
#=================================

n,m=gets.split.map(&:to_i)
data=gets.split.map(&:to_i)

get=data.sort[0, m].select{|v| v<0}.inject(0){|sum, val| sum+=val}.abs
puts get

# coding: utf-8

# if __FILE__ == $0
# end

n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort
earned = 0
a[0...m].each { |i|
    earned += -i if i < 0
}
puts earned

n,m = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i).select{|a| a<0}.sort[0..(m-1)]
sum = arr.inject(0,:+)
puts -sum

m, n = gets.split.map!(&:to_i)
a = gets.split.map!(&:to_i).select{|e| e <= 0}.sort!.first(n).inject(0) {|res, elem| res + elem}
puts a.abs

n, m = gets.split.collect{|i| i.to_i}
a = gets.split.collect{|i| i.to_i}.select{|i| i<0}.sort
if a.size == 0
    puts 0
    exit
end
puts a[0,m].inject{|sum,i| sum+=i}.abs
'''
if m > a.size
    puts a.inject{|sum,i| sum+=i}.abs
else
    puts a[0,m].inject{|sum,i| sum+=i}.abs
end
'''

n, m = gets.chomp.split.map(&:to_i)
prices = gets.chomp.split.map(&:to_i)

min, sum = 0, 0

prices.sort[0..m - 1].each do |p|
  sum += p
  min = [min, sum].min
end

puts min.abs

n, m = gets.strip.split(" ").map{|s| s.to_i}
prices = gets.strip.split(" ").map {|s| - s.to_i}.reject {|j| j <= 0}.sort.reverse
print prices[0...m].reduce(0, :+)

n, m = gets.split.map{|s| s.to_i}
prices = gets.split.map {|s| s.to_i}.select {|j| j < 0}.sort
print -prices[0...m].reduce(0, :+)

n, m = gets.split(' ').map(&:to_i)
puts gets.split(' ').map(&:to_i).select { |x| x < 0 }.sort.first(m).inject(0, &:+).abs

ns,ms= gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i

sa = gets.chomp.split(/ /)
sa.map!{|x| x.to_i}

sum = 0

sa.sort!
for i in 0..m-1
	sum += sa[i] if sa[i] < 0
end

puts -sum

n,m=gets.split.map(&:to_i)
v=[]
gets.split.map{|i|
	i=i.to_i
	if i<0 then
		v << i
	end
}
v.sort!
ans=0
[m,v.size].min.times{|i|
	ans-=v[i]
}
p ans

a = gets.chop.split(/ /)
a.map! do |i|
i = i.to_i
end

b = gets.chop.split(/ /)
b.map! do |i|
i = i.to_i
end

$cnt=0
$tmp = Array.new
for i in 0..(b.size-1)
if b[i] < 0
$tmp[$cnt] = i
$cnt=$cnt + 1
end
end

$sum = 0
if $cnt <= a[1]
$tmp.each do |i|
$sum=$sum - b[i]
end
else
b.sort!
for i in 0..a[1]-1
$sum=$sum - b[i]
end
end

puts $sum

a = gets.chop.split(/ /)
a.map! do |i|
i = i.to_i
end

b = gets.chop.split(/ /)
b.map! do |i|
i = i.to_i
end

$cnt=0
$tmp = Array.new
for i in 0..(b.size-1)
if b[i] < 0
$tmp[$cnt] = i
$cnt=$cnt + 1
end
end

$sum = 0
if $cnt <= a[1]
$tmp.each do |i|
$sum=$sum - b[i]
end
else
b.sort!
for i in 0..a[1]
$sum=$sum - b[i]
end
end

puts $sum

n,m=gets.chomp.split.map(&:to_i)
a=gets.chomp.split.map(&:to_i).sort
t,i=0,0
while n!=0
	t+=a[i] if a[i]<0
	n-=1
	i+=1
end
puts -t

n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).find_all {|x| x < 0 }
puts (sc = a.first([a.size, m].min).inject(:+)).nil? ? 0 : -sc

n, m = gets.split.map(&:to_i)
j, score = 0, 0
a = gets.split.map(&:to_i).sort!
while j<=m and a[j] < 0
  score += a[j]
  j+=1
end
puts -score

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}.sort
ans=0
m.times{|i|
	ans-=x[i]
}
puts ans

#=================================
if ARGV.include?("ONLINE_JUDGE")
	$stdin = File.open(ARGV[0], "r")
	ARGV.delete("ONLINE_JUDGE")
end
#=================================

n,m=gets.split.map(&:to_i)
data=gets.split.map(&:to_i)

get=data.select{|v| v<0}.inject(0){|sum, val| sum+=val}.abs
puts get

# coding: utf-8

# if __FILE__ == $0
# end

n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort
puts -a[0...m].reduce(:+)

n,m = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i).select{|a| a<0}.sort
sum = arr.inject(0,:+)
puts -sum

n, m = gets.chomp.split.map(&:to_i)
puts gets.chomp.split.map(&:to_i).sort[0..m - 1].inject(0) { |sum, v| sum + v }.abs

n, m = gets.strip.split(" ").map{|s| s.to_i}
prices = gets.strip.split(" ").map {|s| - s.to_i}.reject {|j| j <= 0}.sort.reverse
m = prices.length if m > prices.length
print prices[0..m].reduce(0) {|s, a| s + a}


T = gets.to_i

T.times do
  n = gets.to_i

  if n % 2 == 0
    puts '1' * (n / 2)
  else
    puts '7' + '1' * ((n - 3) / 2)
  end
end

gets.strip.to_i.times do
  n = gets.strip.to_i
  if n % 2 == 0
    puts "1" * (n / 2)
  else
    puts "7" + ("1" * ((n - 3) / 2))
  end
end

T = gets.to_i

T.times do
  n = gets.to_i

  if n % 2 == 0
    puts '1' * (n / 2)
  else
    puts '1' * ((n - 3) / 2) + '7'
  end
end


n = gets.chomp.to_i
for _ in 0...n
  num = gets.chomp
  len = num.length()
  if num.to_i % 7 == 0
    puts num
  else
    for i in 1..9
      temp = (num[0, len-1] + i.to_s).to_i
      if temp % 7 == 0
        puts temp
        break
      end
    end
  end
end

gets.to_i.times do
  n = gets.to_i
  if n % 7 == 0
    puts n
  else
    m = n / 10
    (0..9).each { |d|
      if (m - 2 * d) % 7 == 0
        puts "#{m}#{d}"
        break
      end
    }
  end
end


gets.to_i.times do
  n,l,r=gets.split.map(&:to_i)
  a=gets.split.map(&:to_i).sort
  c=0
  i=0
  j=n-1
  k=n-1
  while i<n
    while j>i&&a[i]+a[j]>=l
      j-=1
    end
    while k>0&&a[i]+a[k]>r
      k-=1
    end
    c+=[k,i].max-[j,i].max
    i+=1
  end
  puts c
end

def l_bound(x, i, a)
  lo, hi = 0, i - 1
  while lo <= hi
    mi = (lo + hi) / 2
    if a[mi] < x
      lo = mi + 1
    elsif
      hi = mi - 1
    end
  end
  return lo
end

def u_bound(x, i, a)
  lo, hi = 0, i - 1
  while lo <= hi
    mi = (lo + hi) / 2
    if a[mi] <= x
      lo = mi + 1
    elsif
      hi = mi - 1
    end
  end
  return hi
end

for t in 0...gets.to_i
  n, l, r = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i).sort
  ans = 0
  (0...n).each do |i|
    lo = l_bound(l - a[i], i, a)
    hi = u_bound(r - a[i], i, a)
    if lo <= hi and lo >= 0 and hi < i
      ans += hi - lo + 1
    end
  end
  puts ans
end


require 'Matrix'

def modpowMatrix(x,n)
  ret = Matrix.I(x.column_size)
  while n > 0
    ret = (ret * x).map{|v| v % (MOD-1)} if n&1 == 1
    x = (x*x).map{|v| v % (MOD-1)}
    n >>= 1
  end
  return ret
end

def modpow(x,n)
  ret = 1
  while n > 0
    ret = (ret * x) % MOD if n&1 == 1
    x = (x*x) % MOD
    n >>= 1
  end
  return ret
end

MOD = 10**9+7
n,x,y,z,c = gets.split.map(&:to_i)
a = Matrix.rows([[1,1,1],[1,0,0],[0,1,0]])
b = Matrix.rows([[1,1,1,1,0],[1,0,0,0,0],[0,1,0,0,0],[0,0,0,1,1],[0,0,0,0,1]])

m = modpowMatrix(a,n-3)
ft = Matrix.columns([[0,0,0,2,2]])
k = (modpowMatrix(b,n-3)*ft)[0,0] % (MOD-1)
p ((modpow(c,k) * modpow(z,m[0,0]) % MOD) * modpow(y,m[0,1]) % MOD) * modpow(x,m[0,2]) % MOD

require 'Matrix'

def modpowMatrix(x,n)
  ret = Matrix.I(x.column_size)
  while n > 0
		ret = (ret * x).map{|v| v%MOD} if n&1 == 1
    x = (x*x).map{|v| v%MOD}
    n >>= 1
  end
  return ret
end

def modpow(x,n)
  ret = 1
  while n > 0
    ret = (ret * x) % MOD if n&1 == 1
    x = (x*x) % MOD
    n >>= 1
  end
  return ret
end

MOD = 10**9+7
n,x,y,z,c = gets.split.map(&:to_i)
a = Matrix.rows([[1,1,1],[1,0,0],[0,1,0]])
b = Matrix.rows([[1,1,1,1,0],[1,0,0,0,0],[0,1,0,0,0],[0,0,0,1,1],[0,0,0,0,1]])

m = modpowMatrix(a,n-3)
cf = Matrix.columns([[0,0,0,2,2]])
k = (modpowMatrix(b,n-3)*cf)[0,0] % MOD
p ((modpow(c,k) * modpow(z,m[0,0]) % MOD) * modpow(y,m[0,1]) % MOD) * modpow(x,m[0,2]) % MOD


n=gets.to_i
a=[0]+gets.split.map(&:to_i)
1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];"#{p k,i,j;exit}"if k}
p -1

n = gets.to_i

lens = gets.split.map{|i| i.to_i}

i = 0
k = true
while k && i < n-1 do
    j = 1
    while k && j < n do
        if (t = lens.index(lens[i] + lens[j])) then
            k = false
            result = (t+1).to_s + " " + (i+1).to_s + " " + (j+1).to_s
        end
        j += 1
    end
    i += 1
end

if k then
    puts "-1"
else
    puts result
end

@ok = false

def chk3(arr, i, j, k)
	if (arr[i] == arr[j]+arr[k])
		puts (i+1).to_s + " " + (j+1).to_s + " " + (k+1).to_s
		@ok = true
		return true
	end
	return false
end

n = gets.to_i
s = gets
na = s.split(/ /)
arr = Array.new(n)
for i in 0..n-1
  arr[i] = na[i].to_i
end

(n-1).downto(2) { |i|
	(i-1).downto(1) { |j|
		(j-1).downto(0) { |k|
			break if chk3(arr, i, j, k)
			break if chk3(arr, i, k, j)
			break if chk3(arr, j, i, k)
			break if chk3(arr, j, k, i)
			break if chk3(arr, k, i, j)
			break if chk3(arr, k, j, i)
		}
		break if @ok
	}
	break if @ok
}
puts("-1") if @ok == false

#!/usr/bin/ruby
gets;a=gets.split.map(&:to_i)
a.size.times{|i|a.size.times{|j|(j+1).step(a.size-1){|k|
	if a[i]==a[j]+a[k] then puts [i+1,j+1,k+1]*' ';exit end
}}}
p -1

a = gets.chop

b = gets.chop.split(/ /)
b.map! do |i|
i = i.to_i
end

c = Array.new
flag = 0
for i in 0..b.size-1
	for j in i+1..b.size-1
		for k in j+1..b.size-1
			if b[i] == b[j]+b[k]
				flag=1
				c[0]=i+1
				c[1]=j+1
				c[2]=k+1
			elsif b[j] == b[i]+b[k]
				flag=1
				c[0]=j+1
				c[1]=i+1
				c[2]=k+1
			elsif b[k] == b[i]+b[j]
				flag=1
				c[0]=k+1
				c[1]=i+1
				c[2]=j+1
			end
			if flag==1
				break
			end
		end
		if flag==1
			break
		end
	end
	if flag==1
		break
	end
end

if flag==0
	print -1
else
	for i in 0..c.size-1
		print c[i]
		if i !=c.size-1
			print " "
		end
	end
end

a = gets.chop

b = gets.chop.split(/ /)
b.map! do |i|
i = i.to_i
end

c = Array.new
flag = 0
for i in 0..b.size-1
	for j in i+1..b.size-1
		for k in j+1..b.size-1
			if b[i] == b[j]+b[k]
				flag=1
				c[0]=i+1
				c[1]=j+1
				c[2]=k+1
			elsif b[j] == b[i]+b[k]
				flag=1
				c[0]=j+1
				c[1]=i+1
				c[2]=k+1
			elsif b[k] == b[i]+b[j]
				flag=1
				c[0]=k+1
				c[1]=i+1
				c[2]=j+1
			end
			if flag==1
				break
			end
		end
		if flag==1
			break
		end
	end
	if flag==1
		break
	end
end

if flag==0
	print -1
else
	for i in 0..c.size-1
		print c[i].to_s + " "
	end
end

n=gets.to_i
a=gets.chomp.split.map(&:to_i)
for i in 0...n
	for j in 0...n
		for k in 0...n
			if i!=j && i!=k && j!=k && a[i]==a[j]+a[k]
				puts "#{i+1} #{j+1} #{k+1}"
				exit
			end
		end
	end
end
puts "-1"

n = gets.strip.to_i
a = gets.strip.split.map(&:to_i)
done = false
x = y = z = 0
n.times do |i|
    break if done
    ai = a[i]
    n.times do |j|
        next if i == j
        break if done
        aj = a[j]
        n.times do |k|
            next if j == k
            if aj + a[k] == ai
                x = i + 1
                y = k + 1
                z = j + 1
                done = true
                break
            end
        end
    end
end
if !done
    puts "-1"
else
    puts "#{x} #{y} #{z}"
end

n = gets.to_i
a = gets.split.map &:to_i

q = {}
a.each_index { |i| q[a[i]] = i }

n.times do |i|
    i.times do |j|
        if q[ a[i] + a[j] ] != nil
            puts "#{ q[a[i] + a[j]] + 1 } #{i + 1} #{j + 1}"
            exit
        end
    end
end

puts "-1"

def main()
	n=gets.chomp.to_i
	strinp=gets.chomp.split(" ")
	strinp=strinp.map{|i| i.to_i}
	for i in 0...n
		sum=strinp[i]
		f=false
		for j in 0...n
			rem=sum-strinp[j]
				for k in 0...n
					if strinp[k]==rem && k!=j && k!=i
						puts "%d %d %d" %[i+1,j+1,k+1]
						f=true
						break
					end
				end
			if f
				break
			end
		end
		if f
			break
		end
	end
	if !f
		puts "-1"
	end
end
main()

# def bin_search(arr,e)
# 	n = arr.length
# 	l = 0
# 	r = n - 1

# 	while l <= r
# 		m = l + (r-l) / 2
# 		if arr[m] == e
# 			puts "#{m}"
# 			return m
# 		elsif e < arr[m]
# 			r = m - 1
# 		else
# 			l = m + 1
# 		end
# 	end

# 	return false

# end

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
# arr.sort!
ans = false
ans_i = ans_j = ans_k = 0
arr.each_with_index do |i,ind_i|
	arr[ind_i+1..-1].each_with_index do |j,ind_j|
		# puts "#{i+j} ans #{bin_search(arr[ind_j+1..-1], i + j)}"
		ind_k = arr.index(i+j) #bin_search(arr[ind_j+1..-1], i + j)
		# puts "#{i} #{j} #{ind_k}"
		if ind_k
			ans = true
			ans_j = ind_i
			ans_k = ind_i + ind_j + 1
			ans_i = ind_k
			break
		end
	end
	break if ans
end

puts ans ? "#{ans_i+1} #{ans_j+1} #{ans_k+1}": "-1"

gets
mas = gets.split(" ")
for i in (0..mas.size - 1)
  mas[i] = mas[i].to_i
end

for i in (0..mas.size - 1)
  for j in (0..mas.size - 1)
    for k in (0..mas.size - 1)
      if (i != j && j != k && i != k && mas[i] == mas[j]+mas[k])
        print i+1, " ", j+1, " ", k+1
        exit
      end
    end
  end
end

print -1

gets
worms = gets.chomp.split.map(&:to_i)

res = [-1]
found = false
worms.each_with_index do |w1, i|
  break if found
  worms.each_with_index do |w2, j|
    break if found
    next if j == i
    worms.each_with_index do |w3, k|
      break if found
      next if k == j or k == i
      if w3 == w1 + w2
        res = [k.succ, j.succ, i.succ]
        found = true
      end
    end
  end
end
puts res.join(' ')

n = gets.to_i

lens = gets.split.map{|i| i.to_i}

i = 0
k = true
while k && i < n-1 do
    j = 1
    while k && j < n do
        if (t = lens.index(lens[i] + lens[j])) then
            k = false
            result = lens[t].to_s + " " + lens[i].to_s + " " + lens[j].to_s
        end
        j += 1
    end
    i += 1
end

if k then
    puts "-1"
else
    puts result
end

n = gets.to_i
s = gets
na = s.split(/ /)
arr = Array.new(n)
for i in 0..n-1
  arr[i] = na[i].to_i
end
arr.sort!
ok = false
(n-1).downto(2) { |i|
	(i-1).downto(1) { |j|
		(j-1).downto(0) { |k|
			if (arr[i] == arr[j]+arr[k])
				puts (i+1).to_s + " " + (j+1).to_s + " " + (k+1).to_s
				ok = true
				break
			end
		}
		break if ok
	}
	break if ok
}
puts("-1") if ok == false

n = gets.strip.to_i
a = gets.strip.split.map(&:to_i)
done = false
x = y = z = 0
n.times do |i|
    break if done
    ai = a[i]
    (1...n).each do |j|
        if a[j - 1] + a[j] == ai
            x = i + 1
            y = j + 1
            z = j
            done = true
            break
        end
    end
end
if !done
    puts "-1"
else
    puts "#{x} #{y} #{z}"
end

n = gets.strip.to_i
a = gets.strip.split.map(&:to_i)
done = false
x = y = z = 0
n.times do |i|
    break if done
    ai = a[i]
    n.times do |j|
        break if done
        aj = a[j]
        n.times do |k|
            if aj + a[k] == ai
                x = i + 1
                y = j + 1
                z = k + 1
                done = true
                break
            end
        end
    end
end
if !done
    puts "-1"
else
    puts "#{x} #{y} #{z}"
end

gets
a=gets.split.map &:to_i
a.combination(2).each{|i,j|"#{p i+j,i,j;exit}"if a.index i+j}
p -1

def main()
	n=gets.chomp.to_i
	strinp=gets.chomp.split(" ")
	strinp=strinp.map{|i| i.to_i}
	for i in 0...n
		sum=strinp[i]
		f=false
		for j in 0...n
			rem=sum-strinp[j]
			search=strinp-[sum,strinp[j]]
			if search.include?(rem)
				puts "%d %d %d" %[sum,strinp[j],rem]
				f=true
				break
			end
		end
		if f
			break
		end
	end
	if !f
		puts "-1"
	end
end
main()

# def bin_search(arr,e)
# 	n = arr.length
# 	l = 0
# 	r = n - 1

# 	while l <= r
# 		m = l + (r-l) / 2
# 		if arr[m] == e
# 			puts "#{m}"
# 			return m
# 		elsif e < arr[m]
# 			r = m - 1
# 		else
# 			l = m + 1
# 		end
# 	end

# 	return false

# end

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
# arr.sort!
ans = false
ans_i = ans_j = ans_k = 0
arr.each_with_index do |i,ind_i|
	arr[ind_i+1..-1].each_with_index do |j,ind_j|
		# puts "#{i+j} ans #{bin_search(arr[ind_j+1..-1], i + j)}"
		ind_k = arr[ind_j+1..-1].index(i+j) #bin_search(arr[ind_j+1..-1], i + j)
		# puts "#{i} #{j} #{ind_k}"
		if ind_k
			ans = true
			ans_j = ind_i
			ans_k = ind_i + ind_j + 1
			ans_i = ans_k + ind_k
			break
		end
	end
	break if ans
end

puts ans ? "#{ans_i+1} #{ans_j+1} #{ans_k+1}": "-1"

def bin_search(arr,e)
	n = arr.length
	l = 0
	r = n - 1

	while l <= r
		m = l + (r-l) / 2
		if arr[m] == e
			return true
		elsif e < arr[m]
			r = m - 1
		else
			l = m + 1
		end
	end

	return false

end

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
arr.sort!
ans = false
ans_i = ans_j = ans_k = 0
arr.each_with_index do |i,ind_i|
	arr[ind_i+1..-1].each_with_index do |j,ind_j|
		# puts "#{i+j} ans #{bin_search(arr[ind_j+1..-1], i + j)}"
		if bin_search(arr[ind_j+1..-1], i + j)
			ans = true
			ans_i = i + j
			ans_j = i
			ans_k = j
			break
		end
	end
	break if ans
end

puts ans ? "#{ans_i} #{ans_j} #{ans_k}": "-1"

gets
worms = gets.chomp.split.map(&:to_i)

res = [-1]
found = false
worms.each_with_index do |w1, i|
  break if found
  worms.each_with_index do |w2, j|
    break if found
    next if j == i
    worms.each_with_index do |w3, k|
      break if found
      next if k == j or k == i
      if w3 == w1 + w2
        res = [i.succ, j.succ, k.succ]
        found = true
      end
    end
  end
end
puts res.join(' ')


require 'set'

n = readline.to_i
f = [nil] + readline.split.map(&:to_i)

zeros = (1..n).select { |i| f[i] == 0 }.to_set
sources = (1..n).select { |i| f[i] != 0 and f[f[i]] == 0 }
sinks = (1..n).to_set.difference(f).select { |i| f[i] != 0 }

if sources.length > 1 then
  for source in sources[0..-2] do
    f[f[source]] = sinks.pop
    zeros.delete(f[source])
  end
end

zeros = zeros.to_a
zeros.each_cons(2) do |i,j|
  f[i] = j
end
f[zeros[-1]] = zeros[0]

if sources.length > 0 then
  if zeros.length > 1 then
    f[zeros[(zeros.find_index(f[sources[-1]]) - 1) % zeros.length]] = sinks.pop
  else
    f[f[sources[-1]]] = sinks.pop
  end
end

print(f[1..-1].join(' '))

gets.strip.to_i
f = gets.strip.split(' ').map(&:to_i)
no_rece = Array.new(f.size, -1)
no_give = []
f.each_with_index do |e, i|
  no_give << (i+1) if e.zero?
  no_rece[e - 1] = i unless e.zero?
end
no_rece = no_rece.each_with_index.map { |e, i| e == -1 ? (i + 1) : nil }.compact.sort
no_give = no_give.sort.reverse
# inter = no_rece & no_give
# puts no_rece.inspect
# puts no_give.inspect
no_give.each_with_index do |e, i|
  if no_rece[i] == e
    a = no_give[i]
    b = no_rece[i]
    c = i ==0 ? no_give[i +1] : no_give[i - 1]
    d = i ==0 ? no_rece[i +1] : no_rece[i - 1]
    # no_give[i] = c
    # no_give[i == 0 ? i + 1 : i - 1] = a
    no_rece[i] = d
    no_rece[i == 0 ? i + 1 : i - 1] = b
  end
end
# puts no_rece.inspect
# puts no_give.inspect
no_give.each_with_index do |e, i|
  f[e - 1] = no_rece[i]
end
puts f.join(" ")
# puts f.map { |e| e.zero? ?  }

MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
n=gi
f=gsmi
fixed=f.sort_by{|x|-x}.to_a
#puts f.to_s
#puts fixed.to_s

ar=(1..n).to_a
pos=[]
n.times do |i|
  if f[i]==0
    pos<<i+1
    next
  end
end
n.times do |i|
  break if fixed[i]==0
  ar.delete_at(fixed[i]-1)
end
#puts ar.to_s
lackpos = ar & pos
ar3 = ar - lackpos
# puts ar.to_s
# puts pos.to_s
# puts lackpos.to_s
# puts ar3.to_s
if lackpos.size==1
  f[lackpos[0]-1]=ar3[0]
  ar3[0]=lackpos[0]
elsif lackpos.size>0
  s=lackpos.size
  s.times do |i|
    f[lackpos[i]-1]=lackpos[(i+1)%s]
  end
end
#puts f.to_s

n.times do |i|
  next if f[i]!=0
  f[i]=ar3.shift

  break if ar.size==0
end

puts f.join(" ")

n=gets.to_i
f=gets.split.map(&:to_i)
fixed=f.sort_by{|x|-x}.to_a
#puts f.to_s
#puts fixed.to_s

ar=(1..n).to_a
n.times do |i|
    break if fixed[i]==0
    ar.delete_at(fixed[i]-1)
end
#puts ar.to_s
ar.push(ar.shift)
n.times do |i|
    next if f[i]!=0
    nn=ar.shift
    if nn==i+1
        f[i]=ar.shift
        ar<<nn
    else
      f[i]=nn
    end
    break if ar.size==0
end

puts f.join(" ")

n=gets.to_i
f=gets.split.map(&:to_i)
fixed=f.sort_by{|x|-x}.to_a
#puts f.to_s
#puts fixed.to_s

ar=(1..n).to_a
n.times do |i|
    break if fixed[i]==0
    ar.delete_at(fixed[i]-1)
end
#puts ar.to_s
ar.push(ar.shift) if ar[0]==f.index(0)
n.times do |i|
    next if f[i]!=0
    nn=ar.shift
    if nn==i+1
        f[i]=ar.shift
        ar<<nn
    else
      f[i]=nn
    end
    break if ar.size==0
end

puts f.join(" ")


arr = readline.strip.each_char.to_a
1.upto(arr.length - 1) do |ind|
  if arr[ind] == arr[ind - 1]
    arr[ind] = 'd'
    if arr[ind] == arr[ind - 1] ||
        (ind < arr.length - 1 && arr[ind] == arr[ind + 1])
      arr[ind] = 'q'
    end
    if arr[ind] == arr[ind - 1] ||
        (ind < arr.length - 1 && arr[ind] == arr[ind + 1])
      arr[ind] = 'c'
    end
  end
end
puts arr.join

def getNextChar(c)
  return 'a' if c == 'z'
  return c.next
end
string = gets.chomp
for i in 0..string.length - 2 do
  while string[i] == string[i + 1]
    if i < string.length - 2
      while string[i] == string[i + 1] or string[i+1] == string[i+2]
        string[i+1] = getNextChar(string[i+1])
      end
    else
      while string[i + 1] == string[i]
        string[i+1] = getNextChar(string[i+1])
      end
    end
  end
end
puts string


n = gets.chomp.to_i
n.times do |i|
    s = gets.chomp
    if s.to_i >= 31 && s != "36" && s != "40" && s != "44"
    puts "YES"
    print "6"
    print " "
    print "10"
    print " "
    print "14"
    print " "
    print s.to_i - 30
        print "\n"
    elsif s == "40" || s == "36" || s == "44"
        puts "YES"
        print "6"
        print " "
        print "10"
        print " "
        print "15"
        print " "
        print s.to_i - 31
            print "\n"
    else
        puts "NO"
    end
end

$, = " "
$\ = "\n"
gets.to_i.times do
    n = gets.to_i
    if n <= 30
        puts "NO"
    else
        puts "YES"
        if n == 36 || n == 40 || n == 44
            print 6, 10, 15, n - 31
        else
            print 6, 10, 14, n - 30
        end
    end
end

t=gets.chomp.to_i
rep=15
while t!=0
 ar=[6,10,14]
 sum=30
 num=gets.chomp.to_i
 ar.each_with_index do |x,i|
 if (num-sum == x)
  ar[ar.length-1]=rep
  sum+=1
  break
 end

 end
if(num-sum >= 1)
 puts "YES"
 ar.each { |x| print "#{x} "}
 print "#{num-sum}"
 puts
 else
     puts "NO"
end
t-=1
end

t= gets.chomp.to_i
ar=[6,10,14]
sum = 30
while t!=0
 num= gets.chomp.to_i
if(num-sum >= 1)
 puts "YES"
 ar.each { |x| print "#{x} "}
 print "#{num-sum}"
 else
     puts "NO"
end
t-=1
end

t=gets.chomp.to_i
ar=[6,10,14]
rep=15
sum = 30
while t!=0
 num=gets.chomp.to_i
 ar.each_with_index do |x,i|
 if (num-sum == x)
   puts"in if"
  ar[ar.length-1]=rep
  sum+=1
  break
 end

 end
 p "sum = #{sum}"
 ar.each {|x| puts x}
if(num-sum >= 1)
 puts "YES"
 ar.each { |x| print "#{x} "}
 print "#{num-sum}"
 puts
 else
     puts "NO"
end
t-=1
end

t= gets.chomp.to_i
ar=[6,10,14]
sum = 30
while t!=0
 num= gets.chomp.to_i
if(num-sum >= 1)
 puts "YES"
 print "#{num-sum} "
 ar.each { |x| print "#{x} "}
 else
     puts "NO"
end
t-=1
end

t=gets.chomp.to_i

rep=15
sum = 30
while t!=0
 ar=[6,10,14]
 num=gets.chomp.to_i
 ar.each_with_index do |x,i|
 if (num-sum == x)
  ar[ar.length-1]=rep
  sum+=1
  break
 end

 end
if(num-sum >= 1)
 puts "YES"
 ar.each { |x| print "#{x} "}
 print "#{num-sum}"
 puts
 else
     puts "NO"
end
t-=1
end

t=gets.chomp.to_i
ar=[6,10,14]
rep=15
sum = 30
while t!=0
 num=gets.chomp.to_i
 ar.each_with_index do |x,i|
 if (num-sum == x)
  ar[ar.length-1]=rep
  sum+=1
  break
 end

 end
if(num-sum >= 1)
 puts "YES"
 ar.each { |x| print "#{x} "}
 print "#{num-sum}"
 puts
 else
     puts "NO"
end
t-=1
end

t= gets.chomp.to_i
ar=[6,10,14]
sum = 30
while t!=0
 num= gets.chomp.to_i
if(num-sum >= 1)
 puts "YES"
 ar.each { |x| print "#{x} "}
 print "#{num-sum}"
 puts
 else
     puts "NO"
end
t-=1
end

n = gets.chomp.to_i
n.times do |i|
    s = gets.chomp.to_i
    if s >= 31
    puts "YES"
    print "6"
    print " "
    print "10"
    print " "
    print "14"
    print " "
    print s - 30
        print "\n"
    else
        puts "NO"
    end
end

n = gets.chomp.to_i
n.times do
    s = gets.chomp.to_i
    if n >= 31
    puts "YES"
    print "6"
    print " "
    print "10"
    print " "
    print "14"
    print s - 30
    else
        puts "NO"
    end
end

$, = ' '
gets.to_i.times do
    n = gets.to_i
    if n <= 30
        puts "NO"
    else
        puts "YES"
        if n == 36 || n == 40 || n == 44
            print 6, 10, 15, n - 31
        else
            print 6, 10, 14, n - 30
        end
    end
end

n = gets.to_i
ans = []
n.times do
  x = gets.to_i
  if x <= 30
    ans << [-1,-1,-1,-1]
  else
    ans << [6,10,14,x-30]
  end
end
ans.each do |x|
  if x[0] == -1
    puts "NO"
  else
    puts "YES"
    puts "#{x[0]} #{x[1]} #{x[2]} #{x[3]}"
  end
end


n = gets.strip.to_i
arr = []
n.times do
  arr << gets.strip.to_i
end
arr.sort!
sum = 0
for i in 0..((n-1)/2) do
  if i != n-1-i
    sum += arr[i] * arr[n-1-i] * 2
  else
    sum += arr[i] * arr[i]
  end
end
puts (sum % 10007)

n = gets.strip.to_i
tasks = Array.new(n, 0)
laziness = Array.new(n, 0)
n.times do |i|
    a = gets.strip.to_i
    tasks[i] = laziness[i] = a
end
tasks.sort!.reverse!
laziness.sort!
ans = 0
n.times do |i|
    ans += tasks[i] * laziness[i]
end
ans %= 10007
puts ans

n = gets.strip.to_i
tasks = Array.new(n, 0)
laziness = Array.new(n, 0)
n.times do |i|
    a = gets.strip.to_i
    tasks[i] = laziness[i] = a
end
tasks.sort!.reverse!
laziness.sort!
ans = 0
n.times do |i|
    ans += tasks[i] * laziness[i]
end
puts ans

n = gets.strip.to_i
arr = []
n.times do
  arr << gets.strip.to_i
end
arr.sort!
sum = 0
for i in 0..((n-1)/2) do
  if i != n-1-i
    sum += arr[i] * arr[n-1-i] * 2
  else
    sum += arr[i] * arr[i]
  end
end
puts sum


def main
  n = gets.chomp.to_i
  i = n * n
  ints = []
  i.times do
    h, v = gets.chomp.split.map {|r| r.to_i}
    ints << [h, v]
  end

  hhash = {}
  vhash = {}

  days = []
  ints.each_with_index do |a, i|
    h, v = a
    if not hhash[h] and not vhash[v]
      hhash[h] = :done
      vhash[v] = :done
      days << i+1
    end
  end

  days.each do |d|
    print "#{d} "
  end

  puts ""

end

main

n = gets.to_i
x = Array.new(n, false)
y = Array.new(n, false)
ans = []
(n * n).times do |a|
  i, j = gets.split.map(&:to_i)
  next if x[i] || y[j]
  x[i] = true
  y[j] = true
  ans << a + 1
end
puts ans.join(' ')

ans = [ ]

n = gets.strip.to_i
h_road = Array.new(n, false)
v_road = Array.new(n, false)

(n * n).times do |i|
  h, v = gets.strip.split(' ').map(&:to_i)
  h -= 1
  v -= 1

  if !h_road[h] and !v_road[v]
    ans.push(i + 1)
    h_road[h] = true
    v_road[v] = true
  end
end

puts ans.sort.join(' ')

a=gets.chomp.to_i
h=[]
v=[]
temp=gets.chomp.split(" ")
h[0]=temp[0]
v[0]=temp[1]
printf "1 "
if a>1
2.upto(a*a) do |i|
temp=gets.chomp.split(" ")
if h.count("#{temp[0]}")==0 && v.count("#{temp[1]}")==0
h << temp[0]
v << temp[1]
printf "#{i} "
end
end
end

#$stdin = File.open('in.txt')
#$stdout = File.open('out.txt', 'w')

n = gets.to_i

ur = []
uc = []

(1..(n*n)).each { |i|
  r, c = gets.split.map(&:to_i)
  if ur[r] == nil && uc[c] == nil
    ur[r] = 1
    uc[c] = 1
    p i
  end
}

n = gets.chomp.to_i
row = Array.new(n, false)
col = Array.new(n, false)

all = n * n
ans = []
all.times do |day|
  r, c = gets.chomp.split(' ').map(&:to_i)
  next if row[r - 1] || col[c - 1]
  row[r - 1] = true
  col[c - 1] = true
  ans << day + 1
end

puts ans.join(' ')

n=gets.to_i
a = []
b = []

out= []
(n*n).times do |i|
	x,y = gets.split(" ").map(&:to_i)
	if (!a[x] && !b[y])
		out << i+1
		a[x] = true
		b[y] = true
	end
end

puts out.join(" ")

h,v, days = [], [], []
n = gets.chomp.to_i
(n**2).times do |t|
	hi, vi = gets.chomp.split(" ").map(&:to_i)
	if !h.include?(hi) and !v.include?(vi)
		h << hi
		v << vi
		days << (t+1)
	end
end
puts days.join " "

n = STDIN.gets.strip.to_i
hs = Array.new(n+1, false)
vs = Array.new(n+1, false)
days = []
(n*n).times do |d|
  h, v = STDIN.gets.strip.split(" ").map{|i| i.to_i}
  next if hs[h] || vs[v]
  hs[h] = vs[v] = true
  days << d + 1
end

puts days.sort.join(" ")

n = gets.to_i
a = Array.new(n){false}
b = Array.new(n){false}
(n*n).times{ |i|
  x, y = gets.split(" ").map(&:to_i)
  if !a[x] && !b[y]
    print (i+1).to_s + " "
    a[x] = true
    b[y] = true
  end
}

n = gets.chomp.to_i
arr = []
for i in (0..(n**2)-1)
	arr[i] = gets.chomp.split.map(&:to_i)
end

h = []
v = []
h[0] = arr[0][0]
v[0] = arr[0][1]

print 1
print " "
for i in (1..(n**2)-1)
	if h.include?(arr[i][0]) == false && v.include?(arr[i][1]) == false
		print i+1
		print " "
		h << arr[i][0]
		v << arr[i][1]
	end
end

n = gets.to_i
x = []
y = []
ans = []
(n * n).times do |a|
  i, j = gets.split.map(&:to_i)
  next if x[i] || y[j]
  x[i] = true
  y[j] = true
  ans << a + 1
end
puts ans.join(' ')

a=gets.chomp.to_i
h=[]
v=[]
temp=gets.chomp.split(" ")
h[0]=temp[0]
v[0]=temp[0]
printf "1 "
if a>1
2.upto(a*a) do |i|
temp=gets.chomp.split(" ")
if h.count("#{temp[0]}")==0 && v.count("#{temp[1]}")==0
h << temp[0]
v << temp[1]
printf "#{i} "
end
end

end

a=gets.chomp.to_i
h=[]
v=[]
temp=gets.chomp.split(" ")
h[0]=temp[0]
v[0]=temp[1]
printf "1 "
if a>1
2.upto(a*a) do |i|
puts "#{i}"

temp=gets.chomp.split(" ")
if h.count("#{temp[0]}")==0 && v.count("#{temp[1]}")==0
h << temp[0]
v << temp[1]
printf "#{i} "
end
end

end

n = gets.chomp.to_i
arr = []
for i in (0..(n**2)-1)
	arr[i] = gets.chomp.split.map(&:to_i)
end

h = []
v = []
h[0] = arr[0][0]
v[0] = arr[0][1]

print 1
print " "
for i in (1..(n**2)-1)
	if h.include?(arr[i][0]) == false && v.include?(arr[i][1]) == false
		print i+1
		h << arr[i][0]
		v << arr[i][1]
	end
end


def nxt(t, s, d)
  tmp = [t - s, 0].max
  tmp = (tmp + d - 1) / d
  return s + tmp * d
end

n, t = gets.to_i, 0
for i in (1..n) do
  t = t + 1
  s, d = gets.split
  t = nxt(t, s.to_i, d.to_i)
end
print t

n = gets.to_i
ans = 0
n.times do |i|
    x, y = gets.split.map(&:to_i)
    tmp = ans - x
    if tmp < 0
        ans = x
    elsif tmp % y == 0
        ans = (tmp / y + 1) * y + x
    else
        ans = (tmp + y - 1) / y * y + x
    end
end
puts ans

def antimod(x, m)
  return (m - x % m) % m
end
n = gets.to_i
k = 0
n.times { |i|
  sd = gets.split().map{|a| a.to_i}
  if i == 0
    k = sd[0]
  else
    if sd[0] > k
      k = sd[0]
    else
      k = sd[0] + ((k-sd[0])/sd[1]+1)*sd[1]
    end
  end
}
puts k

n = gets.to_i
ans = 0
n.times do |i|
    x, y = gets.split.map(&:to_i)
    tmp = [ans - x, 0].max
    if i == 0
        ans = x
    elsif tmp % y == 0
        ans = (tmp / y + 1) * y + x
    else
        ans = (tmp + y - 1) / y * y + x
    end
end
puts ans


gets.chomp.to_i.times do |_i|
  a, b, c, d = gets.split.map(&:to_i)
  p [a + b, c + d].max
end

gets.to_i.times do
  a, b, c, d = gets.split.map &:to_i
  puts [a + b, c + d].max
end