# http://codeforces.com/contest/556/problem/A
n = gets.chomp.to_i
digits = gets.chomp.chars.map { |d| d == '0' ? 1 : -1 }

puts digits.inject(0, :+).abs

n = gets.to_i
s = gets
ans = 0
n.times { |i|
  if s[i] == '0'
    ans +=1
  else
    ans -= 1
  end
}
if ans < 0
  ans = -ans
end
print ans

gets
a=gets.chomp.chars
b=[]
ans=a.size
while !a.empty?
	while !b.empty? && !a.empty?
		if (b.last!=a[0]) then
			ans-=2
			b.pop
			a.shift
		else
			break
		end
	end
	b.push(a.shift)
end
puts ans

t = gets.chomp.to_i
s = gets.chomp

puts t - (2*[s.count('0'),s.count('1')].min)

def read_ints; gets.chomp.split.map(&:to_i) end
n = read_ints[0]
line = gets.chars
ones = line.count("1")
zeros = line.count("0")
puts (ones-zeros).abs

n = gets.to_i
a = gets.each_char.count{|i| i == '1'}
puts n - [ a , n - a].min * 2

length = gets.chomp.to_i
s = gets.chomp
string = s.split("")

# while string.include?('1') && string.include?('0')
# 	for i in (0..string.size-1)
# 		if i != string.size-1
# 			if (string[i].to_i == 0 && string[i+1].to_i == 1) || (string[i].to_i == 1 && string[i+1].to_i == 0)
# 				string.delete_at(i)
# 				string.delete_at(i+1)
# 			end
# 		end
# 	end
# end

if string.count('1') != 0 && string.count('0') != 0 && length > 1
	if string.count('0') == string.count('1')
		puts 0
	elsif string.count('0') > string.count('1')
		length = length - (string.count('1') * 2)
		puts length
	else
		length = length - (string.count('0') * 2)
		puts length
	end
elsif (string.count('1') == 0 || string.count('0') == 0) && length > 1
	puts length
elsif length <= 1
	puts length
end

len = gets.to_i
s = gets

a = 0
b = 0

for i in (0...len)
	s[i] == "0" ? a += 1 : b += 1
end

puts len - 2 * [a, b].min

gets
s = gets.chomp

puts (s.count('1') - s.count('0')).abs

n = gets.chomp.to_i
str = gets.chomp

one = 0
zero = 0

for i in (0..str.length-1)
        if str[i] == "0"
                zero += 1
                else
                one += 1
                end
                end

                puts (one-zero).abs

stringLength = gets.chomp.to_i
string = gets.chomp.split('')

zero = 0
one = 0

string.each do |item|
	if item == '1'
		one += 1
	else
		zero += 1
	end
end

puts stringLength - 2 * [zero, one].min

gets
s=gets
p (s.count('0')-s.count('1')).abs

# your code goes here

n = gets.to_i
s = gets.chomp

a = 0
i = 0

while i<n do
	if s[i]=='0'
		a += 1
	else
		a -= 1
	end

	i += 1
end

puts "#{a.abs}"

n = gets.to_i
arr = gets.split('')
stac = []
arr.each do |a|
    if a == '1'
        if stac.last == '0'
            stac.pop
        else
            stac.push(a)
        end
    elsif a == '0'
        if stac.last == '1'
            stac.pop
        else
            stac.push(a)
        end
    end
end
puts stac.size

n = gets.to_i
s = gets.chomp.to_s.split(//)

xounter = 0
counter = 0
s.each do |x|
    if x == "0"
        counter+=1
    else
        xounter+=1
    end
end
puts (counter-xounter).abs

n = gets.to_i
s = gets.chomp.to_s.split(//)

counter = 0
s.each do |x|
    if x == "0"
        counter+=1
    else
        counter-=1
    end
end
puts counter.abs

n = gets.to_i
str = gets.chomp.gsub(/10/,'x').gsub(/01/,'x')
puts n - 2 * str.scan(/x/).size

length = gets.chomp.to_i
s = gets.chomp
string = s.split("")

# while string.include?('1') && string.include?('0')
# 	for i in (0..string.size-1)
# 		if i != string.size-1
# 			if (string[i].to_i == 0 && string[i+1].to_i == 1) || (string[i].to_i == 1 && string[i+1].to_i == 0)
# 				string.delete_at(i)
# 				string.delete_at(i+1)
# 			end
# 		end
# 	end
# end

if string.count('1') != 0 && string.count('0') != 0
	if string.count('0') == string.count('1')
		puts 0
	elsif string.count('0') > string.count('1')
		length = length - (string.count('1') * 2)
		puts length
	else
		length = length - (string.count('0') * 2)
		puts length
	end
end

stringLength = gets.chomp.to_i
string = gets.chomp.split('')

i = 0

until i == string.length || string.length < 2
#	puts i
#	puts string.length

	if string[i] != string[i + 1]
		2.times {string.delete_at(i)}
		i -= 1
	end

	i += 1
end

puts string.length


require 'set'

p = [71, 72, 82, 83, 63]
n = ["Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"]

s = Set.new [[0, 0, 0, 0]]
gets
gets.each_byte do |c|
   t = Set.new []
   next if !p.include?(c)
   s.each do |a|
      if c == 63
         for i in (0..3)
            if a[i] == a.min
               b = Array.new(a)
               b[i] += 1
               t << b
            end
         end
      else
         a[p.index(c)] += 1
         t << a
      end
   end

   s = t
end
for i in (0..3)
    puts n[i] if s.any? {|a| a[i] == a.min}
end

require 'set'

p = [71, 72, 82, 83]
n = ["Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"]

s = Set.new [[0, 0, 0, 0]]
gets
gets.chomp.each_byte do |c|
   t = Set.new
   s.each do |a|
      if c == 63
         for i in (0..3).select { |y| a[y] == a.min }
            b = a.clone
            b[i] += 1
            t << b
         end
      else
         a[p.index(c)] += 1
         t << a
      end
   end

   s = t
end
for i in (0..3)
    puts n[i] if s.any? {|a| a[i] == a.min}
end

require 'set'

p = [71, 72, 82, 83]
n = ["Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"]

s = Set.new [[0, 0, 0, 0]]
gets
gets.chomp.each_byte do |c|
   t = Set.new []
   s.each do |a|
      if c == 63
         for i in (0..3)
            if a[i] == a.min
               b = Array.new(a)
               b[i] += 1
               t << b
            end
         end
      else
         a[p.index(c)] += 1
         t << a
      end
   end

   s = t
end
for i in (0..3)
    puts n[i] if s.any? {|a| a[i] == a.min}
end


#!/usr/bin/ruby

n = STDIN.readline.to_i
heights = STDIN.readline.split.collect {|s| s.to_i }
heights.unshift(0)

boost, energy = 0, 0
(1..n).each do |pos|
	energy -= heights[pos] - heights[pos-1]
	if energy < 0
		boost -= energy
		energy = 0
	end
end

puts boost

n, *a = gets(p).split.map(&:to_i)
puts a.max

n=gets
x=gets.split.map{|e| e.to_i}
x.shift(0)
sum=0
ans=0
x.inject(0){|e1,e2|
	sum+=(e2-e1)
	if sum>0 then
		ans+=sum
		sum=0
	end
	e2
}
puts ans

n = gets.to_i
h = gets.split.map{|s| s.to_i}

puts h.max

n = gets.chomp.split(' ').map(&:to_i)

e = 0
cost = 0
last_h = 0

gets.chomp.split(' ').each do |s|
  h = s.to_i
  de = last_h - h + e
  if de < 0
    cost += de.abs
    e += de.abs
  end
  e += last_h - h
  last_h = h
end

puts cost

# coding: utf-8

# if __FILE__ == 0
# end

n = gets.to_i
prev = 0
energy = 0
peak = 0
gets.split.map { |i|
    i = i.to_i
    energy += prev - i
    if energy < peak
        peak = energy
    end
    prev = i
}
if peak < 0
    puts -peak
else
    puts 0
end

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

# $stdin = File.new('rf','r')

n = readline.chomp.to_i
a = [0] + readline.chomp.split(' ').map(&:to_i)
e,m = 0,0

loop do
  cur, nex = a.shift, a.first
  if e < nex - cur
    m += nex - cur - e
    e += nex - cur - e
  end
  e += cur - nex
  break if a.size == 1
end

puts m

n = gets.to_i

a = gets.chomp.split(' ').map(&:to_i)

puts a.max

r = s = d = 0
gets.to_i
gets.chomp.split.map(&:to_i).each { |x|
  d += r - x
  if d < 0
    s -= d
    d = 0
  end
  r = x
}
puts s

#!/usr/bin/env ruby

n = Integer(gets)

h= gets.chomp.split(" ").map(&:to_i)
dollars = 0
h.each do |h|
	dollars = [dollars, h].max
end

puts dollars

#!/usr/bin/env ruby

n = Integer(gets)

h= gets.chomp.split(" ").map(&:to_i)
energy = 0
dollars = 0
c_h = 0
h.each do |h|
	energy+= c_h - h
	c_h = h
	dollars+=[0,energy].min.abs
	energy=[0,energy].max
end

puts dollars

n = gets.to_i
a = gets.split.map { |x| x.to_i }
puts a.max

n = gets
a = gets.split.map { |i| i.to_i }
puts a.max

require 'pp'

n = STDIN.gets.chomp.to_i
m = STDIN.gets.chomp.split(/ /).map(&:to_i)

puts m.max

n = gets.split
puts gets.split.map(&:to_i).max

gets
arr = gets.split.map { |x| x.to_i }
k = arr.first
arr.shift
arr.each do |x|
  k += (x - k) if k < x
end
puts k

n=gets
x=gets.split.map{|e| e.to_i}
x.shift(0)
sum=0
x.inject(0){|e1,e2|
	sum+=(e2-e1)
	e2
}
puts sum

# coding: utf-8

# if __FILE__ == 0
# end

n = gets.to_i
prev = 0
energy = 0
gets.split.map { |i|
    i = i.to_i
    energy += prev - i
    prev = i
}
if energy < 0
    puts -energy
else
    puts 0
end

# coding: utf-8

# if __FILE__ == 0
# end

n = gets.to_i
prev = 0
energy = 0
gets.split.map { |i|
    i = i.to_i
    energy += prev - i
    prev = i
}

puts energy

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

# $stdin = File.new('rf','r')

n = readline.chomp.to_i
a = [0] + readline.chomp.split(' ').map(&:to_i)
e = 0

loop do
  e += a.shift - a.first
  break if a.size == 1
end

puts [0, -e].max

gets.to_i
puts -gets.chomp.split.map(&:to_i).inject([0, 0]) { |a, x| a[0] += a[1] - x; a[1] = x; a }[0]

n = gets.split
puts gets.split.map(&:to_s).max


n,q = gets.chomp.split.map(&:to_i)
p = gets.chomp.split.map(&:to_i).sort
c = [p[0]]
1.upto(n-1) do |i|
  c[i] = c[i-1] + p[i]
end
#puts c.inspect
q.times do
  x,y = gets.chomp.split.map(&:to_i)
  #puts c[n-x+y-1]
  #puts c[n-x-1]
  puts c[n-x+y-1] - (n-x-1 >= 0 ? c[n-x-1] : 0)
end

n, q = gets.split.map(&:to_i)
ps = gets.split.map(&:to_i).sort.reverse
sums = [0]
ps.each { |p| sums << p + sums.last }
rs = []
q.times do
  x, y = gets.split.map(&:to_i)
  rs << sums[x] - sums[x - y]
end
puts rs.join("\n")

n, q = gets.split.map &:to_i
p = gets.split.map(&:to_i).sort
blah = 0
prefix_sum = []
for i in (0...p.size)
  blah += p[i]
  prefix_sum.push(blah)
end
q.times{
  x, y = gets.split.map &:to_i
  if x == n
    puts prefix_sum[-x+y-1]
  else
    puts prefix_sum[-x+y-1] - prefix_sum[-x-1]
  end
}

def solve(cs, x, y)
   cs[-x + y - 1] - cs[-x - 1]
end

n, q = gets.chomp.split.map(&:to_i)
xs = gets.chomp.split.map(&:to_i).sort
cs = [0]
xs.each do |x|
    cs << x + cs[-1]
end

results = []

q.times do
    x, y = gets.chomp.split.map(&:to_i)
    results << solve(cs, x, y)
end

puts results

n, q = gets.split.map(&:to_i)
p = gets.split.map(&:to_i).sort
i = 0
psum = p.map{|e| i+=e }
psum.unshift(0)

q.times do
  x, y = gets.split.map(&:to_i)

  puts psum[n-x+y] - psum[n-x]
end


n, k, a, b = gets.chomp.split(" ").map(&:to_i)
s = ''
if a<=b
  ctr = 1
else
  ctr = -1
end
a1 = [a,b].min # a1 = b
b1 = [a,b].max #
if b1 > k*(a1+1)
  print "NO"
else
  while(b1 - a1 > k)
    if (ctr == 1)
      s = s + "B"*k + "G"
    else
      s = s + "G"*k + "B"
    end
    b1-=k
    a1-=1
  end
  if ctr == 1
    s += "B"*(b1-a1)
    s += "GB"*a1
  else
    s += "G"*(b1-a1)
    s +="BG"*a1
  end
  puts s
end

n, k, g, b = gets.chomp.split.map(&:to_i)

inverse = false
a = []
if g < b
  inverse = true
  g, b = b, g
end

if (b + 1) * k < g
  puts "NO"
  exit
end

while g != b and b > 0
  cnt = [k, g - (b - 1)].min
  g -= cnt
  b -= 1
  cnt.times {a.push(true)}
  a.push(false)
end

while g > 0
  g -= 1
  a.push(true)
  if b > 0
    b -= 1
    a.push(false)
  end
end

if inverse
  a.map!(&:!)
end

puts a.map {|x| x ? 'G' : 'B'}.join

n,k,a,b=gets.split.map &:to_i
x=?G
y=?B
x,y,a,b=y,x,b,a if a>b

q=[b,k].min

r=[]
i=0
while b>0
    if i.even?
        t=[b, q].min
        r<<y*t
        b-=t
    else
        r<<x if a>0
        a-=1
    end
    i+=1
end

if a<0
    puts "NO"
else
    if a>0
        r<<x
        a-=1
    end
    i=1
    while a>0
        r[i]+=x
        if r[i].size>k
            puts a
            r="NO"
            break
        end
        a-=1
        i+=2
        i=1 if i>=r.size
    end
    puts (r=="NO" ? r : r*'')
end

n,k,a,b=gets.split.map &:to_i
x=?G
y=?B
x,y,a,b=y,x,b,a if a>b

q=[b,k].min
p=[[a/(b/q), 1].max, a, k].min

r=''
i=0
while a>0 or b>0
    if i.even?
        t=[b, q].min
        r<<y*t
        b-=t
    else
        t=[a, p].min
        r<<x*t
        a-=t
    end
    i+=1
end
if r.include?(x*(k+1)) or r.include?((y*(k+1)))
    puts "NO"
else
    puts r
end

n,k,a,b=gets.split.map &:to_i
x=?G
y=?B
x,y,a,b=y,x,b,a if a>b

q=[b,k].min

r=[]
i=0
while b>0
    if i.even?
        t=[b, q].min
        r<<y*t
        b-=t
    else
        r<<x if a>0
        a-=1
    end
    i+=1
end
if a<0
    puts "NO"
else
    if a>0
        r<<x
        a-=1
    end
    i=1
    while a>0
        r[i] << x
        if r[i].size>k
            r="NO"
            break
        end
        a-=1
        i=(i+2) % r.size
    end
    puts (r=="NO" ? r : r*'')
end


def gcd(a,b)
if b == 0
  return a;
else
  return gcd(b, a % b);
end
end

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

sign = nil
ans = nil
if n > m
	ans = "Infinity"
	sign = "-" if (a[0]/b[0]) < 0
elsif n < m
	ans = "0/1"
	# sign = "-" if (a[0]/b[0]) < 0
else
	num = a[0]
	den = b[0]
	if num < den
		a,b = den,num
	else
		a,b = num,den
	end
	gc = gcd(a,b)
	num /= gc
	den /= gc
	ans = "#{num.abs}/#{den.abs}"
	# puts "here"
	sign = "-" if (num/den) < 0
end

puts "#{sign}#{ans}"

# https://en.wikipedia.org/wiki/Limit_of_a_function
# Rational functions

#!/usr/bin/ruby

def gcd(a, b)
	return (b != 0) ? gcd(b, a % b) : a
end

while line = gets
	m, n = line.split.map(&:to_i)
	as = gets.split.map(&:to_i)
	bs = gets.split.map(&:to_i)
	if m > n
		puts (as[0] * bs[0] < 0) ? "-Infinity" : "Infinity"
	elsif m < n
		puts "0/1"
	else
		p = as[0]
		q = bs[0]
		g = gcd(p, q);
		p /= g
		q /= g
		if q < 0
			p *= -1
			q *= -1
		end
		puts "#{p}/#{q}"
	end
end

# cook your code here

n,m = gets.split.map{ |x| x.to_i }

first = gets.split.map{ |x| x.to_i }
second = gets.split.map{ |x| x.to_i }

if n>m
    if first[0]*second[0]>0
        puts "Infinity"
    else
        puts "-Infinity"
    end
elsif n<m
    puts "0/1"
else
    puts Rational(first[0],second[0])
end

def gcd(a, b)
	if b == 0
		return a
	end
	return gcd(b, a % b)
end

n, m = gets.split
n = n.to_i
m = m.to_i

p = gets.split.reverse
q = gets.split.reverse

if n > m
	if p.last.to_i * q.last.to_i > 0
		print "Infinity\n"
	else
		print "-Infinity\n"
	end
elsif n == m
	a, b = p.last.to_i, q.last.to_i
	if a * b > 0
		if a < 0
			a, b = -a, -b
		end
		g = gcd(a, b)
		print "%d/%d\n" % [a / g, b / g]
	else
		if a < 0
			a = -a
		end
		if b < 0
			b = -b
		end
		g = gcd(a, b)
		print "-%d/%d\n" % [a / g, b / g]
	end
else
	print "0/1\n"
end

n,m = gets.split.map{|e| e.to_i}
a1 = gets.split.map{|e| e.to_i}
a2 = gets.split.map{|e| e.to_i}
if m>n
  puts "0/1"
  exit
end
if n>m
  k = a1[0]*a2[0]
  if k>0 then puts "Infinity" else puts "-Infinity" end
  exit
end
if a2[0]<0
  a2[0]*=-1
  a1[0]*=-1
end

g = a1[0].gcd(a2[0])
puts "#{a1[0]/g}/#{a2[0]/g}"

require 'mathn'

n, m = gets.split.map(&:to_i)
pn = gets.split.first.to_i
qm = gets.split.first.to_i

sign = pn/qm < 0

if n > m
  puts "#{sign ? "-" : ""}Infinity"
elsif n < m
  puts "0/1"
else
  divs_p = pn.abs.prime_division Prime::TrialDivisionGenerator.new
  divs_q = qm.abs.prime_division Prime::TrialDivisionGenerator.new
  divs_p.each_with_index do |d, ip|
    l = divs_q.assoc d.first
    if l
      iq = divs_q.index l
      delta = [d.last, l.last].min
      divs_p[ip] = [d.first, d.last - delta]
      divs_q[iq] = [d.first, l.last - delta]
    end
  end

  puts "#{sign ? "-" : ""}#{Integer.from_prime_division divs_p }/#{Integer.from_prime_division divs_q }"
end

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

if n==m
  g = a[0].gcd(b[0])
  if b[0]<0 then a[0], b[0] = -a[0], -b[0] end
  print a[0]/g,'/',b[0]/g
elsif n>m
  print (a[0]*b[0]<0 ? "-" :""),"Infinity"
elsif n<m
  print "0/1"
end

def gcd(x,y)
  return x==0?y:gcd(y%x, x)
end

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

if n==m
  g = gcd(a[0].abs, b[0].abs)
  if b[0]<0 then a[0], b[0] = -a[0], -b[0] end
  print a[0]/g,'/',b[0]/g
elsif n>m
  print (a[0]*b[0]<0 ? "-" :""),"Infinity"
elsif n<m
  print "0/1"
end

# cook your code here

n,m = gets.split.map{ |x| x.to_i }

first = gets.split.map{ |x| x.to_i }
second = gets.split.map{ |x| x.to_i }

if n>m
    if n*m>0
        puts "Infinity"
    else
        puts "-Infinity"
    end
elsif n<m
    puts "0/1"
else
    puts Rational(first[0],second[0])
end

def gcd(a, b)
	if b == 0
		return a
	end
	return gcd(b, a % b)
end

n, m = gets.split
n = n.to_i
m = m.to_i

p = gets.split.reverse
q = gets.split.reverse

if n > m
	if p.last.to_i > 0
		print "Infinity\n"
	else
		print "-Infinity\n"
	end
elsif n == m
	a, b = p.last.to_i, q.last.to_i
	if a * b > 0
		if a < 0
			a, b = -a, -b
		end
		g = gcd(a, b)
		print "%d/%d\n" % [a / g, b / g]
	else
		if a < 0
			a = -a
		end
		if b < 0
			b = -b
		end
		g = gcd(a, b)
		print "-%d/%d\n" % [a / g, b / g]
	end
else
	print "0/1\n"
end

require 'mathn'

n, m = gets.split.map(&:to_i)
pn = gets.split.first.to_i
qm = gets.split.first.to_i

if n > m
  puts "#{pn < 0 ? "-" : ""}Infinity"
elsif n < m
  puts "#{qm < 0 ? "-" : ""}0/1"
else
  sign = pn/qm < 0
  divs_p = pn.abs.prime_division Prime::EratosthenesGenerator.new
  divs_q = qm.abs.prime_division Prime::EratosthenesGenerator.new
  divs_p.each_with_index do |d, ip|
    l = divs_q.assoc d.first
    if l
      iq = divs_q.index l
      delta = [d.last, l.last].min
      divs_p[ip] = [d.first, d.last - delta]
      divs_q[iq] = [d.first, l.last - delta]
    end
  end

  puts "#{sign ? "-" : ""}#{Integer.from_prime_division divs_p }/#{Integer.from_prime_division divs_q }"
end

require 'mathn'

n, m = gets.split.map(&:to_i)
pn = gets.split.first.to_i
qm = gets.split.first.to_i

if n > m
  puts "#{pn < 0 ? "-" : ""}Infinity"
elsif n < m
  puts "#{qm < 0 ? "-" : ""}0/1"
else
  sign = pn/qm < 0
  divs_p = pn.abs.prime_division
  divs_q = qm.abs.prime_division
  divs_p.each_with_index do |d, ip|
    l = divs_q.assoc d.first
    if l
      iq = divs_q.index l
      delta = [d.last, l.last].min
      divs_p[ip] = [d.first, d.last - delta]
      divs_q[iq] = [d.first, l.last - delta]
    end
  end

  puts "#{sign ? "-" : ""}#{Integer.from_prime_division divs_p }/#{Integer.from_prime_division divs_q }"
end

require 'mathn'

n, m = gets.split.map(&:to_i)
pn = gets.split.first.to_i
qm = gets.split.first.to_i

if n > m
  puts "#{pn < 0 ? "-" : ""}Infinity"
elsif n < m
  puts "0/1"
else
  sign = pn/qm < 0
  divs_p = pn.abs.prime_division Prime::TrialDivisionGenerator.new
  divs_q = qm.abs.prime_division Prime::TrialDivisionGenerator.new
  divs_p.each_with_index do |d, ip|
    l = divs_q.assoc d.first
    if l
      iq = divs_q.index l
      delta = [d.last, l.last].min
      divs_p[ip] = [d.first, d.last - delta]
      divs_q[iq] = [d.first, l.last - delta]
    end
  end

  puts "#{sign ? "-" : ""}#{Integer.from_prime_division divs_p }/#{Integer.from_prime_division divs_q }"
end

require 'mathn'

n, m = gets.split.map(&:to_i)
pn = gets.split.first.to_i
qm = gets.split.first.to_i

if n > m
  puts "#{pn < 0 ? "-" : ""}Infinity"
elsif n < m
  puts "#{qm < 0 ? "-" : ""}0/1"
else
  sign = pn/qm < 0
  divs_p = pn.abs.prime_division
  divs_q = qm.abs.prime_division
  divs_p.each_with_index do |d, ip|
    l = divs_q.assoc d.first
    if l
      iq = divs_q.index l
      delta = (d.last - l.last).abs
      divs_p[ip] = [d.first, d.last - delta]
      divs_q[iq] = [d.first, l.last - delta]
    end
  end

  puts "#{sign ? "-" : ""}#{Integer.from_prime_division divs_p }/#{Integer.from_prime_division divs_q }"
end

require 'mathn'

n, m = gets.split.map(&:to_i)
pn = gets.split.first.to_i
qm = gets.split.first.to_i

if n > m
  puts "#{pn < 0 ? "-" : ""}Infinity"
elsif n < m
  puts "#{qm < 0 ? "-" : ""}0/1"
else
  sign = pn/qm < 0
  divs_p = pn.abs.prime_division Prime::TrialDivisionGenerator.new
  divs_q = qm.abs.prime_division Prime::TrialDivisionGenerator.new
  divs_p.each_with_index do |d, ip|
    l = divs_q.assoc d.first
    if l
      iq = divs_q.index l
      delta = [d.last, l.last].min
      divs_p[ip] = [d.first, d.last - delta]
      divs_q[iq] = [d.first, l.last - delta]
    end
  end

  puts "#{sign ? "-" : ""}#{Integer.from_prime_division divs_p }/#{Integer.from_prime_division divs_q }"
end

def gcd(a,b)
if b == 0
  return a;
else
  return gcd(b, a % b);
end
end

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

sign = nil
ans = nil
if n > m
	ans = "Infinity"
	sign = "-" if (a[0]/b[0]) < 0
elsif n < m
	ans = "0/1"
	sign = "-" if (a[0]/b[0]) < 0
else
	num = a[0]
	den = b[0]
	if num < den
		a,b = den,num
	else
		a,b = num,den
	end
	gc = gcd(a,b)
	num /= gc
	den /= gc
	ans = "#{num}/#{den}"
	# puts "here"
	# sign = "-" if (num/den) < 0
end

puts "#{sign}#{ans}"

# https://en.wikipedia.org/wiki/Limit_of_a_function
# Rational functions

def gcd(a,b)
if b == 0
  return a;
else
  return gcd(b, a % b);
end
end

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

sign = nil
ans = nil
if n > m
	ans = "Infinity"
	sign = "-" if (a[0]/b[0]) < 0
elsif n < m
	ans = "0/1"
	# sign = "-" if (a[0]/b[0]) < 0
else
	num = a[0]
	den = b[0]
	if num < den
		a,b = den,num
	else
		a,b = num,den
	end
	gc = gcd(a,b)
	num /= gc
	den /= gc
	ans = "#{num}/#{den}"
	# puts "here"
	# sign = "-" if (num/den) < 0
end

puts "#{sign}#{ans}"

# https://en.wikipedia.org/wiki/Limit_of_a_function
# Rational functions

def gcd(x,y)
  return x==0?y:gcd(y%x, x)
end

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

if n==m
  g = gcd(a[0].abs, b[0].abs)
  if b[0]<0 then a[0], b[0] = -a[0], -b[0] end
  print a[0]/g,'/',b[0]/g
elsif n>m
  print (a[0]<0?"-":""),"Infinity"
elsif n<m
  print "0/1"
end


n, a, b = readline.split(' ').collect(&:to_i)
if a * b < n
  puts "-1"
else
  x, y = 1, 2
  1.upto(a) do |row|
    1.upto(b) do |col|
      if col != 1
        print " "
      end
      if ((row + col) & 1) == 0
        if (x <= n)
          print x
          x += 2
        else
          print "0"
        end
      else
        if (y <= n)
          print y
          y += 2
        else
          print "0"
        end
      end
    end
    puts ""
  end
end

n, a, b = gets().split(' ').map { |e| e.to_i  }

if a * b < n
  puts -1
  exit()
end

current = 1
total = a * b
arr = []
(0 .. a - 1).each do |row|
  r = []
  (0 .. b - 1).each do |column|
    cond = (row % 2 == 0 && column % 2 == 0) || (row % 2 == 1 && column % 2 == 1)
    r.push(cond)
  end
  arr.push(r)
end
odd = (1 .. n).to_a.select { |e| e % 2 == 1}
even = (1 .. n).to_a.select { |e| e % 2 == 0}

(0 .. a - 1).each do |row|
  (0 .. b - 1).each do |column|
    print ' ' if column > 0
    if arr[row][column]
      if odd.size > 0
        print odd.pop
      else
        print 0
      end
    else
      if even.size > 0
        print even.pop
      else
        print 0
      end
    end
  end
  puts()
end

g = gets.split(" ")
n,a,b=g[0].to_i,g[1].to_i,g[2].to_i
if n>a*b
	puts -1
else
	f=Array.new(a)
	for i in 0..a-1
		f[i] = Array.new(b, 0)
	end
	x=y=0
	dir=c=1
	while x<a && c<=n
		f[x][y]=c
		c+=1
		y+=dir
		if y<0||y>=b
			y-=dir
			x+=1
			dir*=-1
		end
	end
	for i in 0..a-1
		puts f[i].join(" ")
	end
end

array_as_string = gets
n, a, b = array_as_string.split.map(&:to_i)
if ((a * b + 1) / 2 < (n + 1) / 2)
   puts -1
else
   oddidx = 0
   evenidx = 0
   1.upto a do |row|
      arr = []
      1.upto b do |column|
         if ((row + column) % 2 == 0)
            if (oddidx < (n + 1) / 2)
               arr << (oddidx * 2 + 1)
               oddidx = oddidx + 1
            else
               arr << 0
            end
         else
            if (evenidx < n / 2)
               evenidx = evenidx + 1
               arr << evenidx * 2
            else
               arr << 0
            end
         end
      end
      puts arr.join(" ")
   end
end

n,a,b = gets.split.map(&:to_i)
res = ''
ans = []
if n>a*b
  p -1
else
  if b.odd?
    for i in (0..(a-1))
      for j in (0..(b-1))
        res+=n.to_s+' '
        if (n > 0)
          n-=1
        end
      end
      puts res
      res=''
    end
  else
    for i in (0..(a-1))
      if i.odd?
        for j in (0..(b-1))
          ans[b-1-j] = n
          if (n > 0)
            n-=1
          end
        end
      else
        for j in (0..(b-1))
          ans[j] = n
          if (n > 0)
            n-=1
          end
        end
      end
      puts ans.join(' ')
      ans.clear
    end
  end
end

n, a, b = gets.split.map(&:to_i)
res = ''
temp = []
temp.push(0)
if (n > a*b)
    p -1
else
    if b.odd?
        for i in (0..(a-1))
            for j in (0..(b-1))
                res+=n.to_s+' '
                if (n > 0)
                    n-=1
                end
            end
            puts res
            res=''
        end
    else
        for i in (0..(a-1))
            for j in (0..(b-1))
                if ((n == 0) and (temp != []))
                    res+=temp[0].to_s+' '
                    temp.delete(temp[0])
                else
                    res+=n.to_s+' '
                end
                if (n > 0)
                    n-=1
                end
            end
            if (n > 0)
                temp.push(n)
                n-=1
            end
            puts res
            res=''
        end
    end
end


T = gets.to_i

T.times do
  n = gets.to_i
  a = gets.split.map(&:to_i).sort

  puts a[n] - a[n - 1]
end

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

arr.each_with_index do |ar, i|
  puts ar[nums[i]] - ar[nums[i] - 1]
end


n = gets.to_i
a = Array.new(n)
w = 0
h = 0
s1 = 0
s2 = 0
b = Array.new(n, 0)
c = Array.new(n, 0)
n.times{|i|
    a[i] = gets.split.map(&:to_i)
    n.times{|j|
        if a[i][j] == 0
            w = j
            h = i
        end
        if i == j
            s1 += a[i][j]
        end
        if i + j == n - 1
            s2 += a[i][j]
        end
        b[i] += a[i][j]
        c[j] += a[i][j]
        }
    }
if h == 0
    if n == 1
        p 1
        exit
    end
    sum = b[1]
else
    sum = b[0]
end
n.times{|i|
    if i != h && sum != b[i]
        p -1
        exit
    end
    }
n.times{|i|
    if i != w && sum != c[i]
        p -1
        exit
    end
    }
tmp1 = b[h]
tmp2 = c[w]
if tmp1 == tmp2 && sum > tmp1
    if w == h
        if s1 != tmp1
            p -1
            exit
        end
    elsif s1 != sum
        p -1
        exit
    end
    if w + h == n - 1
        if s2 != tmp1
            p -1
            exit
        end
    elsif s2 != sum
        p -1
        exit
    end
    p sum - tmp1
else
    p -1
end

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
sum_row, sum_col, sum_dia = Array.new(n, 0), Array.new(n, 0), Array.new(2, 0)
x, y = 0, 0
n.times do |i|
  n.times do |j|
    sum_row[i] += a[i][j]
    sum_col[j] += a[i][j]
    sum_dia[0] += a[i][j] if i == j
    sum_dia[1] += a[i][j] if i + j == n - 1
    x, y = i, j if a[i][j] == 0
  end
end
candidate = ( sum_row.uniq.last - sum_row.uniq.first ).abs
if n == 1
  puts 1
else
  sum_row[x] += candidate
  sum_col[y] += candidate
  sum_dia[0] += candidate if x == y
  sum_dia[1] += candidate if x + y == n - 1
  possible = candidate != 0 &&
    sum_row.uniq.size == 1 &&
    sum_col.uniq.size == 1 &&
    sum_dia.uniq.size == 1 &&
    sum_row.first == sum_col.first &&
    sum_col.first == sum_dia.first
  puts possible ? candidate : -1
end

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
sum_row = Array.new(n, 0)
sum_col = Array.new(n, 0)
sum_dia = Array.new(2, 0)
x, y = nil, nil
n.times do |i|
  n.times do |j|
    cur = a[i][j]
    sum_row[i] += cur
    sum_col[j] += cur
    sum_dia[0] += cur if i == j
    sum_dia[1] += cur if i + j == n - 1
    x, y = i, j if cur == 0
  end
end
ans = (sum_row.uniq.last - sum_row.uniq.first).abs
if n == 1
  puts 1437
else
  sum_row[x] += ans
  sum_col[y] += ans
  sum_dia[0] += ans if x == y
  sum_dia[1] += ans if x + y == n - 1
  valid = ans != 0 && (sum_row + sum_col + sum_dia).uniq.size == 1
  puts valid ? ans : -1
end

#!/usr/bin/ruby
def chk(m)
	n=m.size
	x=y=0
	n.times{|i|
		x+=m[i][i]
		y+=m[i][n-i-1]
	}
	(
		[x]+[y]+
		m.map{|e|e.reduce(:+)}+
		m.transpose.map{|e|e.reduce(:+)}
	).group_by{|e|e}
end

n=gets.to_i
if n==1
	p 1
else
	m=$<.map{|e|e.split.map(&:to_i)}
	a=chk(m)
	if a.size!=2
		p -1
	else
		k=-a.map(&:first).minmax.reduce(:-)
		n.times{|i|n.times{|j|
			m[i][j]=k if m[i][j]==0
		}}
		a=chk(m)
		p a.size!=1 ? -1 : k
	end
end

# http://codeforces.com/contest/711/problem/B
n = gets.chomp.to_i
rows = [].tap do |a|
  n.times { a << gets.chomp.split.map(&:to_i) }
end
arr = rows.tap do |a|
  # cols
  (0...n).each { |i| a << (0...n).map { |j| rows[j][i] } }
  # diagonal
  a << (0...n).map { |i| rows[i][i] }
  a << (0...n).map { |i| rows[n - i - 1][i] }
end
not_filled, filled = arr.partition { |a| a.include?(0) }
sum_of_not_filled = not_filled.map { |a| a.inject(0, :+) }.uniq
sum_of_filled = filled.map { |a| a.inject(0, :+) }.uniq
if n == 1
  puts 1
elsif sum_of_filled.length == 1 && sum_of_not_filled.length == 1
  diff = sum_of_filled.first - sum_of_not_filled.first
  puts diff > 0 ? diff : -1
else
  puts -1
end

def magic_grid?( grid )
    n = grid.length
    sum_rows = Array.new(n, 0)
    sum_cols = Array.new(n, 0)
    sum_diag_left = sum_diag_right = 0
    n.times do |i|
        n.times do |j|
            sum_rows[i] += grid[i][j]
            sum_cols[i] += grid[j][i]
        end
        sum_diag_right += grid[i][i]
        sum_diag_left += grid[i][n - i - 1]
    end
    return false if sum_rows.uniq.length != 1 || sum_cols.uniq.length != 1
    sum_cols[0] == sum_rows[0] && sum_rows[0] == sum_diag_left && sum_diag_left == sum_diag_right
end
n = gets.strip.to_i
grid = Array.new(n) { Array.new(n, 0) }
missing_pos = Array.new(2, 0)
in_left_diag = false
in_right_diag = false
if n == 1
    puts 1
else
    n.times do |i|
        grid[i] = gets.strip.split.map(&:to_i)
        j = grid[i].each_index.select { |z| grid[i][z] == 0 } [0]
        if !j.nil?
            in_right_diag = true if i == j
            in_left_diag = true if n - i - 1 == j
            missing_pos[0] = i
            missing_pos[1] = j
        end
    end
    sum_rows = Array.new(n, 0)
    sum_cols = Array.new(n, 0)
    sum_diag_left = sum_diag_right = 0
    n.times do |i|
        n.times do |j|
            sum_rows[i] += grid[i][j]
            sum_cols[i] += grid[j][i]
        end
        sum_diag_right += grid[i][i]
        sum_diag_left += grid[i][n - i - 1]
    end
    x, y = missing_pos
    sum_req = 0
    if in_left_diag && in_right_diag
        if x == 0
            sum_req = sum_rows[n - 1]
        elsif x == n - 1
            sum_req = sum_rows[0]
        else
            sum_req = sum_rows[x - 1]
        end
        ans = sum_req - sum_diag_left
    elsif in_right_diag
        sum_req = sum_diag_left
        ans = sum_req - sum_diag_right
    elsif in_left_diag
        sum_req = sum_diag_right
        ans = sum_req - sum_diag_left
    else
        sum_req = sum_diag_left
        ans = sum_req - sum_rows[x]
    end
    grid[x][y] = ans
    if magic_grid? grid
        if ans <= 0
            puts "-1"
        else
            puts ans
        end
    else
        puts "-1"
    end
end

n=readline.to_i
a = []
found = false
(1..n).each do
	ai = readline.split.map(&:to_i)
	a << ai
end

filled = []
not_filled = []
(0...n).each do |i|
	row = a[i]
	if row.include? 0
		not_filled << row.reduce(&:+)
	else
		filled << row.reduce(&:+)
	end

	col = (0...n).map{|j| a[j][i]}
	if col.include? 0
		not_filled << col.reduce(&:+)
	else
		filled << col.reduce(&:+)
	end
end

dia_1 = (0...n).map{|j| a[j][j]}
dia_2 = (0...n).map{|j| a[n-j-1][j]}

if dia_1.include? 0
	not_filled << dia_1.reduce(&:+)
else
	filled << dia_1.reduce(&:+)
end

if dia_2.include? 0
	not_filled << dia_2.reduce(&:+)
else
	filled << dia_2.reduce(&:+)
end

filled.uniq!
not_filled.uniq!
if n == 1
	puts 1
elsif filled.size == 1 && not_filled.size == 1 && filled[0] > not_filled[0]
	puts filled[0]-not_filled[0]
else
	puts -1
end

def mysum(a)
  nozero = true
  sum = 0
  a.each do |e|
    if e != 0
      sum += e
    else
      nozero = false
      break
    end
  end
  if nozero
    return sum
  else
    return -1
  end
end

def realsum(a)
  sum = 0
  a.each do |e|
    sum += e
  end
  return sum
end

def checkmat(mat, num)
  valid = true
  for i in 0..(mat.length-1) do
    rowsum = 0
    for ii in 0..(mat[i].length-1) do
      rowsum += mat[i][ii]
    end
    if rowsum != num
      valid = false
      break
    end
  end
  if not valid
    return false
  end
  for i in 0..(mat[0].length-1) do
    colsum = 0
    for ii in 0..(mat.length-1) do
      colsum += mat[ii][i]
    end
    if colsum != num
      valid = false
      break
    end
  end
  if not valid
    return false
  end
  dia1 = 0
  for i in 0..(mat.length-1) do
    dia1 += mat[i][i]
  end
  if dia1 != num
    return false
  end
  dia2 = 0
  for i in 0..(mat.length-1) do
    dia2 += mat[i][mat.length-1-i]
  end
  if dia2 != num
    return false
  end
  return true
end

n = gets.strip.to_i
matris = []
n.times do
  input = gets.strip.split(' ').map(&:to_i)
  matris << input
end

if n == 1
  puts 3
else
  thesum = 0
  zeroline = -1
  for i in 0..(matris.length-1) do
    x = mysum(matris[i])
    if x != -1
      thesum = x
      if zeroline != -1
        break
      end
    else
      zeroline = i
      next
    end
  end

  lol = realsum(matris[zeroline])
  trol = thesum - lol
  if trol > 0
    for i in 0..(matris[zeroline].length-1) do
      if matris[zeroline][i] == 0
        matris[zeroline][i] = trol
        break
      end
    end
    if checkmat(matris, thesum)
      puts trol
    else
      puts -1
    end
  else
    puts -1
  end
end

def func(a, n, s, t)
    a.each{|v|
        v.include?(0) ?  s<<v.inject(:+) : t<<v.inject(:+)
    }

    x=0
    f=false
    n.times{|i|
        x+=a[i][i]
        f=true if !f and a[i][i]==0
    }
    f ? s<<x : t<<x
end

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

s=[]
t=[]
func(a, n, s, t)
func(a.reverse.transpose, n, s, t)

if n==1
    p 1
elsif s.uniq.size>1 or t.uniq.size>1 or s[0]>=t[0]
    p -1
else
    p t[0]-s[0]
end

n=gets.to_i
if n==1
  p 1
  exit(0)
end
a=[]
x=n
y=n
max=0
min=10**15
n.times{|i|a<< gets.split.map(&:to_i)}
n.times{|i|
  l=0
  n.times{|j|
    l+=a[i][j]
    x,y=i,j if a[i][j]==0
  }
  max=[max,l].max
  min=[min,l].min
}
a[x][y] =[max-min,1].max
max=0
n.times{|i| max+=a[x][i]}
n.times{|i|
  v,h=0,0
  n.times{|j|
    v+=a[i][j]
    h+=a[j][i]
  }
  if v!=max || h!=max
    p -1
    exit(0)
  end
}
v,h=0,0
n.times{|i|
  v+=a[i][i]
  h+=a[i][n-1-i]
}
if (v!=max)||(h!=max)
  p -1
  exit(0)
end
p a[x][y]

#!/usr/bin/ruby
n=gets.to_i
if n==1
	p 1
else
	m=$<.map{|e|e.split.map(&:to_i)}
	x=y=0
	n.times{|i|
		x+=m[i][i]
		y+=m[i][n-i-1]
	}
	a=(
		[x]+[y]+
		m.map{|e|e.reduce(:+)}+
		m.transpose.map{|e|e.reduce(:+)}
	).group_by{|e|e}
	if a.size==2
		p -a.map(&:first).minmax.reduce(:-)
	else
		p -1
	end
end

#!/usr/bin/ruby
n=gets.to_i
if n==1
	p 1
else
	m=$<.map{|e|e.split}
	a=(
		m.map{|e|e.map(&:to_i).reduce(:+)}+
		m.transpose.map{|e|e.map(&:to_i).reduce(:+)}
	).group_by{|e|e}
	if a.size==2
		p -a.map(&:first).minmax.reduce(:-)
	else
		p -1
	end
end

#!/usr/bin/ruby
def chk(m)
	n=m.size
	x=y=0
	n.times{|i|
		x+=m[i][i]
		y+=m[i][n-i-1]
	}
	(
		[x]+[y]+
		m.map{|e|e.reduce(:+)}+
		m.transpose.map{|e|e.reduce(:+)}
	).group_by{|e|e}
end

n=gets.to_i
if n==1
	p 1
else
	m=$<.map{|e|e.split.map(&:to_i)}
	a=chk(m)
	if a.size!=2
		p -1
	else
		k=-a.map(&:first).minmax.reduce(:-)
		n.times{|i|n.times{|j|
			m[i][j]=k if m[i][j]==0
		}}
		a=chk(m)
		p a.size!=2 ? -1 : k
	end
end

# http://codeforces.com/contest/711/problem/B
n = gets.chomp.to_i
rows = [].tap do |a|
  n.times { a << gets.chomp.split.map(&:to_i) }
end
arr = rows.tap do |a|
  # cols
  (0...n).each { |i| a << (0...n).map { |j| rows[j][i] } }
  # diagonal
  a << (0...n).map { |i| rows[i][i] }
  a << (0...n).map { |i| rows[n - i - 1][i] }
end
not_filled, filled = arr.partition { |a| a.include?(0) }
sum_of_not_filled = not_filled.map { |a| a.inject(0, :+) }.uniq
sum_of_filled = filled.map { |a| a.inject(0, :+) }.uniq
if n == 1
  puts 1
elsif sum_of_filled.length == 1 && sum_of_not_filled.length == 1
  puts sum_of_filled.first - sum_of_not_filled.first
else
  puts -1
end

def magic_grid?( grid )
    n = grid.length
    sum_rows = Array.new(n, 0)
    sum_cols = Array.new(n, 0)
    sum_diag_left = sum_diag_right = 0
    n.times do |i|
        n.times do |j|
            sum_rows[i] += grid[i][j]
            sum_cols[i] += grid[j][i]
        end
        sum_diag_right += grid[i][i]
        sum_diag_left += grid[i][n - i - 1]
    end
    return false if sum_rows.uniq.length != 1 || sum_cols.uniq.length != 1
    sum_cols[0] == sum_rows[0] && sum_rows[0] == sum_diag_left && sum_diag_left == sum_diag_right
end
n = gets.strip.to_i
grid = Array.new(n) { Array.new(n, 0) }
missing_pos = Array.new(2, 0)
in_left_diag = false
in_right_diag = false
n.times do |i|
    grid[i] = gets.strip.split.map(&:to_i)
    j = grid[i].each_index.select { |z| grid[i][z] == 0 } [0]
    if !j.nil?
        in_right_diag = true if i == j
        in_left_diag = true if n - i - 1 == j
        missing_pos[0] = i
        missing_pos[1] = j
    end
end
sum_rows = Array.new(n, 0)
sum_cols = Array.new(n, 0)
sum_diag_left = sum_diag_right = 0
n.times do |i|
    n.times do |j|
        sum_rows[i] += grid[i][j]
        sum_cols[i] += grid[j][i]
    end
    sum_diag_right += grid[i][i]
    sum_diag_left += grid[i][n - i - 1]
end
x, y = missing_pos
sum_req = 0
if in_left_diag && in_right_diag
    if x == 0
        sum_req = sum_rows[n - 1]
    elsif x == n - 1
        sum_req = sum_rows[0]
    else
        sum_req = sum_rows[x - 1]
    end
    ans = sum_req - sum_diag_left
elsif in_right_diag
    sum_req = sum_diag_left
    ans = sum_req - sum_diag_right
elsif in_left_diag
    sum_req = sum_diag_right
    ans = sum_req - sum_diag_left
else
    sum_req = sum_diag_left
    ans = sum_req - sum_rows[x]
end
grid[x][y] = ans
if magic_grid? grid
    puts ans
else
    puts "-1"
end

def magic_grid?( grid )
    n = grid.length
    sum_rows = Array.new(n, 0)
    sum_cols = Array.new(n, 0)
    sum_diag_left = sum_diag_right = 0
    n.times do |i|
        n.times do |j|
            sum_rows[i] += grid[i][j]
            sum_cols[i] += grid[j][i]
        end
        sum_diag_right += grid[i][i]
        sum_diag_left += grid[i][n - i - 1]
    end
    return false if sum_rows.uniq.length != 1 || sum_cols.uniq.length != 1
    sum_cols[0] == sum_rows[0] && sum_rows[0] == sum_diag_left && sum_diag_left == sum_diag_right
end
n = gets.strip.to_i
grid = Array.new(n) { Array.new(n, 0) }
missing_pos = Array.new(2, 0)
in_left_diag = false
in_right_diag = false
if n == 1
    puts 1
else
    n.times do |i|
        grid[i] = gets.strip.split.map(&:to_i)
        j = grid[i].each_index.select { |z| grid[i][z] == 0 } [0]
        if !j.nil?
            in_right_diag = true if i == j
            in_left_diag = true if n - i - 1 == j
            missing_pos[0] = i
            missing_pos[1] = j
        end
    end
    sum_rows = Array.new(n, 0)
    sum_cols = Array.new(n, 0)
    sum_diag_left = sum_diag_right = 0
    n.times do |i|
        n.times do |j|
            sum_rows[i] += grid[i][j]
            sum_cols[i] += grid[j][i]
        end
        sum_diag_right += grid[i][i]
        sum_diag_left += grid[i][n - i - 1]
    end
    x, y = missing_pos
    sum_req = 0
    if in_left_diag && in_right_diag
        if x == 0
            sum_req = sum_rows[n - 1]
        elsif x == n - 1
            sum_req = sum_rows[0]
        else
            sum_req = sum_rows[x - 1]
        end
        ans = sum_req - sum_diag_left
    elsif in_right_diag
        sum_req = sum_diag_left
        ans = sum_req - sum_diag_right
    elsif in_left_diag
        sum_req = sum_diag_right
        ans = sum_req - sum_diag_left
    else
        sum_req = sum_diag_left
        ans = sum_req - sum_rows[x]
    end
    grid[x][y] = ans
    if magic_grid? grid
        puts ans
    else
        puts "-1"
    end
end

def magic_grid?( grid )
    n = grid.length
    sum_rows = Array.new(n, 0)
    sum_cols = Array.new(n, 0)
    sum_diag_left = sum_diag_right = 0
    n.times do |i|
        n.times do |j|
            sum_rows[i] += grid[i][j]
            sum_cols[i] += grid[j][i]
        end
        sum_diag_right += grid[i][i]
        sum_diag_left += grid[i][n - i - 1]
    end
    return false if sum_rows.uniq.length != 1 || sum_cols.uniq.length != 1
    sum_cols[0] == sum_rows[0] && sum_rows[0] == sum_diag_left && sum_diag_left == sum_diag_right
end
n = gets.strip.to_i
grid = Array.new(n) { Array.new(n, 0) }
missing_pos = Array.new(2, 0)
in_left_diag = false
in_right_diag = false
if n == 1
    puts 1
else
    n.times do |i|
        grid[i] = gets.strip.split.map(&:to_i)
        j = grid[i].each_index.select { |z| grid[i][z] == 0 } [0]
        if !j.nil?
            in_right_diag = true if i == j
            in_left_diag = true if n - i - 1 == j
            missing_pos[0] = i
            missing_pos[1] = j
        end
    end
    sum_rows = Array.new(n, 0)
    sum_cols = Array.new(n, 0)
    sum_diag_left = sum_diag_right = 0
    n.times do |i|
        n.times do |j|
            sum_rows[i] += grid[i][j]
            sum_cols[i] += grid[j][i]
        end
        sum_diag_right += grid[i][i]
        sum_diag_left += grid[i][n - i - 1]
    end
    x, y = missing_pos
    sum_req = 0
    if in_left_diag && in_right_diag
        if x == 0
            sum_req = sum_rows[n - 1]
        elsif x == n - 1
            sum_req = sum_rows[0]
        else
            sum_req = sum_rows[x - 1]
        end
        ans = sum_req - sum_diag_left
    elsif in_right_diag
        sum_req = sum_diag_left
        ans = sum_req - sum_diag_right
    elsif in_left_diag
        sum_req = sum_diag_right
        ans = sum_req - sum_diag_left
    else
        sum_req = sum_diag_left
        ans = sum_req - sum_rows[x]
    end
    grid[x][y] = ans
    if magic_grid? grid
        if ans == 0
            puts "-1"
        else
            puts ans
        end
    else
        puts "-1"
    end
end

def magic_grid?( grid )
    n = grid.length
    sum_rows = Array.new(n, 0)
    sum_cols = Array.new(n, 0)
    sum_diag_left = sum_diag_right = 0
    n.times do |i|
        n.times do |j|
            sum_rows[i] += grid[i][j]
            sum_cols[i] += grid[j][i]
        end
        sum_diag_right += grid[i][i]
        sum_diag_left += grid[i][n - i - 1]
    end
    return false if sum_rows.uniq.length != 1 || sum_cols.uniq.length != 1
    sum_cols[0] == sum_rows[0] && sum_rows[0] == sum_diag_left && sum_diag_left == sum_diag_right
end
n = gets.strip.to_i
grid = Array.new(n) { Array.new(n, 0) }
missing_pos = Array.new(2, 0)
in_left_diag = false
in_right_diag = false
n.times do |i|
    grid[i] = gets.strip.split.map(&:to_i)
    j = grid[i].each_index.select { |z| grid[i][z] == 0 } [0]
    if !j.nil?
        in_right_diag = true if i == j
        in_left_diag = true if n - i - 1 == j
        missing_pos[0] = i
        missing_pos[1] = j
    end
end
sum_rows = Array.new(n, 0)
sum_cols = Array.new(n, 0)
sum_diag_left = sum_diag_right = 0
n.times do |i|
    n.times do |j|
        sum_rows[i] += grid[i][j]
        sum_cols[i] += grid[j][i]
    end
    sum_diag_right += grid[i][i]
    sum_diag_left += grid[i][n - i - 1]
end
x, y = missing_pos
sum_req = 0
if in_left_diag && in_right_diag
    if x == 0
        sum_req = sum_rows[n - 1]
    elsif x == n - 1
        sum_req = sum_rows[0]
    else
        sum_req = sum_rows[x - 1]
    end
    ans = sum_req - sum_diag_left
elsif in_right_diag
    if x == 0
        sum_req = sum_rows[n - 1]
    elsif x == n - 1
        sum_req = sum_rows[0]
    else
        sum_req = sum_rows[x - 1]
    end
    ans = sum_req - sum_diag_right
elsif in_left_diag
    if x == 0
        sum_req = sum_rows[n - 1]
    elsif x == n - 1
        sum_req = sum_rows[0]
    else
        sum_req = sum_rows[x - 1]
    end
    ans = sum_req - sum_diag_left
else
    sum_req = sum_diag_left
    ans = sum_req - sum_rows[x]
end
grid[x][y] = ans
if magic_grid? grid
    puts ans
else
    puts "-1"
end

def mysum(a)
  nozero = true
  sum = 0
  a.each do |e|
    if e != 0
      sum += e
    else
      nozero = false
      break
    end
  end
  if nozero
    return sum
  else
    return -1
  end
end

def realsum(a)
  sum = 0
  a.each do |e|
    sum += e
  end
  return sum
end

def checkmat(mat, num)
  valid = true
  for i in 0..(mat.length-1) do
    rowsum = 0
    for ii in 0..(mat[i].length-1) do
      rowsum += mat[i][ii]
    end
    if rowsum != num
      valid = false
      break
    end
  end
  if not valid
    return false
  end
  for i in 0..(mat[0].length-1) do
    colsum = 0
    for ii in 0..(mat.length-1) do
      colsum += mat[ii][i]
    end
    if colsum != num
      valid = false
      break
    end
  end
  if not valid
    return false
  end
  dia1 = 0
  for i in 0..(mat.length-1) do
    dia1 += mat[i][i]
  end
  if dia1 != num
    return false
  end
  dia2 = 0
  for i in 0..(mat.length-1) do
    dia2 += mat[i][mat.length-1-i]
  end
  if dia2 != num
    return false
  end
  return true
end

n = gets.strip.to_i
matris = []
n.times do
  input = gets.strip.split(' ').map(&:to_i)
  matris << input
end

if n == 1
  puts matris[0][0]
else
  thesum = 0
  zeroline = -1
  for i in 0..(matris.length-1) do
    x = mysum(matris[i])
    if x != -1
      thesum = x
      if zeroline != -1
        break
      end
    else
      zeroline = i
      next
    end
  end

  lol = realsum(matris[zeroline])
  trol = thesum - lol
  if trol > 0
    for i in 0..(matris[zeroline].length-1) do
      if matris[zeroline][i] == 0
        matris[zeroline][i] = trol
        break
      end
    end
    if checkmat(matris, thesum)
      puts trol
    else
      puts -1
    end
  else
    puts -1
  end
end

def mysum(a)
  nozero = true
  sum = 0
  a.each do |e|
    if e != 0
      sum += e
    else
      nozero = false
      break
    end
  end
  if nozero
    return sum
  else
    return -1
  end
end

def realsum(a)
  sum = 0
  a.each do |e|
    sum += e
  end
  return sum
end

def checkmat(mat, num)
  valid = true
  for i in 0..(mat.length-1) do
    rowsum = 0
    for ii in 0..(mat[i].length-1) do
      rowsum += mat[i][ii]
    end
    if rowsum != num
      valid = false
      break
    end
  end
  if not valid
    return false
  end
  for i in 0..(mat[0].length-1) do
    colsum = 0
    for ii in 0..(mat.length-1) do
      colsum += mat[i][ii]
    end
    if colsum != num
      valid = false
      break
    end
  end
  if not valid
    return false
  end
  dia1 = 0
  for i in 0..(mat.length-1) do
    dia1 += mat[i][i]
  end
  if dia1 != num
    return false
  end
  dia2 = 0
  for i in 0..(mat.length-1) do
    dia2 += mat[i][mat.length-1-i]
  end
  if dia2 != num
    return false
  end
  return true
end

n = gets.strip.to_i
matris = []
n.times do
  input = gets.strip.split(' ').map(&:to_i)
  matris << input
end

if n == 1
  puts matris[0][0]
else
  thesum = 0
  zeroline = -1
  for i in 0..(matris.length-1) do
    x = mysum(matris[i])
    if x != -1
      thesum = x
      if zeroline != -1
        break
      end
    else
      zeroline = i
      next
    end
  end

  lol = realsum(matris[zeroline])
  trol = thesum - lol
  if trol > 0
    for i in 0..(matris[zeroline].length-1) do
      if matris[zeroline][i] == 0
        matris[zeroline][i] = trol
        break
      end
    end
    if checkmat(matris, thesum)
      puts trol
    else
      puts -1
    end
  else
    puts -1
  end
end

def func(a, n, s, t)
    a.each do |v|
        if v.include?(0)
            s<<v.inject(:+)
        else
            t<<v.inject(:+)
        end
    end

    x=0
    f=false
    n.times do |i|
        x+=a[i][i]
        f=true if !f and a[i][i]==0
    end
    f ? s<<x : t<<x
end

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

s=[]
t=[]
func(a, n, s, t)
func(a.transpose, n, s, t)

if s.uniq.size>1 or t.uniq.size>1 or s[0]>=t[0]
    p -1
else
    p s[0]-t[0]
end

def func(a, n, s, t)
    a.each{|v|
        v.include?(0) ?  s<<v.inject(:+) : t<<v.inject(:+)
    }

    x=0
    f=false
    n.times{|i|
        x+=a[i][i]
        f=true if !f and a[i][i]==0
    }
    f ? s<<x : t<<x
end

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

s=[]
t=[]
func(a, n, s, t)
func(a.transpose, n, s, t)

if n==1
    p 1
elsif s.uniq.size>1 or t.uniq.size>1 or s[0]>=t[0]
    p -1
else
    p t[0]-s[0]
end

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
summed = a.map{|x| x.inject(&:+)}.sort
possible = true
if n == 1
  possible = false
elsif !(summed.uniq.size == 2 && summed.last - summed.first > 0)
  possible = false
else
  possible = n.times.map{|x| a[x][x] == 0 ? summed.last - summed.first : a[x][x]}.inject(&:+) == n.times.map{|x| a[x][n - x - 1] == 0 ? summed.last - summed.first : a[x][n - x - 1]}.inject(&:+)
end
puts possible ? summed.last - summed.first : -1

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
summed = a.map{|x| x.inject(&:+)}.sort
possible = true
if n == 1
  possible = false
elsif summed.uniq.size != 2
  possible = false
elsif summed.last - summed.first <= 0
  possible = false
else
  possible = ( n.times.map{|x| a[x][x] == 0 ? summed.last - summed.first : a[x][x]}.inject(&:+) == n.times.map{|x| a[x][n - x - 1] == 0 ? summed.last - summed.first : a[x][n - x - 1]}.inject(&:+) )
end
puts possible ? summed.last - summed.first : -1

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
row, col = Array.new(n, 0), Array.new(n, 0)
n.times do |i|
  n.times do |j|
    row[i] += a[i][j]
    col[j] += a[i][j]
  end
end
guess1 = row.uniq.sort.last - row.uniq.sort.first
guess2 = col.uniq.sort.last - col.uniq.sort.first
dia1 = n.times.map{|x| a[x][x] == 0 ? guess1 : a[x][x]}.inject(&:+)
dia2 = n.times.map{|x| a[x][n - 1 - x] == 0 ? guess1 : a[x][n - 1 - x]}.inject(&:+)
if n == 1
  puts 1
else
  puts row.uniq.size == 2 && col.uniq.size == 2 && guess1 == guess2 && dia1 == dia2 ? guess1 : -1
end

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
summed = a.map{|x| x.inject(&:+)}.sort
if n == 1
  puts 1
else
  puts summed.uniq.size == 2 && summed.last - summed.first > 0 ? summed.last - summed.first : -1
end

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
row, col = Array.new(n, 0), Array.new(n, 0)
n.times do |i|
  n.times do |j|
    row[i] += a[i][j]
    col[j] += a[i][j]
  end
end
guess1 = row.uniq.sort.last - row.uniq.sort.first
guess2 = col.uniq.sort.last - col.uniq.sort.first
dia1 = n.times.map{|x| a[x][x] == 0 ? guess1 : a[x][x]}.inject(&:+)
dia2 = n.times.map{|x| a[x][n - 1 - x] == 0 ? guess1 : a[x][n - 1 - x]}.inject(&:+)
if n == 1
  puts 1
else
  puts guess1 == guess2 && dia1 == dia2 ? guess1 : -1
end

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
summed = a.map{|x| x.inject(&:+)}.sort
puts summed.uniq.size == 2 && summed.last - summed.first > 0 ? summed.last - summed.first : -1

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
sum_row, sum_col, sum_dia = Array.new(n, 0), Array.new(n, 0), Array.new(2, 0)
x, y = 0, 0
n.times do |i|
  n.times do |j|
    sum_row[i] += a[i][j]
    sum_col[j] += a[i][j]
    sum_dia[0] += a[i][j] if i == j
    sum_dia[1] += a[i][j] if i + j == n - 1
    x, y = i, j if a[i][j] == 0
  end
end
candidate = ( sum_row.uniq.last - sum_row.uniq.first ).abs
if n == 1
  puts 1
else
  sum_row[x] += candidate
  sum_col[y] += candidate
  sum_dia[0] += candidate if x == y
  sum_dia[1] += candidate if x + y == n - 1
  puts sum_row.uniq.size == 1 && sum_col.uniq.size == 1 && sum_dia.uniq.size == 1 && candidate != 0 ? candidate : -1
end

n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
sum_row, sum_col, sum_dia = Array.new(n, 0), Array.new(n, 0), Array.new(2, 0)
x, y = 0, 0
n.times do |i|
  n.times do |j|
    sum_row[i] += a[i][j]
    sum_col[j] += a[i][j]
    sum_dia[0] += a[i][j] if i == j
    sum_dia[1] += a[i][j] if i + j == n - 1
    x, y = i, j if a[i][j] == 0
  end
end
candidate = ( sum_row.uniq.last - sum_row.uniq.first ).abs
if n == 1
  puts 1
else
  sum_row[x] += candidate
  sum_col[y] += candidate
  sum_dia[0] += candidate if x == y
  sum_dia[1] += candidate if x + y == n - 1
  puts sum_row.uniq.size == 1 && sum_col.uniq.size == 1 && sum_dia.uniq.size == 1 ? candidate : -1
end

n = gets.to_i
a = Array.new(n)
w = 0
h = 0
n.times{|i|
    a[i] = gets.split.map(&:to_i)
    n.times{|j|
        if a[i][j] == 0
            w = j
            h = i
        end
        }
    }
sum = 0
if h == 0
    if n == 1
        p 1
        exit
    end
    sum = a[1].inject(:+)
else
    sum = a[0].inject(:+)
end
tmp = a[h].inject(:+)
if tmp == a.transpose[w].inject(:+) && sum > tmp
    p sum - tmp
else
    p -1
end

n = gets.to_i
a = Array.new(n)
w = 0
h = 0
s1 = 0
s2 = 0
n.times{|i|
    a[i] = gets.split.map(&:to_i)
    n.times{|j|
        if a[i][j] == 0
            w = j
            h = i
        end
        if i == j
            s1 += a[i][j]
        end
        if i + j == n - 1
            s2 += a[i][j]
        end
        }
    }
sum = 0
if h == 0
    if n == 1
        p 1
        exit
    end
    sum = a[1].inject(:+)
else
    sum = a[0].inject(:+)
end
tmp1 = a[h].inject(:+)
tmp2 = a.transpose[w].inject(:+)
if tmp1 == tmp2 && sum > tmp1
    if w == h && s1 != tmp1
        p -1
        exit
    end
    if w + h == n - 1 && s2 != tmp1
        p -1
        exit
    end
    p sum - tmp1
else
    p -1
end

n = gets.to_i
a = Array.new(n)
w = 0
h = 0
s1 = 0
s2 = 0
b = Array.new(n, 0)
c = Array.new(n, 0)
n.times{|i|
    a[i] = gets.split.map(&:to_i)
    n.times{|j|
        if a[i][j] == 0
            w = j
            h = i
        end
        if i == j
            s1 += a[i][j]
        end
        if i + j == n - 1
            s2 += a[i][j]
        end
        b[i] += a[i][j]
        c[j] += a[i][j]
        }
    }
if h == 0
    if n == 1
        p 1
        exit
    end
    sum = b[1]
else
    sum = b[0]
end
n.times{|i|
    if i != h && sum != b[i]
        p -1
        exit
    end
    }
n.times{|i|
    if i != w && sum != c[i]
        p -1
        exit
    end
    }
tmp1 = b[h]
tmp2 = c[w]
if tmp1 == tmp2 && sum > tmp1
    if w == h && s1 != tmp1
        p -1
        exit
    end
    if w + h == n - 1 && s2 != tmp1
        p -1
        exit
    end
    p sum - tmp1
else
    p -1
end

n = gets.to_i
a = Array.new(n)
w = 0
h = 0
s1 = 0
s2 = 0
b = Array.new(n, 0)
c = Array.new(n, 0)
n.times{|i|
    a[i] = gets.split.map(&:to_i)
    n.times{|j|
        if a[i][j] == 0
            w = j
            h = i
        end
        if i == j
            s1 += a[i][j]
        end
        if i + j == n - 1
            s2 += a[i][j]
        end
        b[i] += a[i][j]
        c[j] += a[i][j]
        }
    }
if h == 0
    if n == 1
        p 1
        exit
    end
    sum = b[1]
else
    sum = b[0]
end
n.times{|i|
    if i != h && sum != b[i]
        p -1
        exit
    end
    }
n.times{|i|
    if i != w && sum != c[i]
        p -1
        exit
    end
    }
tmp1 = b[h]
tmp2 = c[w]
if tmp1 == tmp2 && sum > tmp1
    if w == h && s1 != tmp1 || s1 != sum
        p -1
        exit
    end
    if w + h == n - 1 && s2 != tmp1 || s2 != sum
        p -1
        exit
    end
    p sum - tmp1
else
    p -1
end

n=readline.to_i
a = []
found = false
(1..n).each do
	ai = readline.split.map(&:to_i)
	a << ai
end

filled = []
not_filled = []
(0...n).each do |i|
	row = a[i]
	if row.include? 0
		not_filled << row.reduce(&:+)
	else
		filled << row.reduce(&:+)
	end

	col = (0...n).map{|j| a[j][i]}
	if col.include? 0
		not_filled << col.reduce(&:+)
	else
		filled << col.reduce(&:+)
	end
end

dia_1 = (0...n).map{|j| a[j][j]}
dia_2 = (0...n).map{|j| a[n-j-1][j]}

if dia_1.include? 0
	not_filled << dia_1.reduce(&:+)
else
	filled << dia_1.reduce(&:+)
end

if dia_2.include? 0
	not_filled << dia_2.reduce(&:+)
else
	filled << dia_2.reduce(&:+)
end

filled.uniq!
not_filled.uniq!
if filled.size == 1 && not_filled.size == 1 && filled[0] > not_filled[0]
	puts filled[0]-not_filled[0]
else
	puts -1
end

n=readline.to_i
a = []
found = false
(1..n).each do
	ai = readline.split.map(&:to_i)
	a << ai
end

filled = []
not_filled = []
(0...n).each do |i|
	row = a[i]
	if row.include? 0
		not_filled << row.reduce(&:+)
	else
		filled << row.reduce(&:+)
	end

	col = (0...n).map{|j| a[j][i]}
	if col.include? 0
		not_filled << col.reduce(&:+)
	else
		filled << col.reduce(&:+)
	end
end

dia_1 = (0...n).map{|j| a[j][j]}
dia_2 = (0...n).map{|j| a[n-j-1][j]}

if dia_1.include? 0
	not_filled << dia_1.reduce(&:+)
else
	filled << dia_1.reduce(&:+)
end

if dia_2.include? 0
	not_filled << dia_2.reduce(&:+)
else
	filled << dia_2.reduce(&:+)
end

filled.uniq!
not_filled.uniq!
if filled.size == 1 && not_filled.size == 1
	puts filled[0]-not_filled[0]
else
	puts -1
end


def rot(a)
  a.push(a.shift)
end
def already(h,a)
  h.include?(a) || h.include?(rot(a)) || h.include?(rot(rot(a))) || h.include?(rot(rot(rot(a))))
end

n=gets.to_i
a=Array.new(4)
h={}
cnt=0
n.times{|x|
	a[0]=gets(1)
	a[1]=gets(1)
	gets
	a[3]=gets(1)
	a[2]=gets(1)
	gets
	if x!=n-1 then gets; end
	if !already(h,a) then
		cnt+=1
	    h[a]=true
	end
}
puts cnt

def r(s)
  s[1..-1] + s[0..0]
end

n = gets.to_i

a = Array.new(n) do |i|
  s = gets[0..1] + gets[0..1].reverse
  gets if i != n - 1
  s
end

a = a.map do |s|
  [s, r(s), r(r(s)), r(r(r(s)))].min
end

puts a.uniq.count

def rotate(amulet)
  a = amulet[0][0]
  b = amulet[0][1]
  c = amulet[1][0]
  d = amulet[1][1]

  [[c, a], [d, b]]
end
def equal(a, b)
  if a == b
    true
  elsif rotate(a) == b || rotate(rotate(a)) == b || rotate(rotate(rotate(a))) == b
    true
  else
    false
  end
end

number = gets.chomp.to_i

amulets = []
number.times {|i|
  str = gets.chomp
  a = str[0..0]
  b = str[1..1]
  str = gets.chomp
  c = str[0..0]
  d = str[1..1]
  amulets << [[a, b], [c, d]]
  gets if not i == number - 1
}

piles = []
amulets.each {|amulet|
  if index = piles.index {|item| equal(item[0], amulet)}
    piles[index] << amulet
  else
    piles << [amulet]
  end
}

puts piles.length

amulets, piles = [], []
n = STDIN.gets.to_i

n.times do
  li1, li2, sep = STDIN.gets.chomp, STDIN.gets.chomp.reverse, STDIN.gets
  amulets << li1 + li2
end

amulets.each do |amu|
  4.times do |n|
    amu_rotate = (amu * 2)[n, 4]
    break if piles.include?(amu_rotate)
    piles << amu if n == 3
  end
end

puts piles.count

amulets = []
piles = {}

n = STDIN.gets
n.to_i.times do
  line1 = STDIN.gets.chomp
  line2 = STDIN.gets.chomp.reverse
  sep   = STDIN.gets

  amulets << line1 + line2
end

puts 1

def rot(a)
  a[0],a[1],a[2],a[3] = a[2],a[0],a[3],a[1]
end
def already(h,a)
  h.include?(a) || h.include?(rot(a)) || h.include?(rot(rot(a))) || h.include?(rot(rot(rot(a))))
end

n=gets.to_i
a=Array.new(4)
h={}
cnt=0
n.times{|x|
	a[0]=gets(1)
	a[1]=gets(1)
	gets
	a[2]=gets(1)
	a[3]=gets(1)
	gets
	if x!=n-1 then gets; end
	if !already(h,a) then
		cnt+=1
	    h[a]=true
	end
}
puts cnt

def rot(a)
  a[0],a[1],a[2],a[3] = a[2],a[0],a[2],a[1]
end
def already(h,a)
  h.include?(a) || h.include?(rot(a)) || h.include?(rot(rot(a))) || h.include?(rot(rot(rot(a))))
end

n=gets.to_i
a=Array.new(4)
h={}
cnt=0
n.times{|x|
	a[0]=gets(1)
	a[1]=gets(1)
	gets
	a[2]=gets(1)
	a[3]=gets(1)
	gets
	if x!=n-1 then gets; end
	if !already(h,a) then
		cnt+=1
	    h[a]=true
	end
}
puts cnt

def rot(a)
  a.push(a.shift)
end
def already(h,a)
  h.include?(a) || h.include?(rot(a)) || h.include?(rot(rot(a))) || h.include?(rot(rot(rot(a))))
end

n=gets.to_i
a=Array.new(4)
h={}
cnt=0
n.times{|x|
	gets
	a[0]=gets(1)
	a[1]=gets(1)
	gets
	a[2]=gets(1)
	a[3]=gets(1)
	gets
	if !already(h,a) then
		cnt+=1
	    h[a]=true
	end
}
puts cnt

def rotate(amulet)
  a = amulet[0][0]
  b = amulet[0][1]
  c = amulet[1][0]
  d = amulet[1][1]

  [[c, a], [d, b]]
end
def equal(a, b)
  if a == b
    true
  elsif rotate(a) == b || rotate(rotate(a)) == b || rotate(rotate(rotate(a))) == b
    true
  else
    false
  end
end

number = gets.chomp.to_i

amulets = []
number.times {|i|
  str = gets.chomp
  a = str[0..0]
  b = str[1..1]
  str = gets.chomp
  c = str[0..0]
  d = str[1..1]
  amulets << [[a, b], [c, d]]
  gets if not i == number - 1
}

piles = []
amulets.each {|amulet|
  if index = piles.index {|item| equal(item[0], amulet)}
    piles[index] << amulet
  else
    piles << [amulet]
  end
}

puts piles.select {|pile| pile.length != 1}.length


def solve(n, k, a)
  type = -1
  ans = -1
  for i in 0...k.to_i
    cmp = (n.to_i / a[i].to_i) * a[i].to_i
    if cmp > ans
      ans = cmp
      type = i
    end
  end

  ret = []
  ret.push(type.to_i + 1)
  ret.push(n.to_i / a[type.to_i].to_i)
  return ret;
end

n, k = gets.split(&:to_i)
a = gets.split(&:to_i)

ans = solve(n, k, a)
print ans[0].to_s + " " + ans[1].to_s

m, n = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).shift(n)
ans = [0]
a.each_with_index do |i, j|
  ans = [ans, [m - m % i, j + 1, m / i]].max
end
puts "#{ans[1]} #{ans[2]}"

#Hamster farm

inp = gets.chomp.split().map(&:to_i)
k = inp[0] ; n = inp[1]
inp = gets.chomp.split().map(&:to_i)
remain = inp.map { |e| k%e }
ind = remain.each_with_index.min[1]
model = ind + 1
num = k/inp[ind]
puts [model ,num]


def binary_search(range)
  lower = 0
  upper = range.size
  while lower < upper
    mi = (lower + upper)/2
    yield(range[mi]) ? upper = mi : lower = mi + 1
  end
  [range[upper], upper]
end

n = gets.to_i
p = gets.split.map(&:to_i).sort
q = gets.to_i
(0...q).each do |i|
  b = gets.to_i
  puts binary_search(p){ |j| j > b}[1]
end

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                @shopNum = Integer(line)
            when 1
                @priceArr = line.split(" ")
                @priceArr = @priceArr.map{ |i| i = Integer(i) }
                @priceArr.sort!
            when 2
                @dayNum = Integer(line)
            else
                coin = Integer(line)
                puts searchResult(coin,0,@shopNum-1)
            end
        end
    end

    def searchResult(coin,lo,hi)
        while lo <= hi do
            mid = lo + (hi-lo)/2
            if coin < @priceArr[mid]
                hi = mid - 1
            elsif coin == @priceArr[mid]
                rtn = mid + 1;
                lo = mid + 1;
            else
                lo = mid + 1
            end
        end

        return rtn ? rtn : (coin > @priceArr[mid] ? mid + 1 : mid)
    end
end

main = Program.new(ARGF)

max1=100000
c=(0..max1).map{0}
gets
x=gets.split.map{|e| e.to_i}
x.each{|e|
	c[e]+=1
}

sum=0
0.upto(max1){|i|
	sum+=c[i]
	c[i]=sum
}
gets.to_i.times{
	n=gets.to_i
	if n>=max1 then
		puts c[max1]
	else
		puts c[n]
	end
}

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i

prev = -1
prices = gets.split.map(&:to_i)
days_staying = gets.to_i
last_pos = {}
prices.sort!
prev = -1
prices.each_with_index { |item, index|
    if prev != item
        prev = item
    end

    last_pos[prev] = index
}

days_staying.times {
    coins = gets.to_i

    # binary search (end will be exclusive b <= i < e)
    b, e = 0, prices.length
    # p last_pos # index test
    loop {
        mid = (b + e) / 2
        midval = prices[mid]
        # p [b, e, mid, coins]
        if midval < coins
            # p '#1'
            if (e - b) <= 1
                puts e
                break
            end

            b = mid
        elsif midval == coins
            # p '#2'
            puts last_pos[midval] + 1
            break
        else
            # p '#3'

            if (e - b) <= 1
                puts b
                break
            end

            e = mid
        end
    }
}

n = gets.chomp.to_i
x = gets.chomp.split.map(&:to_i).sort
q = gets.chomp.to_i
m = []
for i in (0..q-1)
	m[i] = gets.chomp.to_i
end

for i in (0..q-1)
	first = 0
	last = n-1
	mid = (first + last)/2
	while first < last
		if x[mid] <= m[i]
			first = mid+1
		else
			last = mid-1
		end
		mid = (first+last)/2
	end
	if x[mid] <= m[i]
		puts mid+1
	else
		if mid < 0
			puts 0
		else
			puts mid
		end
	end
end

n = gets.to_i
arr = gets.split(' ').map {|a| a.to_i}.sort!
gets.to_i.times do
	c = gets.to_i
	l = 0
	h = n-1
	while l<=h
		m = (l+h)/2
		if arr[m]>c then h = m - 1 else l = m + 1 end
	end
	puts l
end

n = gets.to_i
arr = gets.split(' ').map {|a| a.to_i}.sort!
gets.to_i.times do
	c = gets.to_i
	l = 0
	h = n-1
	17.times do
		m = (l+h)/2
		if arr[m]>c then h = m - 1 else l = m + 1 end
	end
	puts l
end

n = gets.to_i
arr = gets.split(' ').map {|a| a.to_i}.sort!
gets.to_i.times do
	c = gets.to_i
	l = 0
	h = n-1
	64.times do
		m = (l+h)/2
		if arr[m]>c then h = m - 1 else l = m + 1 end
	end
	puts l
end

n = gets.to_i
prices = gets.split.map(&:to_i).sort!
days = gets.to_i

for i in 0..(days - 1)
  money = gets.to_i
  left = 0
  right = n - 1

  while left != right do
    middle = (left + right) / 2

    if prices[middle] <= money
      left = middle + 1
    else
      right = middle
    end

  end

  if prices[left] <= money
      puts left + 1
  else
      puts left
  end
end

answer = {}
n = gets.to_i
prices = gets.split.map(&:to_i)
days = gets.to_i

prices.sort!

prices.each_with_index do |x, index|
    answer[x] = index if answer[x].nil?
end

days.times do
   money = gets.to_i
   value = prices.bsearch{ |x| x > money }
   puts value.nil? ? n : answer[value]
end

n = gets.to_i
prices = gets.split.map(&:to_i).sort!
days = gets.to_i

for i in 0..(days - 1)
  money = gets.to_i
  left = 0
  right = n - 1

  while left != right do
    middle = (left + right) / 2

    if prices[middle] < money
      left = middle + 1
    elsif prices[middle] > money
      right = middle
    else
      left = middle + 1
    end
  end

  if prices[left] <= money
      puts left + 1
  else
      puts left
  end
end

def bs(a, t, l, r)
  return l if l == r
  if a[(l + r) / 2] > t
    bs(a, t, l, (l + r) / 2)
  else
    bs(a, t, (l + r) / 2 + 1, r)
  end
end

n = gets.to_i
a = gets.split.map(&:to_i).sort
m = gets.to_i
ans = []
m.times do
  t = gets.to_i
  ans << bs(a, t, 0, n)
end
puts ans * "\n"

n = gets.to_i
a = gets.split.map(&:to_i).sort
m = gets.to_i
ans = []
m.times do
  t = gets.to_i
  ans << ((0...n).bsearch{|i| a[i] > t} || n)
end
puts ans * "\n"

n = gets.chomp.to_i
a = gets.split.map(&:to_i).sort

q = gets.chomp.to_i
1.upto(q) do
	m = gets.chomp.to_i
	l = 0
	r = n-1

	while (r-l > 1) do
		if a[(r+l)/2]>m then
			r = (r+l)/2
		else
			l = (r+l)/2
		end
	end

	result = 0
	if a[l]<=m then
		if a[r]>m then
			result = r
		else
			result = n
		end
	end

	puts result
end

shopNum = gets.chomp.to_i
shopPrices = gets.chomp.split(' ').map(&:to_i)
dayNum = gets.chomp.to_i

shopPrices.sort!
indexHash = {}
duplicate = {}

shopPrices.each_with_index do |p, i|
	indexHash[p] = i + 1

	if duplicate.has_key? p
		duplicate[p] += 1
	else
		duplicate[p] = 0
	end
end

dayNum.times do
	coin = gets.chomp.to_i
	idx = shopPrices.bsearch { |x| x >= coin }

	#puts "idx : #{idx}"

	if idx.nil?
		puts shopPrices.length
	else
		if coin < idx
			puts indexHash[idx] - 1 - duplicate[idx]
		else
			puts indexHash[idx]
		end
	end
end

n = gets.to_i
mas = gets.split.map(&:to_i)
q = gets.to_i

mas.sort!

ans = {}

cnt = 0
mas.each do |x|
    ans[x] = cnt if ans[x].nil?
    cnt+=1
end

q.times do
   m = gets.to_i
   f = mas.bsearch{|x| x > m}

   puts f.nil? ? n : ans[f]
end

n = gets.chomp.to_i
hash = Hash.new { 0 }
arr = gets.chomp.split.map(&:to_i).sort
c = 0
arr.each do |x|
  hash[x] = c unless hash.key?(x)
  c += 1
end

q = gets.chomp.to_i
q.times do
  x = gets.chomp.to_i
  ans = arr.bsearch { |e| e > x }
  puts ans ? hash[ans] : n
end

c = []
for i in (1..100001)
  c.push(0)
end

n = gets.to_i()
x = gets.split(' ')
for i in x
  c[i.to_i()] += 1
end

for i in (1..100000)
  c[i] += c[i-1]
end

q = gets.to_i()
for i in (1..q)
  t = gets.to_i()
  if t > 100000
    puts c[100000]
  else
    puts (c[t])
  end
end

c = []
for i in (1..100001)
  c.push(0)
end

n = gets.to_i()
x = gets.split(' ')
for i in x
  c[i.to_i()] += 1
end

for i in (1..100000)
  c[i] += c[i-1]
end

q = gets.to_i()
for i in (1..q)
  t = gets.to_i()
  puts (c[t])
end

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                @shopNum = Integer(line)
            when 1
                @priceArr = line.split(" ")
                @priceArr = @priceArr.map{ |i| i = Integer(i) }
                @priceArr.sort!
            when 2
                @dayNum = Integer(line)
            else
                coin = Integer(line)
                p searchResult(coin,@shopNum/2)
            end
        end
    end

    def searchResult(coin,partition)

        if partition >= @shopNum - 1
            return @shopNum
        elsif @priceArr[partition] == coin
            return partition + 1
        elsif coin > @priceArr[partition] and coin < @priceArr[partition + 1]
            return partition + 1
        elsif coin < @priceArr[partition] and coin > @priceArr[partition - 1]
            return partition
        elsif coin < @priceArr[partition] and partition == 0
            return partition
        elsif @priceArr[partition] > coin
            return searchResult(coin,partition/2)
        elsif @priceArr[partition] < coin
            return searchResult(coin,(@shopNum + partition)/2 )
        end

    end
end

main = Program.new(ARGF)

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                @shopNum = Integer(line)
            when 1
                @priceArr = line.split(" ")
                @priceArr = @priceArr.map{ |i| i = Integer(i) }
                @priceArr.sort!
            when 2
                @dayNum = Integer(line)
            else
                coin = Integer(line)
                puts searchResult(coin,0,@shopNum-1)
            end
        end
    end

    def searchResult(coin,lo,hi)
        while lo <= hi do
            mid = lo + (hi-lo)/2
            if coin < @priceArr[mid]
                hi = mid - 1
            elsif coin == @priceArr[mid]
                rtn = mid + 1;
                lo = mid + 1;
            else
                lo = mid + 1
            end
        end

        return rtn ? rtn : (mid == 0 ? 0 : mid + 1)
    end
end

main = Program.new(ARGF)

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                @shopNum = Integer(line)
            when 1
                @priceArr = line.split(" ")
                @priceArr = @priceArr.map{ |i| i = Integer(i) }
                @priceArr.sort!
            when 2
                @dayNum = Integer(line)
            else
                coin = Integer(line)
                p searchResult(coin,@shopNum/2)
            end
        end
    end

    def searchResult(coin,partition)
        if partition <= 0
            return 0
        elsif partition >= @shopNum - 1
            return @shopNum
        elsif @priceArr[partition] == coin
            return partition + 1
        elsif coin > @priceArr[partition] and coin < @priceArr[partition + 1]
            return partition + 1
        elsif coin < @priceArr[partition] and coin > @priceArr[partition - 1]
            return partition
        elsif @priceArr[partition] > coin
            return searchResult(coin,partition/2)
        elsif @priceArr[partition] < coin
            return searchResult(coin,(@shopNum + partition)/2 )
        end

    end
end

main = Program.new(ARGF)

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                @shopNum = Integer(line)
            when 1
                @priceArr = line.split(" ")
                @priceArr = @priceArr.map{ |i| i = Integer(i) }
                @priceArr.sort!
            when 2
                @dayNum = Integer(line)
            else
                coin = Integer(line)
                p searchResult(coin,1,@shopNum)
            end
        end
    end

    def searchResult(coin,lo,hi)
        mid = lo+(hi-lo)/2
        if lo >= hi
            return mid
        elsif coin > @priceArr[mid-1]
            return searchResult(coin,mid+1,hi)
        elsif coin < @priceArr[mid-1]
            return searchResult(coin,lo,mid-1)
        elsif coin == @priceArr[mid-1]
            return mid
        end
    end
end

main = Program.new(ARGF)

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                @shopNum = Integer(line)
            when 1
                @priceArr = line.split(" ")
                @priceArr = @priceArr.map{ |i| i = Integer(i) }
                @priceArr.sort!
            when 2
                @dayNum = Integer(line)
            else
                coin = Integer(line)
                puts searchResult(coin,0,@shopNum-1)
            end
        end
    end

    def searchResult(coin,lo,hi)
        while lo <= hi do
            mid = lo + (hi-lo)/2
            if coin < @priceArr[mid]
                hi = mid - 1
            elsif coin == @priceArr[mid]
                rtn = mid + 1;
                lo = mid + 1;
            else
                lo = mid + 1
            end
        end

        return rtn ? rtn : mid
    end
end

main = Program.new(ARGF)

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                @shopNum = Integer(line)
            when 1
                @priceArr = line.split(" ")
                @priceArr = @priceArr.map{ |i| i = Integer(i) }
                @priceArr.sort!
            when 2
                @dayNum = Integer(line)
            else
                coin = Integer(line)
                p searchResult(coin,1,@shopNum)
            end
        end
    end

    def searchResult(coin,lo,hi)
        mid = lo+(hi-lo)/2
        if lo >= hi
            return lo - 1
        elsif coin > @priceArr[mid-1]
            return searchResult(coin,mid+1,hi)
        elsif coin < @priceArr[mid-1]
            return searchResult(coin,lo,mid-1)
        elsif coin == @priceArr[mid-1]
            return mid
        end
    end
end

main = Program.new(ARGF)

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                @shopNum = Integer(line)
            when 1
                @priceArr = line.split(" ")
                @priceArr = @priceArr.map{ |i| i = Integer(i) }
                @priceArr.sort!
            when 2
                @dayNum = Integer(line)
            else
                coin = Integer(line)
                puts searchResult(coin,1,@shopNum)
            end
        end
    end

    def searchResult(coin,lo,hi)
        mid = lo+(hi-lo)/2
        if lo >= hi
            if coin < @priceArr[mid-1] && mid != 0
                return mid - 1
            else
                return mid
            end
        elsif coin > @priceArr[mid-1]
            return searchResult(coin,mid+1,hi)
        elsif coin < @priceArr[mid-1]
            return searchResult(coin,lo,mid-1)
        elsif coin == @priceArr[mid-1]
            return mid
        end
    end
end

main = Program.new(ARGF)

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
prices = gets.split.map(&:to_i)
days_staying = gets.to_i
prices.sort!
days_staying.times {
    coins = gets.to_i

    # binary search (end will be exclusive b <= i < e)
    b, e = 0, prices.length
    loop {
        mid = (b + e) / 2
        p [b, e, mid, coins]
        if prices[mid] < coins
            p '#1'
            if (e - b) <= 1
                puts e
                break
            end

            b = mid
        elsif prices[mid] == coins
            p '#2'
            printed = false
            for i in mid...e
                if prices[i] > coins
                    puts i
                    printed = true
                    break
                end
            end

            puts e if !printed
            break
        elsif prices[mid-1] <= coins && prices[mid+1] >= coins
            p '#3'
            puts mid
            break
        else
            p '#4'

            if (e - b) <= 1
                puts b
                break
            end

            e = mid
        end
    }
}

n = gets.chomp.to_i
x = gets.chomp.split
q = gets.chomp.to_i
m = []
for i in (0..q-1)
	m[i] = gets.chomp.to_i
end

x = x.sort_by { |a,b| a.to_i }
x = x.map(&:to_i)

for i in (0..q-1)
	if x.min.to_i > m[i]
		puts 0
	elsif x.max.to_i <= m[i]
		puts n
	elsif x.find_index(m[i]) != nil
		puts x.find_index(m[i]) + 1
	else
		mid = 0
		first = 0
		last = n
		while first < last
			mid = first + (last-first)/2
			if m[i] > x[mid]
				first = mid + 1
			else
				last = mid
			end
		end
		puts first
	end

end

n = gets.chomp.to_i
x = gets.chomp.split.map(&:to_i).sort
q = gets.chomp.to_i
m = []
for i in (0..q-1)
	m[i] = gets.chomp.to_i
end

for i in (0..q-1)
	first = 0
	last = n-1
	mid = (first + last)/2
	while first < last
		if x[mid] < m[i]
			first = mid+1
		else
			last = mid
		end
		mid = (first + last)/2
	end
	if x[mid] <= m[i]
		puts mid+1
	else
		puts mid
	end
end

n = gets.chomp.to_i
x = gets.chomp.split
q = gets.chomp.to_i
m = []
for i in (0..q-1)
	m[i] = gets.chomp.to_i
end

x = x.sort_by { |a,b| a.to_i }
x = x.map(&:to_i)

for i in (0..q-1)
	if x.min.to_i > m[i]
		puts 0
	elsif x.max.to_i <= m[i]
		puts n
	elsif x.find_index(m[i]) != nil
		puts x.find_index(m[i]) + 1
	else
		mid = 0
		first = 0
		last = n-1
		while first < last
			mid = first + (last-first)/2
			if m[i] > x[mid]
				first = mid + 1
			else
				last = mid
			end
		end
		puts first
	end

end

n = gets.chomp.to_i
x = gets.chomp.split
q = gets.chomp.to_i
m = []
for i in (0..q-1)
	m[i] = gets.chomp.to_i
end

# sorted_x = []
# for i in (0..n-1)
# 	sorted_x << [x[i].to_i, i]
# end
# x = sorted_x.sort { |a,b| a[0] <=> b[0] }
x = x.sort_by { |a,b| a.to_i }
x = x.map(&:to_i)

for i in (0..q-1)
	if x.min.to_i > m[i]
		puts 0
	elsif x.max.to_i <= m[i]
		puts n
	elsif x.find_index(m[i]) != nil
		puts x.find_index(m[i]) + 1
	else
		if m[i] < x[(n/2) + 1]
			temp = n/2
			while x[temp] > m[i]
				temp /= 2
			end
		else
			temp = (n/2) + 1
			while x[temp] > m[i]
				temp *= 2
			end
		end
		puts temp + 1
	end
end

class BinaryTree
  attr_accessor :root

  def insert(value)
    if @root.nil?
      @root = Node.new(value)
    else
      @root.insert(value)
    end
  end

  def count_elements(node = @root, sum_elements = 0)
    sum_elements = count_elements(node.left, sum_elements) if node.left
    sum_elements = sum_elements + 1
    node.count = sum_elements
    sum_elements = count_elements(node.right, node.count) if node.right
    sum_elements
  end

  def search(searched_value, last_count, node = @root)
    return last_count if node.nil?
    if searched_value < node.node_value
      search(searched_value, last_count, node.left)
    elsif searched_value > node.node_value
      search(searched_value, node.count, node.right)
    else
      return node.count
    end
  end

  class Node
    attr_reader :node_value
    attr_accessor :left, :right, :count

    def initialize(value)
      @node_value = value
      @count = 0
    end

    def insert(value)
      case node_value <=> value
      when 1 then insert_left(value)
      when -1 then insert_right(value)
      when 0 then false
      end
    end

    private

    def insert_left(value)
      if left
        left.insert(value)
      else
        self.left = Node.new(value)
      end
    end

    def insert_right(value)
      if right
        right.insert(value)
      else
        self.right = Node.new(value)
      end
    end
  end
end

bt = BinaryTree.new
n = gets.to_i
prices = gets.split.each do |price|
  bt.insert(price.to_i)
end
days = gets.to_i

bt.count_elements

for i in 0..(days - 1)
  money = gets.to_i
  puts bt.search(money, 0)
end

class BinaryTree
  attr_accessor :root

  def insert(value)
    if @root.nil?
      @root = Node.new(value)
    else
      @root.insert(value)
    end
  end

  def count_elements(node = @root, sum_elements = 0)
    sum_elements = count_elements(node.left, sum_elements) if node.left
    sum_elements = sum_elements + 1
    node.count = sum_elements
    sum_elements = count_elements(node.right, node.count) if node.right
    sum_elements
  end

  def search(value, node = @root, last_value = 0)
    return last_value if node.nil?
    if value < node.node_value
      search(value, node.left, node.count - 1)
    elsif value > node.node_value
      search(value, node.right, node.count)
    else
      return node.count
    end
  end

  class Node
    attr_reader :node_value
    attr_accessor :left, :right, :count

    def initialize(value)
      @node_value = value
      @count = 0
    end

    def insert(value)
      case node_value <=> value
      when 1 then insert_left(value)
      when -1 then insert_right(value)
      when 0 then false
      end
    end

    private

    def insert_left(value)
      if left
        left.insert(value)
      else
        self.left = Node.new(value)
      end
    end

    def insert_right(value)
      if right
        right.insert(value)
      else
        self.right = Node.new(value)
      end
    end
  end
end

bt = BinaryTree.new
n = gets.to_i
prices = gets.split.each do |price|
  bt.insert(price.to_i)
end
days = gets.to_i

bt.count_elements

for i in 0..(days - 1)
  money = gets.to_i
  puts bt.search(money)
end

shopNum = gets.chomp.to_i
shopPrices = gets.chomp.split(' ').map(&:to_i)
dayNum = gets.chomp.to_i

shopPrices.sort!
indexHash = {}

shopPrices.each_with_index do |p, i|
	indexHash[p] = i + 1
end

dayNum.times do
	coin = gets.chomp.to_i
	idx = shopPrices.bsearch { |x| x >= coin }

	#puts "idx : #{idx}"

	if idx.nil?
		puts shopPrices.length
	else
		if coin < idx
			puts indexHash[idx] - 1
		else
			puts indexHash[idx]
		end
	end
end


args = gets.split(" ").map(&:to_i)
n = args[0]
k = args[1]

arr1 = [1]*(n-1) + [0]
arr2 = arr1
add = []

k.times do |j|
    arr1.each_with_index do |e, i|
        if (2*e < n-i) and (i<n-1) then
            add[i] = n-(2**j)-1
        else
            add[i] = i+arr1[i]
        end
        arr2[i] = arr1[i]+arr1[add[i]]
    end
    arr1 = arr2
    puts add.map{ |e| e+1 }.join(" ")
end

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

lb = ub = 1

k.times do
  res = ''
  lb = ub
  ub *= 2
  n.times do |i|
    t = n - i - 1
    if t <= lb
      res << "#{n} "
    elsif t > ub
      res << "#{i + 1} "
    else
      res << "#{n - t + lb} "
    end
  end
  puts res
end

at_exit do
  n,k = get_numbers_from_line[0..1]
  numbers = [1]*n
  numbers[n-1] = 0

  targets = (n-1).downto(0).to_a

  ds = (0...n).map{|i| targets[i] - numbers[i] }

  max = 1
  (0...k).each do |i|
    ci = []
    (0...n).each do |j|
      t = ds[j]
      if t == 0
        ci << n
      elsif t >= max
        ci << n - max
        ds[j] -= max
      else
        ci << n - ds[j]
        ds[j] = 0
      end
    end
    puts ci.join(' ')
    max *= 2
  end

end

def get_numbers_from_line
  gets.strip.split.map(&:to_i)
end

args = gets.split(" ").map(&:to_i)
n = args[0]
k = args[1]

arr1 = [1]*(n-1) + [0]
arr2 = arr1
add = []

k.times do |j|
    arr1.each_with_index do |e, i|
        if 2*e < n-i then
            arr2[i] = arr1[i]+e
            add[i] = i
        else
            arr2[i] = arr1[i]+arr1[i+arr1[i]]
            add[i] = i+arr1[i]
        end
    end
    arr1 = arr2
    puts add.map{ |e| e+1 }.join(" ")
end

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

lb = ub = 1

k.times do
  res = ''
  lb = ub
  ub *= 2
  n.times do |i|
    t = n - i - 1
    if t <= lb
      res << "#{n} "
    elsif t > ub
      res << "#{i + 1} "
    else
      res << "#{t} "
    end
  end
  puts res
end


lays = gets.to_i
lights = [0, 0] + gets.split(' ').map(&:to_i)

calc_addit = lambda do |root|
  return [0, 0] if 2*root >= lights.size

  l, l_add = calc_addit.(2*root)
  l += lights[2*root]
  r, r_add = calc_addit.(2*root+1)
  r += lights[2*root+1]
  if l == r
    [l, l_add+r_add]
  elsif l > r
    [l, l_add + r_add + (l-r)]
  else
    [r, l_add + r_add + (r-l)]
  end
end

puts calc_addit.(1).last

n = gets.to_i
a = gets.strip.split(" ").map(&:to_i)
answer = 0
count = Array.new
(2**(n+1)-2).downto(2**n-1) do |i|
  count[i] = 0
end
(2**n-2).downto(0) do |i|
  candidates = [count[i*2+1]+a[i*2], count[i*2+2]+a[i*2+1]]
  count[i] = candidates.max
  answer += candidates.max - candidates.min
end
puts answer

$gl = 0
$h = []
$n
def f(v)

	if(v >= 2**$n and v < 2**($n+1)) then
		return 0
	end

	a = f(2*v) + $h[2*v-2]
	b = f(2*v+1) +  $h[2*v-1]
	$gl += (a-b).abs

	return  [a,b].max
end

$n = gets.to_i
$h = gets.split.collect{|i| i.to_i}

f(1)
puts $gl


results = []

t = gets.chomp
(1..t.to_i).each do |iteration|
	n, k = gets.chomp.split(' ').map{|e| e.to_i}
	rest = gets.chomp
	index  = 0
	fragments = []
	places_count = 0
	while index < rest.length do
		if rest[index] == '0'
			places_count += 1
			index += 1
		else
			places_count -= k
			if places_count > 0
				fragments.push(places_count)
			end
			places_count = 0
			index += k + 1
		end
	end
	if places_count > 0
		fragments.push(places_count)
	end

	optimal_tables = 0
	fragments.each do |el|
		optimal_tables += el / (k + 1)
		optimal_tables += 1 if el % (k + 1) > 0
	end

	results.push(optimal_tables)
end

results.each do |result|
	puts result
end

t = gets.to_i
t.times do
	n, k = gets.split.map(&:to_i)
	s = "1" + "0" * k + gets.chomp + "0" * k + "1" # append sentinel

	occupy = 0

	# count continous 0s and check
	count = 0
	s.each_char do |c|
		case c
		when "0"
			count += 1
		when "1"
			avail = [(count + 1) / (k+1) - 1, 0].max
			occupy += avail
			#puts "  #{count} -> #{avail}"
			count = 0
		end
	end

	p occupy
end

results = []
t = gets.chomp
(1..t.to_i).each do
	tables_count, k = gets.chomp.split(' ').map{|e| e.to_i}
	tabels_map = gets.chomp
	index  = 0
	empty_places_counts = []
	empty_places_count = 0
	while index < tables_count do
		if tabels_map[index] == '0'
			empty_places_count += 1
			index += 1
		else
			empty_places_count -= k
			if empty_places_count > 0
				empty_places_counts.push(empty_places_count)
			end
			empty_places_count = 0
			index += k + 1
		end
	end
	if empty_places_count > 0
		empty_places_counts.push(empty_places_count)
	end
	optimal_tables = 0
	empty_places_counts.each do |el|
		optimal_tables += el / (k + 1)
		optimal_tables += 1 if el % (k + 1) > 0
	end

	results.push(optimal_tables)
end

results.each do |res|
	puts res
end

def solve(arr_str, k)
	parts = arr_str
		.scan(/1+|0+/)
		.map { |i| i[0] == '0' ? i.size : 0}

	parts.map.with_index
		.reduce(0) do |res, (i, ind)|
			without_borders = i
			without_borders -= k unless ind == 0
			without_borders -= k unless ind == parts.size - 1
			if without_borders > 0
				res += 1 + (without_borders - 1) / (k + 1)
			end

			res
		end
end

total_qty = ARGF.gets.to_i
total_qty.times do
	_n, k = ARGF.gets.split.map(&:to_i)
	a = ARGF.gets.strip
	puts solve(a, k)
end

data_lines = gets.chomp
result = []
(1..data_lines.to_i).each do
	allows = gets.chomp.split(" ")
	tables_count = allows.first.to_i
	k = allows.last.to_i
	map = gets.chomp
	reserved_count = 0
	(0..(map.length-1)).each do |index|
		if map[index] == '1'
			reserved_count += 1
			if reserved_count == 1
				ost = index % (k+1)
				if  ost > 0
					tables_count -= ost
				end
			end
		end
	end
	if tables_count <= k
		result.push(1 - reserved_count)
		next
	end
	all_possible_tables = tables_count / (k + 1)
	if tables_count % (k + 1) > 0
		all_possible_tables += 1
	end
	res = all_possible_tables - reserved_count
	result.push(res)
end
result.each do |res|
	puts res
end

data_lines = gets.chomp
result = []
(1..data_lines.to_i).each do
	allows = gets.chomp.split(" ")
	tables_count = allows.first.to_i
	k = allows.last.to_i
	map = gets.chomp
	reserved_count = 0
	(0..(map.length-1)).each do |index|
		if map[index] == '1'
			reserved_count += 1
			if reserved_count == 1
				ost = index % (k+1)
				if  ost > 0
					tables_count -= ost
				end
			end
		end
	end
	all_possible_tables = tables_count / (k + 1)
	if tables_count % (k + 1) > 0
		all_possible_tables += 1
	end
	res = all_possible_tables - reserved_count
	if res < 0
		result.push(1)
		next
	end
	if reserved_count == 0 && res < 1
		result.push(1)
		next
	end
	result.push(res)
end
result.each do |res|
	puts res
end

data_lines = gets.chomp
result = []
(1..data_lines.to_i).each do
	allows = gets.chomp.split(" ")
	tables_count = allows.first.to_i
	k = allows.last.to_i
	map = gets.chomp
	reserved_count = 0
	(0..(map.length-1)).each do |index|
		if map[index] == '1'
			reserved_count += 1
			if reserved_count == 1
				ost = index % (k+1)
				if  ost > 0
					tables_count -= ost
				end
			end
		end
	end
	all_possible_tables = tables_count / (k + 1)
	if tables_count % (k + 1) > 0
		all_possible_tables += 1
	end
	result.push(all_possible_tables - reserved_count)
end
result.each do |res|
	puts res
end

def solve(arr_str, k)
	parts = arr_str
		.split('1')
		.map(&:size)
		.reject(&:zero?)

	parts.map.with_index
		.reduce(0) do |res, (i, ind)|
			without_borders = i
			without_borders -= k unless ind == 0
			without_borders -= k unless ind == parts.size - 1
			if without_borders > 0
				res += 1 + (without_borders - 1) / (k + 1)
			end

			res
		end
end

total_qty = ARGF.gets.to_i
total_qty.times do
	_n, k = ARGF.gets.split.map(&:to_i)
	a = ARGF.gets.strip
	puts solve(a, k)
end


  t = gets.to_i
  t.times do
    n, k = gets.split(' ').map(&:to_i)

    # solution 3

    if n == k
      puts n + 1
      next
    end
    if k < n
      puts k
      next
    end
    # puts n*(k/n) + (k-1)
    # puts n*(k/n-1) + (k-1)
    # 5 12
    ra = k / (n - 1)
    re = k % (n - 1)
    koef = re == 0 ? -1 : re
    puts n * ra + koef
  end

t = gets.to_i

(1).upto(t) do
  x = gets.split(' ')
  n = x[0].to_i
  k = x[1].to_i
  val = k/(n-1)
  val+=k
  val-= (k.modulo(n-1)==0?1:0)
  puts val
end

# Created by IntelliJ IDEA.
# User: gautam.roy
# Date: 5/10/20
# Time: 1:25 PM
# To change this template use File | Settings | File Templates.

# if n > k -> k
# return k/n + k
t = gets.to_i
for i in 1..t do
  n, k = gets.split(' ').map { |s| s.to_i }
  if n > k
    puts k
    next
  end
  puts (k - 1) / (n-1) + k
end

def func(n, k)
    res=2**40-1
    a=2**39
    while a>0
        t=res^a
        cnt=t-t/n
        res=t if cnt>=k
        a>>=1
    end
    res
end

gets.to_i.times{
    n, k=gets.split.map &:to_i
    puts func(n, k)
}

1.upto(gets.to_i) do
  n, k = gets.split(' ').map(&:to_i)

  puts k.div(n-1)*n + (k.modulo(n-1).zero? ? -1 : k.modulo(n-1))
end

  t = gets.to_i
  t.times do
    n, k = gets.split(' ').map(&:to_i)
    if n == k
      puts n + 1
      next
    end
    if k < n
      puts k
      next
    end
    ra = k / (n - 1)
    re = k % ra
    koef = re == 0 ? -1 : re
    puts n * ra + koef
end


t = gets.chomp.to_i
t.times do
  px, py = gets.chomp.split(' ').map(&:to_i)
  s = gets.chomp.split('')
  lx = rx = uy = ly = 0
  s.each do |el|
    case el
    when "U"
      uy += 1
    when "D"
      ly -= 1
    when "R"
      rx += 1
    when "L"
      lx -= 1
    end
  end
  resx = px > 0 && px <= rx || px < 0 && px >= lx || px == 0
  resy = py > 0 && py <= uy || py < 0 && py >= ly || py == 0
  puts resx && resy ? "YES" : "NO"
end

def solve
  x, y = ints
  s = string.chars.tally
  enough = true
  enough = false if x > 0 and (s[?R] || 0) < x.abs
  enough = false if x < 0 and (s[?L] || 0) < x.abs
  enough = false if y > 0 and (s[?U] || 0) < y.abs
  enough = false if y < 0 and (s[?D] || 0) < y.abs
  yesno enough
end

def main
  t = int
  t.times do
    solve
  end
end

DEBUG = true
MOD = 10**9+7
YESNO = %w(NO YES)
INF = 10**9

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)
  s=gets.chomp.chars
  s.each do |ss|
    if ss=="U"&&y>0
      y-=1
    elsif ss=="D"&&y<0
      y+=1
    elsif ss=="R"&&x>0
      x-=1
    elsif ss=="L"&&x<0
      x+=1
    end
  end
  if [x,y]==[0,0]
    puts "YES"
  else
    puts "NO"
  end
end

t= gets.chomp.to_i
while t > 0
 flag = false
 x, y = 	gets.chomp.split(' ')
 s = gets.chomp
 x = x.to_i
 y = y.to_i
 count_u = s.count('U')
 count_r = s.count('R')
 count_l = s.count('L')
 count_d = s.count('D')
 if (x >= 0 && y >= 0)
   if (count_r >= x.abs && count_u >= y.abs)
     puts "YES"
     flag = true
   end
 elsif  (x >= 0 && y < 0)
   if (count_r >= x.abs && count_d >= y.abs)
   	 puts "YES"
     flag = true
   end
 elsif  (x < 0 && y >= 0)
 	if (count_l >= x.abs && count_u >= y.abs)
 		 puts "YES"
     flag = true
   end
 elsif  (x < 0 && y < 0)
   if (count_l >= x.abs && count_d >= y.abs)
   	 puts "YES"
     flag = true
   end
 end
 if(!flag)
   puts "NO"
 end
 t = t - 1
end

gets.to_i.times do
    x, y = gets.split.map &:to_i
    s = gets.chomp
    l, r, d, u = 0, 0, 0, 0
    s.each_char do |c|
        case c
            when 'L'
                l += 1
            when 'R'
                r += 1
            when 'D'
                d += 1
            else
                u += 1
        end
    end
    x = 0 if x > 0 && r >= x || x < 0 && l >= -x
    y = 0 if y > 0 && u >= y || y < 0 && d >= -y
    puts x == 0 && y == 0 ? "YES" : "NO"
end

t= gets.chomp.to_i
while t > 0
 flag = false
 x, y = 	gets.chomp.split(' ')
 s = gets.chomp
 x = x.to_i
 y = y.to_i
 count_u = s.count('U')
 count_r = s.count('R')
 count_l = s.count('L')
 count_d = s.count('D')
 if (x > 0 && y > 0)
   if (count_r >= x.abs && count_u >= y.abs)
     puts "YES"
     flag = true
   end
 elsif  (x > 0 && y < 0)
   if (count_r >= x.abs && count_d >= y.abs)
   	 puts "YES"
     flag = true
   end
 elsif  (x < 0 && y > 0)
 	if (count_l >= x.abs && count_u >= y.abs)
 		 puts "YES"
     flag = true
   end
 elsif  (x < 0 && y < 0)
   if (count_l >= x.abs && count_d >= y.abs)
   	 puts "YES"
     flag = true
   end
 end
 if(!flag)
   puts "NO"
 end
 t = t - 1
end


n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
cnt = 0
cur = a[0]
1.upto(n - 1) do |i|
    if cur == n
        puts n
        break
    elsif cur > a[i]
        cnt += 1
        if cnt == k
            puts cur
            break
        end
    else
        cnt = 1
        cur = a[i]
        if cnt == k || cur == n
            puts cur
            break
        end
    end
end

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
y=x.max
c=m
while true
	if x[0]==y
		break
	end
	if x[0]>x[1]
		a=x.shift
		b=x.shift
		x.unshift(a)
		x.push(b)
		c-=1
	else
		a=x.shift
		x.push(a)
		c=m-1
	end
	break if c==0
end
puts x[0]

n, k = gets.split.map &:to_i
b, c = 0, 0
gets.split.map(&:to_i).each{|v|
	c = b > 0 ? (b > v ? c + 1 : 1) : 0
	b = [b, v].max
	c == k && break
}
p b

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
y=x.max
hs=Hash.new(0)
while true
	if x[0]==y
		hs[y]+=m
		break
	end
	if x[0]>x[1]
		a=x.shift
		b=x.shift
		x.unshift(a)
		x.push(b)
		hs[a]+=1
	else
		a=x.shift
		x.push(a)
		hs[x[0]]+=1
	end
	m-=1
end
ans=-1
ansV=-1
hs.each{|k,v|
	if v>ansV
		ansV=v
		ans=k
	end
}
puts ans

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
y=x.max
hs=Hash.new(0)
while true
	if x[0]==y
		break
	end
	if x[0]>x[1]
		a=x.shift
		b=x.shift
		x.unshift(a)
		x.push(b)
		hs[a]+=1
	else
		a=x.shift
		x.push(a)
		hs[x[0]]+=1
	end
end
puts x[0]

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
y=x.max
hs=Hash.new(0)
while true
	break if m==0
	if x[0]==y
		break
	end
	if x[0]>x[1]
		a=x.shift
		b=x.shift
		x.unshift(a)
		x.push(b)
		hs[a]+=1
	else
		a=x.shift
		x.push(a)
		hs[x[0]]+=1
	end
	m-=1
end
puts x[0]