b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

def modular_pow base, exponent, modulus
  result1 = 1
  while exponent > 0
    if (exponent % 2 == 1)
       result1 = (result1 * base) % modulus
    end
    exponent /= 2
    base = (base * base) % modulus
  end
  return result1
end

if result > s
    result = s
end

kq = modular_pow x, result, 1000000007

puts kq

require 'openssl'
n,x=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=10**9+7
s=a.reduce(&:+)
a.map!{|x|s-x}
p=a.min
a=Hash[a.map{|x|x-p}.inject(Hash.new(0)){|h,v|h[v]+=1;h}.sort]
i=0
loop do
  q,d=a[i].divmod x
  if d==0
    p+=1
    i+=1
    a[i]||=0
    a[i]+=q
  else
    break
  end
end
p x.to_bn.mod_exp([p,s].min,m)

require 'openssl'
x,n=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=10**9+7
s=a.reduce(&:+)
a.map!{|x|s-x}
p=a.min
a=a.group_by{|x|x}.sort.map{|a,b|[a,b.size]}
i=0
while a[i]
  q,d=a[i][1].divmod x
  if d==0
    p+=1
    i+=1
    a[i]||=[i,0]
    a[i][1]+=q
  else
    break
  end
end
p x.to_bn.mod_exp([p,s].min,m)

require 'openssl'
x,n=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
m=10**9+7
s=a.reduce(&:+)
a.map!{|x|s-x}
p=a.min
a=Hash[a.map{|x|x-p}.inject(Hash.new(0)){|h,v|h[v]+=1;h}.sort]
i=0
loop do
  q,d=a[i].divmod x
  if d==0
    p+=1
    i+=1
    a[i]||=0
    a[i]+=q
  else
    break
  end
end
p x.to_bn.mod_exp([p,s].min,m)

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while i> 0 && kt do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i]
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
    end
end

kq = x**result % 1000000007

puts result

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s +=a[i]
end

result = s - a[n-1]

i = n-1
while i > 0 && a[i-1] == a[i] do
    i -= 1
end

k = n - i
while k % x == 0
    k = k/x
    result += 1
end

kq = x**result % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i]
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

def modular_pow base, exponent, modulus
  result1 = 1
  while exponent > 0
    if (exponent % 2 == 1)
       result1 = (result1 * base) % modulus
    end
    exponent /= 2
    base = (base * base) % modulus
  end
  return result1
end

kq = modular_pow x, result, 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

if x!= 1
    while kt == 1 do
        kt = 1
        while i > 0 && a[i] == a[i-1] do
            i -= 1
            k += 1
        end

        if i > 0
            i -= 1
            while k % x == 0 && a[n-1] > a[i] do
                k = k/x
                a[n-1] -= 1
                result += 1
            end
            if a[n-1] != a[i]
                kt = 0
            else
                k += 1
            end
        else
            while k % x == 0 do
                k = k/x
                a[n-1] -= 1
                result += 1
            end
            kt = 0
        end
    end
else
    result = 1
end

kq = x**result % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i]
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        kt = 0
    end
end

kq = ( x**result) % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]
sodu = []

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

tuanhoan = 0

sodu[0] = 1
(1..result).each do |i|
    sodu[i] = (sodu[i-1]*x) % 1000000007
    if sodu[i] == 1
        tuanhoan = i
        break
    end
end

if tuanhoan != 0
    kq = sodu[result%tuanhoan]
else
    kq = sodu[result]
end

puts tuanhoan

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while i> 0 && kt do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i]
            kt = 0
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
    end
end

kq = x**result % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]
sodu1 = []

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

sodu1[0] = 1
(1..1000000).each do |i|
    sodu1[i] = (sodu1[i-1] * x ) % 1000000007
end

if result > 1000000
    sodu = sodu1[1000000]
    thuong = result/1000000
    du = result % 1000000
    (1..thuong).each do |i|
        sodu = (sodu * x) % 1000000007
    end
    if du != 0
        (1..du).each do |j|
            sodu = ( sodu * x) % 1000000007
        end
    end
end

if result <= 1000000
    kq = sodu1[result]
else
    kq = sodu
end

puts sodu

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i]
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        kt = 0
    end
end

kq = x**result % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

puts result

while i> 0 && kt do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i]
            kt = 0
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
    end
end

kq = x**result % 1000000007

puts result

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 0

while i> 0 && ( k % i == 0 || k == 1) do
    while i > 0 && a[i-1] == a[i] do
        i -= 1
        k += 1
    end

    while k % x == 0 && a[n-j] > a[n-i] do
        k = k/x
        a[n-1] -= 1
    end
end

while k % x == 0
    k = k/x
    a[n-1] -= 1
end

result = s - a[n-j]

kq = x**result % 1000000007

puts result

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        kt = 0
    end
end

kq = ( x**result) % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]
sodu1 = []

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

def modular_pow base, exponent, modulus
  result1 = 1
  while exponent > 0
    if (exponent % 2 == 1)
       result1 = (result1 * base) % modulus
    end
    exponent /= 2
    base = (base * base) % modulus
  end
  return result1
end

kq = modular_pow x, result, (10**9 + 7)

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

def modular_pow base, exponent, modulus
  result1 = 1
  while exponent > 0
    if (exponent % 2 == 1)
       result1 = (result1 * base) % modulus
    end
    exponent /= 2
    base = (base * base) % modulus
  end
  return result1
end

kq = modular_pow x, result, 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        kt = 0
    end
end

kq = ( x**result) % 1000000007

puts result

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]
kq = ( x**result) % 1000000007

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
result = s - a[n-1]
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
            result += 1
        end
        kt = 0
    end
end

kq = ( x**result) % 1000000007
if kq == 0
    kq = 10**9 + 7
end

puts kq

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]

s = 0

(0..n-1).each do |i|
    s +=a[i]
end

result = s - a[n-1]

puts x**result

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]
sodu1 = []
sodu2 = []

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

tuanhoan = 0

sodu = 1
(1..1000000).each do |i|
    sodu = (sodu * x ) % 1000000007
end

if result > 1000000
    sodu1 = sodu
    thuong = result/1000000
    du = result % 1000000
    (1..thuong).each do |i|
        sodu1 = (sodu1 * sodu) % 1000000007
    end
    if du > 0
        (1..du).each do |j|
            sodu1 = ( sodu1 * x) % 1000000007
        end
    end
    sodu = sodu1
end

puts sodu

b = gets.chomp.split(" ").map{|i| i.to_i}
a = gets.chomp.split(" ").map{|i| i.to_i}
n = b[0]
x = b[1]
sodu = []

s = 0

(0..n-1).each do |i|
    s += a[i]
end

i = n-1
j = 1
k = 1
kt = 1

while kt == 1 do
    kt = 1
    while i > 0 && a[i] == a[i-1] do
        i -= 1
        k += 1
    end

    if i > 0
        i -= 1
        while k % x == 0 && a[n-1] > a[i] do
            k = k/x
            a[n-1] -= 1
        end
        if a[n-1] != a[i] || a[i] == 0
            kt = 0
        else
            k += 1
        end
    else
        while k % x == 0 do
            k = k/x
            a[n-1] -= 1
        end
        kt = 0
    end
end

result = s - a[n-1]

tuanhoan = 0

sodu[1] = x
(2..result).each do |i|
    sodu[i] = (sodu[i-1]*x) % 1000000007
    if sodu[i] == sodu[1]
        tuanhoan = i-1
        sodu[0] = sodu[i-1]
        break
    end
end

if tuanhoan != 0
    kq = sodu[result%tuanhoan]
else
    kq = sodu[result]
end

puts kq


n, m = gets.chomp.split(/ /).map(&:to_i)
students = []
n.times do
    students << gets.chomp.split("").map(&:to_i)
end
toppers = []
m.times do |subject|
    topper = Array.new(n, 0)
    n.times do |student|
        marks = students[student][subject]
        topper[student] = marks if marks >= topper[student]
    end
    highest = topper.max
    topper.each_index do |student|
        marks = topper[student]
        toppers << student if marks == highest && !toppers.include?( student )
    end
end
puts toppers.length

k=gets.chomp
a=[]
b=[]
a=k.split(' ')
a[0].to_i.times do
  b<<gets.chomp
end
c=[]
a[0].to_i.times do |x|
  c<<b[x].split('')
end
max=[]
a[1].to_i.times do |y|
  maxt=0
  a[0].to_i.times do |x|
    maxt=[maxt,c[x][y].to_i].max
  end
  max[y]=maxt
end
is=[]
a[0].to_i.times do |x|
  a[1].to_i.times do |y|
    is[x]=true if c[x][y].to_i>=max[y]
  end
end
count=0
is.each do |x|
  count+=1 if x==true
end
puts count

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

n.times do
  a << gets.strip.split(//).map(&:to_i)
end

b = a.transpose

ans = Array.new(n){ false }
m.times do |i|
  mx = b[i].max
  b[i].each_with_index{|s,i| ans[i] = true if s == mx}
end

p ans.count(true)

require 'set'
require 'rational'
require 'mathn'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

n, m = gets.split.map &:to_i
table = []

n.times{
  table << gets.split(//).map(&:to_i)
}

students = Set.new
m.times{|index|
  max_value = table.collect{|column| column[index]}.max

  student_id = 0
  table.each{|array|
    students << student_id if array[index] == max_value
    student_id+=1
  }
}

print students.to_a.size

n,m=gets.chomp.split.collect{|a| a.to_i }
students=[]
max_scores=Array.new(m,0)
for i in 0..n-1
s=gets.chomp.split('').collect{|a| a.to_i}
students.push s
s.each_index{|j| if max_scores[j] < s[j] then max_scores[j]=s[j] end}
end
res=0
students.each do |student|
succ=false
student.each_index{|i| if student[i]==max_scores[i] then succ=true end}
if succ
 res+=1
end
end
puts res

input = gets.chomp
nums = input.split
n = Integer(nums[0])
m = Integer(nums[1])
a = Array.new
for i in 0...n
    a[i] = Array.new
    input = gets.chomp
    for j in 0...m
        a[i][j] = Integer(input[j])
    end
end

def best(a,n,j)
    b = Array.new
    c = 0
    for i in 0...n
        if a[i][j] > c
            c = a[i][j]
        end
    end
    for i in 0...n
        if a[i][j] == c
            b.push(i)
        end
    end
    return b
end
b = Array.new
for j in 0...m
    b += best(a,n,j)
end
b = b.sort
c = Array.new
c[0] = b[0]
for i in 0...b.length-1
    if b[i+1] != b[i]
        c.push(b[i+1])
    end
end
puts c.length

n,m=gets.split(' ').map(&:to_i)
a=Array.new(n)
best = Array.new(m){0}
best_student = Array.new(n){0}
n.times do |i|
    a[i] = gets.chomp.split('').map(&:to_i)
    a[i].each_index{|j|
        if(best[j] < a[i][j])
            best[j] = a[i][j]
        end
    }
end
count = 0
n.times do|i|
    a[i].each_index{|j|
        if(a[i][j] == best[j])
            count+=1
            break
        end
    }
end
p count

b=->(a){m=a.max;l=[];a.size.times{|i|a[i]==m&&l<<i};l}
n,m=gets.split.map &:to_i
g=[];r=[]
n.times{g << gets.bytes}
m.times{r|=b[g.map &:shift]}
puts r.size

n, m = gets.split.map(&:to_i)
ar = Array.new(n){gets.chars.to_a}
b = (['0'] * m).zip(*ar).map(&:max)
cnt = 0
n.times do |i|
    a = false
    m.times do |j|
        if ar[i][j] == b[j] then
            a = true
        end
    end
    if a == true then
        cnt = cnt + 1
    end
end
p cnt

n,m = gets.split(" ").map(&:to_i)
max = Array.new(m,0)
arr = []
best = Hash.new(false)
n.times do |s|
	a = gets.split("").map(&:to_i)
	arr << a
	(0..m-1).each do |i|
		if a[i] >= max[i]
			max[i] = a[i]
		end
	end
end

arr.each_with_index do |a,i|
	(0..m-1).each do |j|
		best[i] = true if a[j] == max[j]
	end
end

puts best.keys.count

n, m = gets.split.map(&:to_i)
a = n.times.map{gets.chomp.chars.map(&:to_i)}
c = a.transpose.map(&:max)
puts a.count{|x| m.times.any?{|i| x[i] == c[i]}}

n, m = gets.split.map(&:to_i)
a = n.times.map{gets.chomp.split('').map(&:to_i)}
c = a.transpose.map{|x| x.max}
successful = Array.new(n, false)
n.times do |i|
  m.times do |j|
    if a[i][j] == c[j]
      successful[i] = true
      break
    end
  end
end
puts successful.count{|x| x == true}

n,m = gets.split(" ")
n = n.to_i
m = m.to_i
g = Array.new(m) {[]}
for i in 0..n-1
  gr = gets.chomp
  for j in 0..m-1
    g[j] << [gr[j].to_i, i]
	#puts gr[j]
  end
end
u = Array.new(n)
u.fill(0)
g.each do |q|
  q.sort_by! {|e| e[0]}
  q.reverse!
  curr = 0
  u[q[0][1]] = 1
  while curr < n - 1 && q[curr][0] == q[curr + 1][0]

    u[q[curr+1][1]] = 1
	curr += 1
  end
end

puts (u.inject(0) {|sum, curr| sum += curr})

students, subjects = gets.split.map(&:to_i)
a = []
students.times {
  a << gets.chomp.split('')
}
best = a.transpose.map(&:max)
res = 0
(0...students).each { |i|
  (0...subjects).each { |j|
    if a[i][j] == best[j]
      res += 1
      break
    end
  }
}
p res

require 'set'

n, m = gets.chomp.split.map { |x| x.to_i }
grades = []
for _ in 1..n
  grades << gets.chomp
end

suc_students = Set.new
for j in 0...m
  marks_for_subject = []
  for i in 0...n
    marks_for_subject << grades[i][j]
  end
  max = marks_for_subject.max
  for i in 0...n
    if marks_for_subject[i] == max
      suc_students << i
    end
  end
end

puts suc_students.size

n, m = gets.split(' ').map(&:to_i)
a = []
n.times do
  a << gets.strip.split(//).map(&:to_i)
end
ary = Array.new(n, 0)
b = a.transpose
m.times do |c|
  mx = b[c].max
  n.times do |r|
    if mx == a[r][c]
      ary[r] = 1
    end
  end
end
p ary.count(1)

s = gets.split(' ')
n = s[0].to_i
m = s[1].to_i
a = Array.new(n).map!{ Array.new(m, 0) }
for r in 0..n-1
  ln = gets
  for c in 0..m-1
    a[r][c] = ln[c,1].to_i
  end
end
ary = Array.new(n, 0)
for c in 0..m-1
  mx = 0
  for r in 0..n-1
    if mx < a[r][c]
      mx = a[r][c]
    end
  end
  for r in 0..n-1
    if mx == a[r][c]
      ary[r] = 1
    end
  end
end
res = 0
for r in 0..n-1
  if ary[r] > 0
    res += 1
  end
end
p res

n, m = gets.split.map(&:to_i)
a = []
n.times {
  a << gets.strip.split(//).map(&:to_i)
}
ary = [0]
b = a.transpose
m.times { |c|
  mx = b[c].max
  n.times { |r|
    ary[r] = 1 if mx == a[r][c]
  }
}
p ary.count(1)

st = gets.split
n = st[0].to_i
m = st[1].to_i

marks = []
subs = Array.new(m){|i| i = -1}
result = 0
reestr = Array.new(m){|i| i = []}
n.times do |i|
    marks << gets.chomp
    m.times do |j|
        if marks[i][j].to_i > subs[j]
            subs[j] = marks[i][j].to_i
            reestr[j] = []
            reestr[j] << i
        elsif marks[i][j].to_i == subs[j] then
            reestr[j] << i
        end
    end
end

students = Array.new(n){|i| i = 0}
m.times do |i|
    reestr[i].size.times do |j|
        students[reestr[i][j]] = 1
    end
end

students.delete(0)

puts students.size

n,m = gets.chomp.split(/ /).map!{|x| x.to_i}
a = []
s = []
for i in 0..n-1
	s << false
	a << gets.chomp.split(//).map!{|x| x.to_i}
end

for i in 0..m-1
	w = []
	for j in 0..n-1
		w << a[j][i]
	end
	wm = w.max
	for j in 0..n-1
		s[j] = true if w[j] == wm
	end
end
ret = 0
for j in 0..n-1
	ret += 1 if s[j] == true
end
puts ret

n, m = gets.split.map(&:to_i)
a = Array.new(n){gets.chars.to_a}
b = (['0'] * m).zip(*a).map(&:max)
p a.count{|_| _.zip(b).any?{|i, j| i == j}}

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

gradebook = []
max = Array.new(m,0)

n.times do
  row = []
  row = gets.chomp.split(//).map(&:to_i)

  row.each_with_index do |o, i|
    if max[i] < o then max[i] = o end
  end

  gradebook << row
end

counter = 0

gradebook.each do |row|
  row.each_with_index do |o, i|
    if o == max[i]
      counter += 1
      break
    end
  end
end

puts counter

n,m=gets.split.map(&:to_i)
input=[]
n.times{ input.push(gets.split[0])}
ans=0
n.times{|i|
	ok=0
	m.times{|j|
		val=[]
		n.times{|k|
			val.push(input[k][j].to_i)
		}
		ok=1 if val.max==input[i][j].to_i
	}
	ans+=ok
}
p ans

#!/usr/bin/env ruby

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

highest = Array.new(m, 0)
list = []
n.times do
  arr = gets.chomp.each_char.to_a.map(&:to_i)
  arr.each_with_index do |score, idx|
    highest[idx] = [score, highest[idx]].max
  end
  list << arr
end

ans = 0
list.each do |arr|
  arr.each_with_index do |score, idx|
    if highest[idx] == score
      ans += 1
      break
    end
  end
end

p ans

tmp = gets.chomp.split " "
n = tmp[0].to_i
m = tmp[1].to_i

field = []
n.times do
  field.push gets.chomp.split("").map {|i| i.to_i }
end

d = {}
field = field.transpose

field.each_with_index do |line, no|
  max = line.max

  line.each_with_index do |e, i|
    if e == max
      d[i + 1] = true
    end
  end
end

puts d.keys.length

input = STDIN.read.split("\n")

ss = input.shift.split(' ').map(&:to_i)

students, subjects = ss[0], ss[1]

subj_max = []

subjects.times { subj_max.push 0 }

subjects.times do |i|
	students.times do |j|
		subj_max[i] = input[j][i].to_i if input[j][i].to_i > subj_max[i]
	end
end

succ = 0

students.times do |i|
	subjects.times do |j|
		if input[i][j].to_i == subj_max[j].to_i
			succ += 1
			break
		end
	end
end

STDOUT.puts succ

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

s = n.times.map do
  STDIN.gets.chars.map(&:to_i).take(m)
end

max = 0.upto(m-1).map do |i|
  s.max{|a,b| a[i] <=> b[i] }[i]
end
f = s.map do |student|
  max.each_with_index.any? do |m,i|
    student[i] >= m
  end
end
p f.select{|x| x }.size

n,m = gets.split.map(&:to_i)
a=[]
n.times do
  a<<gets.chomp.split('')
end
ans=0
for i in 0..n-1
	wb=false
	for j in 0..m-1
		ib=true
		for k in 0..n-1
			if(a[k][j] > a[i][j])
				ib = false
			end
		end
		if ib==true
			wb=true
		end
	end
	if wb==true
		ans+=1
	end
end
puts ans

def max_index a
  max = a.max
  (0...a.size).find_all{ |x| a[x] == max}
end
#n, m = gets.split.map(&:to_i)
gets
p readlines.map(&:chomp).map {|x| x.split('').map(&:to_i)}.transpose.map { |x| max_index(x) }.flatten.uniq.size

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

s = n.times.map do
  STDIN.gets.chars.map(&:to_i).take(m)
end
f = Array.new(s.size,false)

max = 0.upto(m-1).map do |i|
  s.max{|a,b| a[i] <=> b[i] }[i]
end
p s.map do |student|
  max.each_with_index.any? do |m,i|
    student[i] >= m
  end
end

n,m = gets.split.map(&:to_i)
a=[]
n.times do
  a<<gets.chomp.split('')
end
best=a.transpose.map(&:max)
ans=0
i=0
j=0
while i<n do
	while j<m do
		if a[i][j] == best[j]
			ans += 1
			break
		end
		j+=1
	end
	i+=1
end
puts ans

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

n.times do
  a << gets.strip.split(//).map(&:to_i)
end

b = a.transpose

ans = Array.new(n){ false }
m.times do |i|
  mx = b[i].max
  b[i].each_with_index{|s,i| ans[i] = true if s == mx}
end
p ans
p ans.count(true)

n, m = gets.split.map(&:to_i)
ar = Array.new(n){gets.chars.to_a}
b = ['0' * m].zip(*ar).map(&:max)
cnt = 0
n.times do |i|
    a = false
    m.times do |j|
        if ar[i-1][j-1] == b[j-1] then
            a = true
        end
    end
    if a == true then
        cnt = cnt + 1
    end
end
p cnt

n, m = gets.split.map(&:to_i)
ar = Array.new(n){gets.chars.to_a}
b = ['0' * m].zip(*ar).map(&:max)
cnt = 0
n.times do |i|
	m.times do |j|
		if ar[i-1][j-1] == b[j-1] then
			cnt = cnt + 1
			break
		end
	end
end
p cnt

n, m = gets.split.map(&:to_i)
ar = Array.new(n){gets.chars.to_a}
b = (['0'] * m).zip(*ar).map(&:max)
cnt = 0
n.times do |i|
    a = false
    m.times do |j|
        if ar[i-1][j-1] == b[j-1] then
            a = true
        end
    end
    if a == true then
        cnt = cnt + 1
    end
end
p cnt

n,m = gets.split(" ")
n = n.to_i
m = n.to_i
g = Array.new(m) {[]}
for i in 0..n-1
  gr = gets
  for j in 0..m-1
    g[j] << [gr[j].to_i, i]
	#puts gr[j]
  end
end
u = Array.new(n)
u.fill(0)
g.each do |q|
  q.sort_by! {|e| e[0]}
  q.reverse!
  curr = 0
  u[q[0][1]] = 1
  while curr < n - 1 && q[curr][0] == q[curr + 1][0]
    u[q[curr+1][1]] = 1
	curr += 1
  end
end

puts (u.inject(0) {|sum, curr| sum += curr})

n,m = gets.split(" ")
n = n.to_i
m = n.to_i
g = Array.new(n){Array.new(m)}
for i in 0..n-1
  gr = gets
  for j in 0..m-1
    g[i][j] = gr[j]
	#puts gr[j]
  end
end

count = 0
for i in 0..n-1
  add = false
  for j in 0..m-1
    best = true
    for q in 0..n-1
      if(g[i][j] < g[q][j])
	    best = false
	  end
	end
	if best
	  add = true
	end
  end
  if add
    count += 1
  end
end

puts count


n = gets.chomp.to_i
puts n
n.times do |i|
	print "1 "
end

# Input
n = gets.to_i

# Output
puts n
puts "1 " * n

n = gets.to_i

puts n
puts ("1"*n).split("").join(" ")

a=gets.to_i
puts a
puts ([1]*a).join(" ")

num = gets.chomp
puts num
num.to_i.times do
  puts "1 "
end

n = gets.to_i
puts n
puts (["1"] * n).join(" ")

input = gets.to_i
flag_odd = 0
answer = 9

while input % answer != 0
  answer -= 1
end
  partition_num = input / answer
  answer = input / partition_num

puts partition_num
(partition_num - 1).times do
  print "#{answer} "
end

puts answer

n = gets.to_i
puts(n)
puts ([1] * n).join(' ')

n = gets.to_i
puts n
n.times do |i|
    print 1,(i == n-1 ? "\n" : " ")
end

DBG = true
n = gets.to_i
if n <= 9
  puts "1"
  puts "#{n}"

elsif n%2 == 0
  puts "#{n/2}"
  (n/2).times do
    print "2 "
  end
  puts ""

elsif n%3 == 0
  puts "#{n/3}"
  (n/3).times do
    print "3 "
  end
  puts ""

elsif n%5 == 0
  puts "#{n/5}"
  (n/5).times do
    print "5 "
  end
  puts ""

elsif n%7 == 0
  puts "#{n/7}"
  (n/7).times do
    print "7 "
  end
  puts ""

else
  puts "#{n}"
  (n).times do
    print "1 "
  end
  puts ""
end

def ascan; gets.split.map(&:to_i); end

n = gets.to_i

p n
puts [1]*n*" "

n = gets.to_i
puts n
n.times { print 1, ' ' }

n = gets.chomp.to_i
digits = []
while n > 0 do
	if n >= 9
		n = n - 9
		digits << 9
	else
		digits << n
		break
	end
end
puts digits.size
digits.each do |i|
	print "#{i} "
end

num = gets.chomp
puts num
num.to_i.times do
  puts 1.class
end

num = gets.chomp
num.to_i.times do
  puts "1" + " "
end

num = gets.chomp
num.to_i.times do
  puts 1.to_int
end

num = gets.chomp
num.to_i.times do
  puts 1
end

n = gets.to_i
n.times do |i|
    print 1,(i == n-1 ? "\n" : " ")
end

DBG = true
n = gets.to_i
if n <= 9
  puts "1"
  puts "#{n}"
elsif n%9 == 0
  puts "#{n/9}"
  (n/9).times do
    print "9 "
  end
  puts ""
else
  puts "#{(n/9)+1}"
  (n/9).times do
    print "9 "
  end
  puts "#{n%9}"
end

DBG = true
n = gets.to_i
if n <= 9
  puts "1"
  puts "#{n}"
elsif n%2 == 0
  puts "#{n/2}"
  (n/2).times do
    print "2 "
  end
  puts ""
elsif n%3 == 0
  puts "#{n/3}"
  (n/3).times do
    print "3 "
  end
  puts ""
elsif n%5 == 0
  puts "#{n/5}"
  (n/5).times do
    print "5 "
  end
  puts ""
elsif n%7 == 0
  puts "#{n/7}"
  (n/7).times do
    print "7 "
  end
  puts ""

else
  puts "#{(n/9)+1}"
  (n/9).times do
    print "9 "
  end
  puts "#{n%9}"
end

DBG = true
n = gets.to_i
if n <= 9
  puts "1"
  puts "#{n}"
elsif n%9 == 0
  puts "1"
  (n/9).times do
    print "9 "
  end
  puts ""
else
  puts "2"
  (n/9).times do
    print "9 "
  end
  puts "#{n%9}"
end

n = gets.to_i
puts (n + 8)/9
(n/9).times { print 9, ' ' }
print n%9 if n%9 != 0


MOD = 10 ** 9 + 7
def gs; gets.chomp; end
def gi; gets.to_i; end
def gli; gets.split.map(&:to_i); end
def cyesno(x); puts x ? :Yes : :No; end
def uyesno(x); puts x ? :YES : :NO; end
def array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end

gets.to_i.times do
  x = gets.to_i
  k = x / 3
  if x%3 == 0
    puts "#{k} #{k}"
  elsif x%3 == 1
    puts "#{k + 1} #{k}"
  else
    puts "#{k} #{k + 1}"
  end
end

t = gets().to_i
while(t!=0)
  n = gets().to_i
  if (n==1)
    puts "1 0\n"

  elsif (n==2)
    puts "0 1\n"

  elsif(n==3)
    puts "1 1\n"

  elsif (n==4)
    puts "2 1\n"

  elsif (n==5)
    puts "1 2\n"
  else
    h1 = n/3
    h2 = n/3.0
    if (h2+0.5>=h1+1)
      h = h1 + 1
    else
      h = h1
    end

    l = n - 2*h
    puts "#{l} #{h}\n"
  end
  t-=1
end


class UF
def initialize;@uf=Hash.new{|_,k|k}end
def u a,b;@uf[f a]=@uf[f b]end
def f c;c==@uf[c]?c:@uf[c]=f(@uf[c])end
end

def g;gets.split.map(&:to_i)end
_,m=g
uf=(1..m).map{UF.new}
m.times{
  a,b,c=g
  uf[c-1].u(a,b)
}
gets.to_i.times{
  u,v=g
  p uf.count{|t|t.f(u)==t.f(v)}
}


def cal(str,ans)
  map={'0'=>0,'1'=>1,'?'=>2};
  if str[0].chr=='?' then
    str[0]='1'; cal(str,ans);
    str[0]='0'; cal(str,ans);
    return;
  end
  cnt=[0,0-str.length%2,0];
  str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }
  ans.push("00") if str.length/2<cnt[0]+cnt[2];
  ans.push("11") if str.length/2<cnt[1]+cnt[2];
  return if str.length/2<cnt[0] || str.length/2<cnt[1];
  ans.push("10") if str[0].chr=='0';
  ans.push("01") if str[0].chr=='1';
end

ans=[]
a=gets.chomp.reverse;
cal(a,ans);
ans.sort!.uniq!;
ans.map{|s| puts(s); }

a=gets.split[0]
zero=0
one=0
hatena=0
a.each_char{|i|
        if i=="1" then
                one+=1
        elsif i=="0" then
                zero+=1
        elsif i=="?" then
                hatena+=1
        end
}
minus=zero+hatena+one-2
ans=[0,0,0,0]
res=["00","01","10","11"]
for i in 0..hatena
        z=zero+i
        o=one+hatena-i
        if minus%2==1 then z+=1 end
                if z>o then
                        ans[0]=1
                elsif z<o then
                        ans[3]=1
                else
                        if a[minus+1]=="?" then
                                if hatena-i>0 then ans[1]=1 end
                                if i>0 then ans[2]=1 end
                        elsif a[minus+1]=="1" then
                                ans[1]=1
                        else
                                ans[2]=1
                        end
                end
end
4.times{|i|
        if ans[i]>0 then
                puts res[i]
        end
}

a=gets.split[0]
zero=0
one=0
hatena=0
a.each_char{|i|
        if i=="1" then
                one+=1
        elsif i=="0" then
                zero+=1
        elsif i=="?" then
                hatena+=1
        end
}
minus=zero+hatena+one-2
ans=[0,0,0,0]
res=["00","01","10","11"]
for i in 0..hatena
        z=zero+i
        o=one+hatena-i
        if minus%2==1 then z+=1 end
                if z>o then
                        ans[0]=1
                elsif z<o then
                        ans[3]=1
                else
                        if a[minus+1]=="?" then
                                ans[1]=1
                                ans[2]=1
                        elsif a[minus+1]=="1" then
                                ans[1]=1
                        else
                                ans[2]=1
                        end
                end
end
4.times{|i|
        if ans[i]>0 then
                puts res[i]
        end
}

a=gets.split[0]
zero=0
one=0
hatena=0
a.each_char{|i|
        if i=="1" then
                one+=1
        elsif i=="0" then
                zero+=1
        elsif i=="?" then
                hatena+=1
        end
}
minus=zero+hatena+one-2
ans=[0,0,0,0]
res=["00","01","10","11"]
for i in 0..hatena
        z=zero+i
        o=one+hatena-i
        if minus%2==1 then z+=1 end
                if z>o then
                        ans[0]=1
                elsif z<o then
                        ans[3]=1
                else
                        if a[minus+1]=="?" then
                                if hatena-i>0 then ans[1]=1 end
                                if hatena>0 then ans[2]=1 end
                        elsif a[minus+1]=="1" then
                                ans[1]=1
                        else
                                ans[2]=1
                        end
                end
end
4.times{|i|
        if ans[i]>0 then
                puts res[i]
        end
}

def cal(str,ans)
  map={'0'=>0,'1'=>1,'?'=>2};
  if str[0].chr=='?' then
    str[0]='1'; cal(str,ans);
    str[0]='0'; cal(str,ans);
    return;
  end
  cnt=[0-str.length%2,0,0];
  str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }
  ans.push("00") if str.length/2<cnt[0]+cnt[2];
  ans.push("11") if str.length/2<cnt[1]+cnt[2];
  return if str.length/2<cnt[0] || str.length<cnt[1];
  ans.push("10") if str[0].chr=='0';
  ans.push("01") if str[0].chr=='1';
end

ans=[]
a=gets.chomp.reverse;
cal(a,ans);
ans.sort!.uniq!;
ans.map{|s| puts(s); }

def cal(str,ans)
  map={'0'=>0,'1'=>1,'?'=>2};
  if str[0].chr=='?' then
    str[0]='1'; cal(str,ans);
    str[0]='0'; cal(str,ans);
    return;
  end
  cnt=[0-str.length%2,0,0];
  str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }
  ans.push("00") if str.length/2<cnt[0]+cnt[2];
  ans.push("11") if str.length/2<cnt[1]+cnt[2];
  return if str.length/2<cnt[0] || str.length<cnt[1];
  ans.push("10") if str[0].chr=='0';
  ans.push("01") if str[0].chr=='1';
end

ans=[]
a=gets.chomp.reverse;
cal(a,ans);
ans.sort!.uniq!;
ans.map{|s| p s; }

def cal(str,ans)
  map={'0'=>0,'1'=>1,'?'=>2};
  if str[0].chr=='?' then
    str[0]='1'; cal(str,ans);
    str[0]='0'; cal(str,ans);
    return;
  end
  cnt=[0-str.length%2,0,0];
  str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }
  ans.push("00") if str.length/2<cnt[0]+cnt[2];
  ans.push("11") if str.length/2<cnt[1]+cnt[2];
  return if str.length/2<cnt[0] || str.length/2<cnt[1];
  ans.push("10") if str[0].chr=='0';
  ans.push("01") if str[0].chr=='1';
end

ans=[]
a=gets.chomp.reverse;
cal(a,ans);
ans.sort!.uniq!;
ans.map{|s| puts(s); }


require 'set'
gets.chomp
x0 = gets.chomp.split(' ').map(&:to_i).inject(:+);
x1 = gets.chomp.split(' ').map(&:to_i).inject(:+);
x2 = gets.chomp.split(' ').map(&:to_i).inject(:+);
puts x0-x1
puts x1-x2

#!/usr/bin/env ruby
def f(a,b)
	a[b.size.times.find{|i|
		a[i]<b[i]
	}||b.size]
end
x=$<.drop(1).map{|e|e.split.map(&:to_i).sort}
p f(x[0],x[1]),f(x[1],x[2])

# http://codeforces.com/contest/519/problem/B
class Errors
  def initialize(a, b, c)
    @errors = [a, b, c].map(&:sort)
  end

  def corrected_error(n)
    (0..@errors[n - 1].length).each do |idx|
      return @errors[n - 1][idx] if @errors[n][idx] != @errors[n - 1][idx]
    end
  end
end

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

erros = Errors.new(a, b, c)
puts erros.corrected_error(1)
puts erros.corrected_error(2)

gets.to_i
a=gets.split.map{|x| x.to_i}
b=gets.split.map{|x| x.to_i}
c=gets.split.map{|x| x.to_i}

puts a.inject(0){|sum, elem| elem+sum} - b.inject(0){|sum, elem| elem+sum}
puts b.inject(0){|sum, elem| elem+sum} - c.inject(0){|sum, elem| elem+sum}

gets
sum = gets.split.map { |x| x.to_i }.inject(:+)
sum_two = gets.split.map { |x| x.to_i }.inject(:+)
sum_three = gets.split.map { |x| x.to_i }.inject(:+)
puts sum - sum_two,sum_two - sum_three

def xor(a, b)
  y=0
  for x in a
    y^=x
  end
  for x in b
    y^=x
  end
  y
end
gets
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)
puts xor(a,b)
puts xor(b,c)

class Array
  def subtract_once(values)
    counts = values.inject(Hash.new(0)) { |h, v| h[v] += 1; h }
    reject { |e| counts[e] -= 1 unless counts[e].zero? }
  end
end

num_rows = gets.strip.to_i

first_errors = gets.strip.split(" ").map{|i| i.to_i }
second_errors = gets.strip.split(" ").map{|i| i.to_i }
third_errors = gets.strip.split(" ").map{|i| i.to_i }

first_error = first_errors.subtract_once(second_errors)
second_error = second_errors.subtract_once(third_errors)

puts first_error
puts second_error

def f(a,b)
	p2=0
	l2=b.size
	while p2<l2
		if a[p2]!=b[p2] then
			return a[p2]
		end
		p2=p2+1
	end
	return a[b.size]
end

require 'set'
gets
a=gets.split.map{|e| e.to_i}.sort
b=gets.split.map{|e| e.to_i}.sort
c=gets.split.map{|e| e.to_i}.sort
puts f(a,b)
puts f(b,c)

n = gets.to_i
a = gets.split.map(&:to_i).each.inject(&:+)
b = gets.split.map(&:to_i).each.inject(&:+)
c = gets.split.map(&:to_i).each.inject(&:+)
puts a - b, b - c

class Array
  def subtract_once(values)
    counts = values.inject(Hash.new(0)) { |h, v| h[v] += 1; h }
    reject { |e| counts[e] -= 1 unless counts[e].zero? }
  end
end

n = gets.chomp.to_i

set1 = gets.chomp.split(' ').map { |e| e.to_i }.sort
set2 = gets.chomp.split(' ').map { |e| e.to_i }.sort
set3 = gets.chomp.split(' ').map { |e| e.to_i }.sort

p set1.subtract_once(set2)[0]
p set2.subtract_once(set3)[0]

n = Integer(gets)
get_first = gets.chomp.split
n_first = Array.new
for i in 0...get_first.length
    n_first[i] = Integer(get_first[i])
end
get_second = gets.chomp.split
n_second = Array.new
for i in 0...get_second.length
    n_second[i] = Integer(get_second[i])
end
get_third = gets.chomp.split
n_third = Array.new
for i in 0...get_third.length
    n_third[i] = Integer(get_third[i])
end

n_first = n_first.sort
n_second = n_second.sort
n_third = n_third.sort
first = 0
second = 0
for i in 0...n_second.length
    if n_first[i] != n_second[i]
        first = n_first[i]
        break
    else
        first = n_first[n_second.length]
    end
end
for i in 0...n_third.length
    if n_second[i] != n_third[i]
        second = n_second[i]
        break
    else
        second = n_second[n_third.length]
    end
end

puts first
puts second

#Made By Hasbi Hamza
gets.chomp
a = gets.chomp.split(' ').map(&:to_i).inject(:+);
b = gets.chomp.split(' ').map(&:to_i).inject(:+);
c = gets.chomp.split(' ').map(&:to_i).inject(:+);
puts a-b
puts b-c

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

a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)

def sum(n)
  return n.inject(0){|sum, i| sum+i}
end

puts sum(a) - sum(b)
puts sum(b) - sum(c)

def check(a, b)
  a.zip(b).find { |x| x.first != x.last }.first
end

gets
a = gets.split(' ').map(&:to_i).sort
b = gets.split(' ').map(&:to_i).sort
c = gets.split(' ').map(&:to_i).sort

puts check(a, b)
puts check(b, c)

def check(a, b)
  a.zip(b).each do |x|
    if x.first != x.last
      puts x.first
      break
    end
  end
end

gets
a = gets.split(' ').map(&:to_i).sort
b = gets.split(' ').map(&:to_i).sort
c = gets.split(' ').map(&:to_i).sort

check a, b
check b, c

# 519B

def read_line
  (gets.chomp.split.map &:to_i).sort
end

n = gets.to_i
s = read_line
t = read_line
p = ->(i) { s[i] != t[i] }
puts s[(0...n - 1).find(&p) || n - 1]
s = read_line
puts t[(0...n - 2).find(&p) || n - 2]

gets.to_i
a=gets.split.map{|x| x.to_i}.sort!
b=gets.split.map{|x| x.to_i}.sort!
c=gets.split.map{|x| x.to_i}.sort!

def otlich(a, b)
  for i in 0...a.max
    b<<i
    b.sort!
    if a == b
      b.delete(i)
      return i
    else
      b.delete(i)
    end
  end
end
puts otlich(a, b)
puts otlich(b, c)

gets.to_i
a=gets.split.map{|x| x.to_i}
b=gets.split.map{|x| x.to_i}
c=gets.split.map{|x| x.to_i}

puts a-b
puts b-c

gets
m = gets.split
ans = []
2.times do
  ans << m - gets.split
end
ans.uniq.each do |x| puts x end

n = gets.to_i
h = Hash.new { |hash, key| hash[key] = 0  }
for i in gets.split.map(&:to_i).each
	h[i] += 1
end
for i in gets.split.map(&:to_i).each
	h[i] += 1
end
for i in gets.split.map(&:to_i).each
	h[i] += 1
end
a = 0
b = 0
for i in h
	if i[1] % 3 == 1
		a = i[0]
	end
	if i[1] % 3 == 2
		b = i[0]
	end
end
puts a,b


n = gets.to_i
a = gets.split.collect{|i| i.to_i}
ans = Array.new(2,0)
count = Array.new(n,0)
(1...n).each do |i|
    count[i] = count[i-1]
    count[i] += 1 if a[i-1] == 1
    ans[0] += count[i] if a[i]== 0
end
count = Array.new(n,0)
(n-2).downto(0).each do |i|
    count[i] = count[i+1]
    count[i] += 1 if a[i+1] == 0
    ans[1] += count[i] if a[i]== 1
end
puts ans.min

#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')

n=gets.to_i

cows = gets.split.map(&:to_i)
n1 = 0
s=0

(0..(n-1)).each do |ind|

	if cows[ind] == 1
		n1+=1
	else
		s+=n1
	end

end

puts s

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @n = @dats[0].to_i
    @cows = @dats[1].split(" ").map(&:to_i)
  end

  def solve
    left1 = 0
    ret = 0
    (0..(@n-1)).each do |i|
      left1 += 1 if @cows[i] == 1
      ret += left1 if @cows[i] == 0
    end
    return ret
  end
end

solver = ProblemA.new
print solver.solve

gets
m=gets.split
res=k=0
m.each do |c|
    k+=1 if (c == '1')
    res += k if (c == '0')
end
p res

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
ans = Array.new(2,0)
cnt = 0
(1...n).each do |i|
    cnt += 1 if a[i-1] == 1
    ans[0] += cnt if a[i]== 0
end
cnt = 0
(n-2).downto(0).each do |i|
    cnt += 1 if a[i+1] == 0
    ans[1] += cnt if a[i]== 1
end
puts ans.min

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
count = Array.new(n,0)
(1...n).each do |i|
    count[i] = count[i-1]
    count[i] += 1 if a[i-1] == 1
end
ans = 0
(0...n).each do |i|
    ans += count[i] if a[i]==0
end
count = Array.new(n,0)
(n-2).downto(0).each do |i|
    count[i] = count[i+1]
    count[i] += 1 if a[i+1] == 0
end
tmp = 0
(0...n).each do |i|
    tmp += count[i] if a[i]==1
end
puts [ans,tmp].min

n=gets.to_i
a=gets.split.map(&:to_i)
l=[0]*n
r=[0]*n
ll=rr=0
n.times do |i|
  l[n-i-1]=ll
  ll+=1 if a[n-i-1]==0
  r[i]=rr
  rr+=1 if a[i]==1
end
x=[0,0]
n.times do |i|
  x[0]+=l[i]if a[i]==1
  x[1]+=r[n-i-1]if a[n-i-1]==0
end
p x.min

n=gets.to_i
a=gets.split.map(&:to_i)
l=[]
r=[]
p=q=0
n.times do |i|
  l[n-i-1]=p
  p+=1 if a[n-i-1]==0
  r[i]=q
  q+=1 if a[i]==1
end
x=[0,0]
n.times do |i|
  x[0]+=l[i]if a[i]==1
  x[1]+=r[n-i-1]if a[n-i-1]==0
end
p x.min

n=gets.to_i
a=gets.split.map(&:to_i)
b=c=d=e=0
n.times do |i|
  c+=b if a[n-i-1]==1
  b+=1 if a[n-i-1]==0
  e+=d if a[i]==0
  d+=1 if a[i]==1
end
p [c,e].min


# coding: utf-8
EMPTY = '.'
WALL = '#'
ADDED_WALL = 'X'
PRESERVED = 'P'
n, m, k = gets.split.map(&:to_i)
C = Struct.new(:i, :j)
g = []
ec = [] # empty cells coordinates
n.times { |i|
    r = gets.chomp.split(//)
    for j in 0...m
        if r[j] == EMPTY
            ec << C.new(i, j)
        end
    end
    g << r
}

# k 개 블럭을 이용해 새로운 벽을 놓습니다
# 윗 줄이 완전히 막혀도 상관없지만,
# 블럭을 설치한 후에도 길은 모두 이어져 있어야합니다.
# 즉, 다음과 같이 섬이 만들어져서는 안됩니다.
# n = 4, m = 4
# ####
# ##..
# #.#.
# ###.
# 1. 전체 empty cell 을 카운트 합니다.
# 2. empty cell 을 DFS/BFS 로 순회하여, 1의 수치에서 k개 점을 제외한만큼 확보합니다.
# 3. 여전히 남아있는 셀들을 벽을 설치할 셀로 마크합니다.
# 불행히도 처음에 생각한 이웃한 empty cell이 적은걸 마킹하는 방법은 실패합니다.
#p "empty cells : #{ec.length}, add wall block: #{k}, preserve goal: #{ec.length - k}"
goal = ec.length - k
collected = []
q = [ec.first]
while collected.length < goal
    c = q.shift
    if g[c.i][c.j] != EMPTY
        next
    end
    collected << c
    # p collected
    g[c.i][c.j] = PRESERVED
    #puts "==> preserved #{collected.length}/#{ec.length} #{c}"
    cands = [C.new(c.i+1, c.j), C.new(c.i-1, c.j), C.new(c.i, c.j+1), C.new(c.i, c.j-1)]
    for cand in cands
        if cand.i >= 0 && cand.i < n && cand.j >= 0 && cand.j < m
            if g[cand.i][cand.j] == EMPTY
                q << cand
            end
        end
    end
end

puts g.map(&:join).join("\n").gsub(EMPTY, ADDED_WALL).gsub(PRESERVED, EMPTY)
#puts g.map(&:join).join("").count(EMPTY)

@di = [-1, 0, 1, 0]
@dj = @di.reverse

def ok(i, j)
  return false if i < 0 || i >= @n || j < 0 || j >= @m || @maze[i][j] != '.'
  return true
end

def bfs(si, sj)
  qi = 0
  qj = 0
  q = Array.new(@n * @m)
  q[qj] = [si, sj]
  qj += 1
  while qi < qj
    arr = q[qi]
    qi += 1
    i = arr[0]
    j = arr[1]
    next if @vis[i][j]
    @vis[i][j] = true
    @reached += 1
    break if @reached == @target
    4.times { |d|
      ni = i + @di[d]
      nj = j + @dj[d]
      if ok(ni, nj)
        q[qj] = [ni, nj]
        qj += 1
      end
    }
  end
end

@n, @m, k = gets.strip.split.map(&:to_i)
@maze = Array.new(@n)
@vis = Array.new(@n) {Array.new(@m) { false }}
s = 0
@n.times {|i|
  @maze[i] = gets.strip.split("")
  s += @maze[i].select{|c| c == '.'}.count
}
@reached = 0
@target = s - k
@n.times {|i|
  done = false
  @m.times {|j|
    # puts " > #{i}, #{j}, #{@maze[i][j].class}"
    if @maze[i][j] == '.'
      done = true
      bfs(i, j)
      break
    end
  }
  break if done
}

# puts @maze.map{|s| s.join("")}.join("\n")

@n.times {|i|
  @m.times {|j|
    if @maze[i][j] == '.' && !@vis[i][j]
      @maze[i][j] = 'X'
    end
  }
}
@maze.map!{|r| r.join("")}
out = @maze.join("\n")
$stdout.print out

# Is the curren node valid?
def valid(node, maze)
    return false if node.min < 0
    return false if node[0] >= maze.length
    return false if node[1] >= maze[0].length
    return false if maze[node[0]][node[1]] != "X"
    return true
end

# Nodes connected to node
def children(node, maze)
    children = []
    offsets = [[-1,0],[1,0],[0,-1],[0,1]]
    for offset in offsets
        nebr = [node[0] + offset[0] ,node[1] + offset[1]]
        children.push(nebr) if valid(nebr, maze)
    end
    return children
end

# Run BFS
def bfs(root, maze, depth)
  queue = Array.new(depth)
  queue[0] = root
  visited = Array.new(maze.length) { Array.new(maze[0].length, false) }
  front = 0; back = 1;
  while true
    root = queue[front]
    front = front + 1
    maze[root[0]][root[1]] = "."
    return if front == depth
    for child in children(root, maze) do
        unless visited[child[0]][child[1]]
            queue[back] = child
            back = back + 1
            visited[child[0]][child[1]] = true
        end
    end
  end
end

# Print maze
def print_maze(maze)
  puts maze.map { |row| row.join("") }
end

# Initialize variables
maze = []; root = [0]; depth = 0; c = 0;

# Read input
n, m, k = gets.strip.split.map{|s| s.to_i}
n.times {maze.push(gets.strip.split(""))}

# Convert all . to x
for i in 0..(n - 1) do
     for j in 0..(m - 1) do
          if maze[i][j] == "."
            maze[i][j] = "X"
            c = c + 1
            root = [i,j]
          end
     end
end

# Size of required clearing
depth = c - k

# Solve maze
bfs(root, maze, depth)

# Print maze
print_maze(maze)

# coding: utf-8
EMPTY = '.'
WALL = '#'
ADDED_WALL = 'X'
PRESERVED = 'P'
n, m, k = gets.split.map(&:to_i)
C = Struct.new(:i, :j)
g = []
ec = [] # empty cells coordinates
n.times { |i|
    r = gets.chomp.split(//)
    for j in 0...m
        if r[j] == EMPTY
            ec << C.new(i, j)
        end
    end
    g << r
}

# k 개 블럭을 이용해 새로운 벽을 놓습니다
# 윗 줄이 완전히 막혀도 상관없지만,
# 블럭을 설치한 후에도 길은 모두 이어져 있어야합니다.
# 즉, 다음과 같이 섬이 만들어져서는 안됩니다.
# n = 4, m = 4
# ####
# ##..
# #.#.
# ###.
# 1. 전체 empty cell 을 카운트 합니다.
# 2. empty cell 을 DFS/BFS 로 순회하여, 1의 수치에서 k개 점을 제외한만큼 확보합니다.
# 3. 여전히 남아있는 셀들을 벽을 설치할 셀로 마크합니다.
# 불행히도 처음에 생각한 이웃한 empty cell이 적은걸 마킹하는 방법은 실패합니다.

goal = ec.length - k
collected = []
q = [ec.first]
while collected.length < goal
    c = q.shift
    collected << c
    # p collected
    g[c.i][c.j] = PRESERVED
    cands = [C.new(c.i+1, c.j), C.new(c.i, c.j+1), C.new(c.i-1, c.j), C.new(c.i, c.j-1)]
    for cand in cands
        if cand.i >= 0 && cand.i < n && cand.j >= 0 && cand.j < m
            if g[cand.i][cand.j] == EMPTY
                q << cand
            end
        end
    end
end

puts g.map(&:join).join("\n").gsub(EMPTY, ADDED_WALL).gsub(PRESERVED, EMPTY)

# Is the curren node valid?
def valid(node, maze)
    return false if node.min < 0
    return false if node[0] >= maze.length
    return false if node[1] >= maze[0].length
    return false if maze[node[0]][node[1]] != "X"
    return true
end

# Nodes connected to node
def children(node, maze)
    children = []
    offsets = [[-1,0],[1,0],[0,-1],[0,1]]
    for offset in offsets
        nebr = [node[0] + offset[0] ,node[1] + offset[1]]
        children.push(nebr) if valid(nebr, maze)
    end
    return children
end

# Run BFS
def bfs(root, maze, depth)
  queue = []; at = 0;
  queue.push(root)
  while (queue.size != 0)
    root = queue.shift
    maze[root[0]][root[1]] = "."
    at = at + 1
    return if at == depth
    for child in children(root, maze) do
      queue.push(child)
    end
  end
end

# Run DFS
def dfs(root, traversal, maze, depth)
    maze[root[0]][root[1]] = "."
    traversal.push(root)
    for child in children(root, maze) do
        if traversal.length < depth
            dfs(child, traversal, maze, depth)
        end
    end
end

# Initialize variables
maze = []; root = [0]; depth = 0; traversal = []; c = 0;

# Read input
n, m, k = gets.strip.split.map{|s| s.to_i}
n.times {maze.push(gets.strip.split(""))}

# Convert all . to x
for i in 0..(n - 1) do
     for j in 0..(m - 1) do
          if maze[i][j] == "."
            maze[i][j] = "X"
            c = c + 1
            root = [i,j]
          end
     end
end

# Size of required clearing
depth = c - k

# Solve maze
bfs(root, maze, depth)
# dfs(root, traversal, maze, depth)

# Print maze
for i in 0..(n - 1) do
    puts maze[i].join("")
end

# Is the curren node valid?
def valid(node, maze)
    return false if node.min < 0
    return false if node[0] >= maze.length
    return false if node[1] >= maze[0].length
    return false if maze[node[0]][node[1]] != "x"
    return true
end

# Nodes connected to node
def children(node, maze)
    children = []
    offsets = [[-1,0],[1,0],[0,-1],[0,1]]
    for offset in offsets
        nebr = [node[0] + offset[0] ,node[1] + offset[1]]
        children.push(nebr) if valid(nebr, maze)
    end
    return children
end

# Run DFS
def dfs(root, traversal, maze, depth)
    maze[root[0]][root[1]] = "."
    traversal.push(root)
    for child in children(root, maze) do
        if traversal.length < depth
            dfs(child, traversal, maze, depth)
        end
    end
end

# Initialize variables
maze = []; root = [0]; depth = 0; traversal = []; c = 0;

# Read input
n, m, k = gets.strip.split.map{|s| s.to_i}
n.times {maze.push(gets.strip.split(""))}

# Convert all . to x
for i in 0..(n - 1) do
     for j in 0..(m - 1) do
          if maze[i][j] == "."
            maze[i][j] = "X"
            c = c + 1
            root = [i,j]
          end
     end
end

# Size of required clearing
depth = c - k

# Solve maze
dfs(root, traversal, maze, depth)

# Print maze
for i in 0..(n - 1) do
    puts maze[i].join("")
end

# Is the curren node valid?
def valid(node, maze)
    return false if node.min < 0
    return false if node[0] >= maze.length
    return false if node[1] >= maze[0].length
    return false if maze[node[0]][node[1]] != "X"
    return true
end

# Nodes connected to node
def children(node, maze)
    children = []
    offsets = [[-1,0],[1,0],[0,-1],[0,1]]
    for offset in offsets
        nebr = [node[0] + offset[0] ,node[1] + offset[1]]
        children.push(nebr) if valid(nebr, maze)
    end
    return children
end

# Run BFS
def bfs(root, maze, depth)
  queue = []; at = 0;
  queue.push(root)
  while (queue.size != 0)
    root = queue.shift
    maze[root[0]][root[1]] = "."
    at = at + 1

    puts
    puts at
    print root
    puts
    print_maze(maze)

    return if at == depth
    for child in children(root, maze) do
      queue.push(child) unless queue.include?(child)
    end
  end
end

# Run DFS
def dfs(root, traversal, maze, depth)
    maze[root[0]][root[1]] = "."
    traversal.push(root)
    for child in children(root, maze) do
        if traversal.length < depth
            dfs(child, traversal, maze, depth)
        end
    end
end

def print_maze(maze)
    for i in 0..(maze.length - 1) do
        puts maze[i].join("")
    end
end

# Initialize variables
maze = []; root = [0]; depth = 0; traversal = []; c = 0;

# Read input
n, m, k = gets.strip.split.map{|s| s.to_i}
n.times {maze.push(gets.strip.split(""))}

# Convert all . to x
for i in 0..(n - 1) do
     for j in 0..(m - 1) do
          if maze[i][j] == "."
            maze[i][j] = "X"
            c = c + 1
            root = [i,j]
          end
     end
end

# Size of required clearing
depth = c - k

# Solve maze
bfs(root, maze, depth)
# dfs(root, traversal, maze, depth)

puts
puts depth
puts

# Print maze
print_maze(maze)

# Is the curren node valid?
def valid(node, maze)
    return false if node.min < 0
    return false if node[0] >= maze.length
    return false if node[1] >= maze[0].length
    return false if maze[node[0]][node[1]] != "x"
    return true
end

# Nodes connected to node
def children(node, maze)
    children = []
    offsets = [[-1,0],[1,0],[0,-1],[0,1]]
    for offset in offsets
        nebr = [node[0] + offset[0] ,node[1] + offset[1]]
        children.push(nebr) if valid(nebr, maze)
    end
    return children
end

# Run DFS
def dfs(root, traversal, maze, depth)
    maze[root[0]][root[1]] = "."
    traversal.push(root)
    for child in children(root, maze) do
        if traversal.length < depth
            dfs(child, traversal, maze, depth)
        end
    end
end

# Initialize variables
maze = []; root = [0]; depth = 0; traversal = []; c = 0;

# Read input
n, m, k = gets.strip.split.map{|s| s.to_i}
n.times {maze.push(gets.strip.split(""))}

# Convert all . to x
for i in 0..(n - 1) do
     for j in 0..(m - 1) do
          if maze[i][j] == "."
            maze[i][j] = "x"
            c = c + 1
            root = [i,j]
          end
     end
end

# Size of required clearing
depth = c - k

# Solve maze
dfs(root, traversal, maze, depth)

# Print maze
for i in 0..(n - 1) do
    puts maze[i].join("")
end

# Is the curren node valid?
def valid(node, maze)
    return false if node.min < 0
    return false if node[0] >= maze.length
    return false if node[1] >= maze[0].length
    return false if maze[node[0]][node[1]] != "X"
    return true
end

# Nodes connected to node
def children(node, maze)
    children = []
    offsets = [[-1,0],[1,0],[0,-1],[0,1]]
    for offset in offsets
        nebr = [node[0] + offset[0] ,node[1] + offset[1]]
        children.push(nebr) if valid(nebr, maze)
    end
    return children
end

# Run BFS
def bfs(root, maze, depth)
  queue = Array.new(depth)
  queue[0] = root
  visited = Array.new(maze.length) { Array.new(maze[0].length, false) }
  front = 0; back = 1;
  while true
    root = queue[front]
    front = front + 1
    maze[root[0]][root[1]] = "."
    return if front == depth
    for child in children(root, maze) do
        unless visited[child[0]][child[1]]
            queue[back] = child
            back = back + 1
        end
    end
  end
end

# Print maze
def print_maze(maze)
  puts maze.map { |row| row.join("") }
end

# Initialize variables
maze = []; root = [0]; depth = 0; c = 0;

# Read input
n, m, k = gets.strip.split.map{|s| s.to_i}
n.times {maze.push(gets.strip.split(""))}

# Convert all . to x
for i in 0..(n - 1) do
     for j in 0..(m - 1) do
          if maze[i][j] == "."
            maze[i][j] = "X"
            c = c + 1
            root = [i,j]
          end
     end
end

# Size of required clearing
depth = c - k

# Solve maze
bfs(root, maze, depth)

# Print maze
print_maze(maze)


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

acc_x = 0
acc_y = 0
vis_x = Array.new(n + 1)
vis_y = Array.new(n + 1)
cnt = n * n
ans = []

m.times do
	x, y = gets.split.map { |x| x.to_i }
	if !vis_x[x]
		vis_x[x] = true
		cnt -= n - acc_y
		acc_x += 1
	end
	if !vis_y[y]
		vis_y[y] = true
		cnt -= n - acc_x
		acc_y += 1
	end
	ans << cnt
end
puts ans.join ' '

n,m=gets.split.map{|e| e.to_i}
ans=[]
xs={}
ys={}
n2=n*n

m.times{
	x,y=gets.split.map{|e| e.to_i}
	f1=xs.key?(x)
	f2=ys.key?(y)
	if f1==true && f2==true then
		#
	elsif f1==false && f2==true then
		n2=n2-(n-ys.size)
	elsif f1==true && f2==false then
		n2=n2-(n-xs.size)

	else
		n2=n2-(n*2-xs.size-ys.size)+1
	end
	ans<<n2
	xs[x]=1
	ys[y]=1
}
puts ans*" "

require "set"
n, m = gets.split.map(&:to_i)
ans = []
rows_with_rooks = Set.new
cols_with_rooks = Set.new

m.times do |i|
	r, c = gets.split.map(&:to_i)
	rows_with_rooks << r
	cols_with_rooks << c
	ans << (n-rows_with_rooks.size)*(n-cols_with_rooks.size)
end
puts ans.join(" ")

n, m = gets.chomp.split.map(&:to_i)
ans = []
files = Hash.new(0)
ranks = Hash.new(0)
m.times do
  x, y = gets.chomp.split.map(&:to_i)
  files[x] = 1
  ranks[y] = 1
  ans << (n - files.size) * (n - ranks.size)
end
puts ans.*' '

require 'set'
n,m = gets.split.map(&:to_i)
r,c = Set.new, Set.new
m.times do
  x,y = gets.split.map(&:to_i)
  r << x
  c << y
  print (n-r.size) * (n-c.size), " "
end

def getRest n, rowslength, colslength
  count = n*n;
  count = count - (n * rowslength);
  count = count - ((n - rowslength) * colslength);
  return count;
end

n,m = gets.split(" ").map{|i| i.to_i}
count = n*n;
rows = {};
rowsC = 0;
cols = {};
colsC = 0;
res = "";

m.times do |i|
  x, y = gets.split(" ").map{|i| i.to_i}
  if rows[x].nil?
    rows[x] = 1;
    rowsC = rowsC + 1
  end
  if cols[y].nil?
    cols[y] = 1;
    colsC = colsC + 1
  end
  res << getRest(n, rowsC, colsC).to_s
  if i != (m-1)
    res << " "
  end
end

puts res

n, m = gets.chomp.split.map(&:to_i)
cols = {}
rows = {}
ans = []
m.times do
  i, j = gets.chomp.split.map(&:to_i)
  rows[i] = true
  cols[j] = true

  ans << (n - rows.length) * (n - cols.length)
end
puts ans.join(' ')

#!/usr/bin/ruby

require 'set'

n, m = gets.chomp.split.map(&:to_i)
col = Array.new(n+1){ |i| 0 }
row = Array.new(n+1){ |i| 0 }
scol = Set.new
srow = Set.new
res = n * n
ans = []
for i in 0...m do
  x, y = gets.chomp.split.map(&:to_i)
  unless col[x] == 1 && row[y] == 1
    csz = scol.size
    rsz = srow.size
    cmr = n - 1 - csz + col[x]
    rmr = n - 1 - rsz + row[y]
    if col[x] == 1
      res -= cmr
    elsif row[y] == 1
      res -= rmr
    else
      res -= rmr + cmr + 1
    end
  end
  col[x] = row[y] = 1
  scol << x
  srow << y
  ans.push( res )
end
puts ans.join(" ")

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.chomp end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

N, M = gis
rooks = ngis M

ans = []

w = N
h = N

rows = [true] * N
cols = [true] * N

rooks.each do |y, x|
    if rows[y - 1]
        h -= 1
        rows[y - 1] = false
    end
    if cols[x - 1]
        w -= 1
        cols[x - 1] = false
    end
    ans << h * w
end

puts ans * ' '

n,m=gets.split.map{|e| e.to_i}
ans=[]
xs={}
ys={}
n2=n*n

m.times{
	x,y=gets.split.map{|e| e.to_i}
	f1=xs.key?(x)
	f2=ys.key?(y)
	if f1==true && f2==true then
		#
	elsif f1==false && f2==true then
		n2=n2-(n-xs.size)
	elsif f1==true && f2==false then
		n2=n2-(n-ys.size)
	else
		n2=n2-(n*2-xs.size-ys.size)+1
	end
	ans<<n2
	xs[x]=1
	ys[y]=1
}
puts ans*" "


T = gets.to_i
T.times do
  a,b,c,d,k = gets.split.map(&:to_f)
  x = (a/c).ceil
  y = (b/d).ceil
  if x + y <= k
    puts "#{x} #{y}"
  else
    p -1
  end
end

readline.to_i.times do
  a,b,c,d,k = readline.split.map(&:to_i)
  x,y = ((a-1)/c)+1,((b-1)/d)+1
  if x+y <= k
    print(x," ",y,"\n")
  else
    p -1
  end
end

t = gets.to_i
t.times do
  a, b, c, d, k = gets.split.map(&:to_f)
  s = (a/c).ceil.to_i
  t = (b/d).ceil.to_i
  if s + t <= k
    puts "#{s} #{t}"
  else
    puts -1
  end
end

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

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

#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end
def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
t = gi
ans=[]

t.times do |i|
  a,b,c,d,k=gsmi
  c1 = (a.to_f/c).ceil
  c2 = (b.to_f/d).ceil
  if k-c1<c2
    ans << -1
  else
    ans << [c1, k-c1].join(" ")
  end
end
puts ans

t = gets.to_i
t.times do
  a, b, c, d, k = gets.split.map &:to_i
  x, y = (a + c - 1)/c, (b + d - 1)/d
  puts x + y <= k ? "#{x} #{y}" : -1
end

t = gets.to_i
t.times do
  a, b, c, d, k = gets.split.map &:to_i
  x, y = (a + c)/c, (b + d)/d
  puts x + y <= k ? "#{x} #{y}" : -1
end


t=gets.to_i
(1).upto(t) do
  s=gets.strip.split('')
  ans=0
  flag=false
  counter=0
  s.each do |i|
    flag=true if i=='1'
    ans=counter if i=='1'
    counter+=1 if i=='0' and flag
  end
  puts ans
end

n = gets.to_i

n.times {
  s = gets.chomp
  while s[0] == "0"
    s = s[1..-1]
  end
  s = s.reverse
  while s[0] == "0"
    s = s[1..-1]
  end
  puts s.count("0")
}

1.upto(gets.to_i) do
  s = gets.chomp

  s.gsub!(/^0*/, '')
  s.gsub!(/0*$/, '')

  puts s.count('0')
end

T = gets.to_i

T.times do
  s = gets.chomp
  l = s.size
  ans = 0
  mode = :none
  cnt = 0

  (l - 1).times do |i|
    if mode == :count
      cnt += 1
    end

    if s[i] == '1' && s[i + 1] == '0'
      mode = :count
    elsif s[i] == '0' && s[i + 1] == '1'
      ans += cnt
      cnt = 0
      mode = :none
    end
  end

  puts ans
end


t = gets.to_i
while t.positive?
  a, b = gets.split.map(&:to_i)
  if b == 1
    puts 'NO'
  else
    puts 'YES'
    print "#{a * b} #{a} #{a * b + a}\n"
  end
  t -= 1
end

t = gets.chomp.to_i

t.times do
  a, b = gets.chomp.split(" ").map(&:to_i)
  if b == 1
    puts "NO"
    next
  end
  puts "YES"
  puts "#{a} #{a*b} #{a*(b+1)}"
end

$, = " "
gets.to_i.times do
    a, b = gets.split.map &:to_i
    if b == 1
        puts "NO"
    else
        puts "YES"
        print a, a * b, a * (b + 1), "\n"
    end
end

t = gets.chomp.to_i

t.times do
  a, b = gets.chomp.split(" ").map(&:to_i)
  puts "YES"
  puts "#{a} #{a*(b-1)} #{a*b}"
end


gets.to_i.times{
a=gets.split.map &:to_i
a[a[2]%3^1]=0
p a[0]^a[1]
}

t = readline.to_i

t.times do
  a,b,n = readline.split.map(&:to_i)
  case n % 3
  when 0
    puts a
  when 1
    puts b
  when 2
    puts a^b
  end
end

readline.to_i.times do
  a,b,n = readline.split.map(&:to_i)
  p [a,b,a^b][n%3]
end

gets.to_i.times do
    a,b,n = gets.split.map(&:to_i)
    if n%3 == 0
        puts a
    elsif n%3 == 1
        puts b
    else
        puts a^b
    end
end

def solve
	n = gets.strip.split(' ').map(&:to_i)
	a1 = [n[0], n[1], n[0] ^ n[1]]
	puts a1[n[2] % 3]
end

gets.strip.to_i.times {
	solve
}

gets.to_i.times do
  a, b, n = gets.split.map &:to_i
  p [a, b, a ^ b][n%3]
end

def solve
	n = gets.strip.split(' ').map(&:to_i)
	a1 = [n[0], n[1], n[0] ^ n[1]]
	if n[2] < 3
		puts a1[n[2]]
	else
		a1[0], a1[1] = a1[1], a1[0]
		puts a1[n[2] % 3]
	end
end

gets.strip.to_i.times {
	solve
}


i = 1
n = gets.chomp.to_i
while i < n
  print "#{i} "
  i += 2
end
i -= 2
print "#{n} " if n % 2 == 1 and n != i
while i > 0
  print "#{i} "
  i -= 2
end
i = 2;
while i < n
  print "#{i} "
  i += 2
end
i -= 2
print "#{n} " if n % 2 == 0 and n != i
while i > 0
  print "#{i} "
  i -= 2
end
i = 2
puts n

n = gets.to_i
a = []
if n.odd? then
    1.step(n, 2) do |i|
        a.push i
    end
    (n-2).step(1, -2) do |i|
        a.push i
    end
    a.push n
    2.step(n, 2) do |i|
        a.push i
    end
    (n-1).step(2, -2) do |i|
        a.push i
    end
else
    1.step(n, 2) do |i|
        a.push i
    end
    (n-1).step(1, -2) do |i|
        a.push i
    end
    a.push n
    2.step(n, 2) do |i|
        a.push i
    end
    (n-2).step(2, -2) do |i|
        a.push i
    end
end
puts a.join(' ')


RECURSIVE = false

def fact(n, p)
  return 1.upto(n).reduce(1.0) {|acc, x| (acc * x) % p } unless RECURSIVE

  if n <= 1
    return 1
  else
    return (n * fact(n - 1, p)) % p
  end
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_f
  a = gets.to_s.split.map {|x| x.to_i }

  mask = ~a.reduce(0xFFFFFFFF) {|acc, x| acc & x }
  zerocnt = a.reduce(0) {|acc, val| acc + (((val & mask) == 0) ? 1 : 0) }

  p = 10 ** 9 + 7
  permcnt = fact(n - 2, p) * zerocnt % p * (zerocnt - 1) % p
  printf("%d\n", permcnt)
end

RECURSIVE = false

def fact(n, p)
  return 1.upto(n).reduce(1) {|acc, x| (acc * x) % p } unless RECURSIVE

  if n <= 1
    return 1
  else
    return (n * fact(n - 1, p)) % p
  end
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_f
  a = gets.to_s.split.map {|x| x.to_i }

  mask = ~a.reduce(0xFFFFFFFF) {|acc, x| acc & x }
  zerocnt = a.reduce(0) {|acc, val| acc + (((val & mask) == 0) ? 1 : 0) }

  permcnt = fact(n - 2, 10 ** 9 + 7) * zerocnt * (zerocnt - 1)
  printf("%d\n", permcnt % (10 ** 9 + 7))
end

def fact(n, p)
  result = 1
  for i in (1 .. n) do
    result = (result * i) % p
  end
  result
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  bitcounters = [0] * 32
  n = gets.to_s.to_i
  a = gets.to_s.split.map {|x| x.to_i }

  valmask = 0
  0.upto(31) do |bitidx|
    bitmask = 1 << bitidx
    a.each do |val|
       if val & bitmask != 0
         bitcounters[bitidx] += 1
       end
    end
    valmask |= bitmask if bitcounters[bitidx] != a.size
  end

  zerocnt = 0
  a.each {|val| zerocnt += 1 if (val & valmask) == 0 }

  permcnt = fact(n - 2, 10 ** 9 + 7) * zerocnt * (zerocnt - 1)
  printf("%d\n", permcnt % (10 ** 9 + 7))
end

RECURSIVE = false

def fact(n, p)
  return 1.upto(n).reduce(1.0) {|acc, x| (acc * x) % p } unless RECURSIVE

  if n <= 1
    return 1
  else
    return (n * fact(n - 1, p)) % p
  end
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_f
  a = gets.to_s.split.map {|x| x.to_i }

  mask = ~a.reduce(0xFFFFFFFF) {|acc, x| acc & x }
  zerocnt = a.reduce(0) {|acc, val| acc + (((val & mask) == 0) ? 1 : 0) }

  permcnt = fact(n - 2, 10 ** 9 + 7) * zerocnt * (zerocnt - 1)
  printf("%d\n", permcnt % (10 ** 9 + 7))
end

def fact(n, p)
  result = 1
  for i in (1 .. n) do
    result = (result * i) % p
  end
  result
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  bitcounters = [0] * 32
  n = gets.to_s.to_i
  a = gets.to_s.split.map {|x| x.to_i }

  valmask = 0
  0.upto(31) do |bitidx|
    bitmask = 1 << bitidx
    a.each do |val|
       if val & bitmask != 0
         bitcounters[bitidx] += 1
       end
    end
    valmask |= bitmask if bitcounters[bitidx] != a.size
  end

  zerocnt = 0
  a.each {|val| zerocnt += 1 if (val & valmask) == 0 }

  permcnt = (fact(n, 10 ** 9 + 7) * zerocnt * (zerocnt - 1) / (n * (n - 1)))
  printf("%d\n", permcnt % (10 ** 9 + 7))
end


require 'scanf'

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

n, m = scanf("%d%d\n")
d = Array.new(n) { 0 }

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = scanf("%d%d\n")
#	puts "a = #{a}, b = #{b}"
	a -= 1
	b -= 1
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
#puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in (n-1).downto(i)
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
				d[i] += 1
				d[j] += 1
			end
		end
	end
end

require 'scanf'

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

n, m = scanf("%d%d\n")
d = Array.new(n) { 0 }

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = gets.chomp.scanf("%d %d")
#	puts "a = #{a}, b = #{b}"
	a -= 1
	b -= 1
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
#puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in (n-1).downto(i)
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
				d[i] += 1
				d[j] += 1
			end
		end
	end
end

require 'scanf'

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

n, m = scanf("%d %d\n")
d = Array.new(n) { 0 }

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = scanf("%d %d\n")
#	puts "a = #{a}, b = #{b}"
	a -= 1
	b -= 1
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
#puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in (n-1).downto(i)
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
				d[i] += 1
				d[j] += 1
			end
		end
	end
end

require 'scanf'

n, m = scanf("%d %d")
d = Array.new(n) { 0 }

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = scanf("%d %d").map { |x| x - 1 }
	puts "a = #{a}, b = #{b}"
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
			end
		end
	end
end

require 'scanf'
require 'matrix'

class Matrix
	def []=(i,j,x)
		@rows[i][j] = x
	end
end

n, m = scanf("%d %d")
g = Matrix.build(n) { 0 }
d = Array.new(n) { 0 }

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = scanf("%d %d").map { |x| x - 1 }
	g[a, b] = 1
	g[b, a] = 1
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
			end
		end
	end
end

require 'scanf'

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

n, m = scanf("%d%d")
d = Array.new(n) { 0 }

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = scanf("%d%d")
	puts "a = #{a}, b = #{b}"
	a -= 1
	b -= 1
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
			end
		end
	end
end

require 'scanf'

n, m = scanf("%d %d")
d = Array.new(n) { 0 }

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = scanf("%d %d").map { |x| x - 1 }
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
			end
		end
	end
end

require 'scanf'

n, m = scanf("%d%d")
d = Array.new(n) { 0 }

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = scanf("%d%d")
	puts "a = #{a}, b = #{b}"
	a -= 1
	b -= 1
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
			end
		end
	end
end

require 'scanf'

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

n, m = scanf("%d%d")
d = Array.new(n) { 0 }

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = scanf("%d%d")
#	puts "a = #{a}, b = #{b}"
	a -= 1
	b -= 1
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
#puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in (n-1).downto(i)
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
				d[i] += 1
				d[j] += 1
			end
		end
	end
end

require 'scanf'

n, m = scanf("%d %d")
d = Array.new(n) { 0 }

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = scanf("%d %d")
	puts "a = #{a}, b = #{b}"
	a -= 1
	b -= 1
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
			end
		end
	end
end

require 'scanf'

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

n, m = scanf("%d%d\n")
d = Array.new(n) { 0 }

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = gets.chomp.scanf("%d %d")
#	puts "a = #{a}, b = #{b}"
	a -= 1
	b -= 1
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
#puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
			end
		end
	end
end

require 'scanf'

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

n, m = scanf(" %d%d")
d = Array.new(n) { 0 }

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = scanf(" %d%d")
#	puts "a = #{a}, b = #{b}"
	a -= 1
	b -= 1
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
#puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in (n-1).downto(i)
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
				d[i] += 1
				d[j] += 1
			end
		end
	end
end

require 'scanf'

class UnionFindSet
	def initialize(n)
		@n = n
		@bel = Array.new(n) { |x| x }
	end

	def find(x)
		@bel[x] = find(@bel[x]) unless @bel[x] == x
		@bel[x]
	end

	def union(x, y)
		@bel[find(x)] = find(y)
	end
end

n, m = scanf("%d%d\n")
d = Array.new(n) { 0 }

uf = UnionFindSet.new(n)
joinEdge = 0
for i in 1..m
	a, b = gets.chomp.scanf("%d %d")
	puts "a = #{a}, b = #{b}"
	a -= 1
	b -= 1
	d[a] += 1
	d[b] += 1
	joinEdge += 1 if uf.find(a) == uf.find(b)
	uf.union(a, b)
end

maxDeg = d.max
puts "max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}"

if maxDeg > 2
	puts "NO"
elsif joinEdge > 0
	if maxDeg == 2 and joinEdge == 1 and n == m
		puts "YES"
		puts "0"
	else
		puts "NO"
	end
else
	puts "YES"
	puts "#{n - m}"
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)
				puts "#{i+1} #{j+1}"
				uf.union(i, j)
				d[i] += 1
				d[j] += 1
			end
		end
	end
	for i in 0...n
		for j in i+1...n
			if d[i] < 2 and d[j] < 2
				puts "#{i+1} #{j+1}"
			end
		end
	end
end


n = gets.chop.to_i
a = gets.chop.split(/ /).map!{|x| x.to_i}
pa = Array.new
ma = Array.new
bzero = false
for i in 0..n-1
	if a[i] == 0
		bzero =  true
	elsif a[i] > 0
		pa.push(a[i])
	else
		ma.push(a[i])
	end
end
ma.sort!{|a,b| a <=> b }
mac = (ma.length / 2)*2
ret = ""
for i in 0..mac-1
	ret << " " if ret.length > 0
	ret << ma[i].to_s
end
for i in 0..pa.length-1
	ret << " " if ret.length > 0
	ret << pa[i].to_s
end

if n == 1
	puts a[0]
elsif ret == "" && bzero
	puts "0"
else
	puts ret
end

n = gets.to_i
costs = gets.split.map { |v| v.to_i }

positive = costs.select { |v| v > 0 }.sort
negative = costs.select { |v| v < 0 }.sort

negative.pop if negative.size.odd?

result = negative + positive
result = Array(costs.max) if result.empty?

puts result.join(' ')

gets
a=gets.split.map &:to_i
x=a.delete 0
puts(if a.size<1
	0
elsif a.size<2
	x ? [a[0],x].max : a[0]
elsif a.count{|_|_<0}%2<1
	a*" "
else
	a.delete_at a.each_with_index.select{|v,i|v<0}.sort[-1][1]
	a*" "
end)

gets
a=gets.split.map &:to_i
a.delete 0
puts(if a.size<1
	0
elsif a.size<2
	a[0]
elsif a.count{|_|_<0}%2<1
	a*" "
else
	a.delete_at a.each_with_index.select{|v,i|v<0}.sort[-1][1]
	a*" "
end)

n = gets.chop.to_i
a = gets.chop.split(/ /).map!{|x| x.to_i}
pa = Array.new
ma = Array.new
bzero = false
for i in 0..n-1
	bzero =  true if a[i] == 0
	if a[i] > 0
		pa.push(a[i])
	else
		ma.push(a[i])
	end
end
ma.sort!{|a,b| b <=> a }
mac = (ma.length / 2)*2
ret = ""
for i in 0..mac-1
	ret << " " if i > 0
	ret << ma[i].to_s
end
for i in 0..pa.length-1
	ret << " " if i > 0
	ret << pa[i].to_s
end

if n == 1
	puts a[0]
elsif ret == "" && bzero
	puts "0"
else
	puts ret
end

n = gets.chop.to_i
a = gets.chop.split(/ /).map!{|x| x.to_i}
pa = Array.new
ma = Array.new
bzero = false
for i in 0..n-1
	bzero =  true if a[i] == 0
	if a[i] > 0
		pa.push(a[i])
	else
		ma.push(a[i])
	end
end
ma.sort!{|a,b| a <=> b }
mac = (ma.length / 2)*2
ret = ""
for i in 0..mac-1
	ret << " " if ret.length > 0
	ret << ma[i].to_s
end
for i in 0..pa.length-1
	ret << " " if ret.length > 0
	ret << pa[i].to_s
end

if n == 1
	puts a[0]
elsif ret == "" && bzero
	puts "0"
else
	puts ret
end

n = gets.chop.to_i
a = gets.chop.split(/ /).map!{|x| x.to_i}
pa = Array.new
ma = Array.new
bzero = false
for i in 0..n-1
	bzero =  true if a[i] == 0
	if a[i] > 0
		pa.push(a[i])
	else
		ma.push(a[i])
	end
end
ma.sort!{|a,b| b <=> a }
mac = (ma.length / 2)*2
ret = ""
for i in 0..mac-1
	ret << " " if i > 0
	ret << ma[i].to_s
end
for i in 0..pa.length-1
	ret << " " if i > 0
	ret << pa[i].to_s
end

if ret == "" && bzero
	puts "0"
else
	puts ret
end


# coding: utf-8

# if __FILE__ == $0
# end

na = gets.to_i
ar = gets.split.map(&:to_i)
nb = gets.to_i
br = gets.split.map(&:to_i)

catch :found do
    ar.each { |ia|
        br.each { |ib|
            s = ia + ib
            if ar.index(s) == nil && br.index(s) == nil
                puts "#{ia} #{ib}"
                throw :found
            end
        }
    }
end

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

printf "%d %d\n", a.max, b.max

a_size = gets.strip.to_i
a_string = gets.strip

b_size = gets.strip.to_i
b_string = gets.strip

a = a_string.split(' ').map(&:to_i)
b = b_string.split(' ').map(&:to_i)

output = nil

a.each_with_index do |a_element, _a_index|
  b.each_with_index do |b_element, _b_index|
    sum = a_element + b_element
    next if a.include?(sum) || b.include?(sum)

    output = "#{a_element} #{b_element}"
    break
  end

  break unless output.eql?(nil)
end

puts output

n = gets.to_i
as = gets.split.map(&:to_i)
m = gets.to_i
bs = gets.split.map(&:to_i)

puts "#{as.max} #{bs.max}"

gets
a = gets.split(' ').map(&:to_i)
gets
b = gets.split(' ').map(&:to_i)
puts a.max.to_s + ' ' + b.max.to_s

n = gets.to_i
aa = gets.split.map(&:to_i).sort
m = gets.to_i
bb = gets.split.map(&:to_i).sort

puts "#{aa.last} #{bb.last}"

n = gets.to_i
a = gets.split.map &:to_i
m = gets.to_i
b = gets.split.map &:to_i
print a.max, ' ', b.max

gets
a = gets.split(' ').map(&:to_i)
gets
b = gets.split(' ').map(&:to_i)
puts a.max + b.max


def solution(arr)
    odd, even = 0, 0
    arr.each do |e|
        if e % 2 == 0
            even += 1
        else
            odd += 1
        end
    end
    puts odd < even ? odd : even
end

n = $stdin.gets.to_i
(0...n).each do |i|
    size = $stdin.gets.to_i
    arr = $stdin.gets.to_s.split(' ').map(&:to_i)
    solution(arr)
end

gets.to_i.times do
n = gets
a = gets.split(' ').map(&:to_i)
puts [a.select{|a|a%2==1}.size, a.select{|a|a%2==0}.size].min
end

t = gets.to_i

(0...t).each do
  n = gets.chomp.to_i
  a = gets.chomp.split(" ").map(&:to_i)

  even_count = a.select(&:even?).count
  odd_count = a.count - even_count
  puts [even_count, odd_count].min
end

gets.to_i.times {
	m = gets.to_i
	n = gets.split.map(&:to_i).count{|a| a.even?}
	p [n, m-n].min
}

t = gets.to_i

(0...t).each do
  n = gets.chomp.to_i
  a = gets.chomp.split(" ").map(&:to_i)

  k = 0
  idx = 0
  # prev_idx = 0
  while idx < a.size
    next_id = idx + 1
    # puts "#{prev_idx}-#{idx}-#{a.inspect} - #{k}"
    x = a[idx]
    y = a[next_id]
    break if y.nil?
    sum = x + y

    if sum.even?
      idx += 1
      next
    else
      # if x.even? && y.odd?
        a.delete_at(next_id)

      # elsif y.odd?
      #   a.delete_at(next_id)
        # idx += 1
      # end
      # idx -= 1
      # if a[idx].odd?
        # a.delete_at(idx)
      # end
      k += 1
    end
  end

  puts k

rescue => e
  puts e
  puts a.inspect
  exit(1)
end

t = gets.to_i

(0...t).each do
  n = gets.chomp.to_i
  a = gets.chomp.split(" ").map(&:to_i)

  k = 0
  idx = 0
  # prev_idx = 0
  while idx < a.size
    next_id = idx + 1
    # puts "#{prev_idx}-#{idx}-#{a.inspect} - #{k}"
    x = a[idx]
    y = a[next_id]
    break if y.nil?
    sum = x + y

    if sum.even?
      idx += 1
      next
    else
      if x.odd?
        a.delete_at(idx)

      elsif y.odd?
        a.delete_at(next_id)
      end
      # idx -= 1
      # if a[idx].odd?
        # a.delete_at(idx)
      # end
      k += 1
    end
  end

  puts k

rescue => e
  puts e
  puts a.inspect
  exit(1)
end

t = gets.to_i

(0...t).each do
  n = gets.chomp.to_i
  a = gets.chomp.split(" ").map(&:to_i)

  k = 0
  idx = 0
  # prev_idx = 0
  while idx < a.size
    next_id = idx + 1
    # puts "#{prev_idx}-#{idx}-#{a.inspect} - #{k}"
    x = a[idx]
    y = a[next_id]
    break if y.nil?
    sum = x + y

    if sum.even?
      idx += 1
      next
    else
      if x.odd?
        a.delete_at(idx)
        # idx -= 1
      elsif y.odd?
        a.delete_at(next_id)
      end

      # if a[idx].odd?
        # a.delete_at(idx)
      # end
      k += 1
    end
  end

  puts k

rescue => e
  puts e
  puts a.inspect
  exit(1)
end

t = gets.to_i

(0...t).each do
  n = gets.chomp.to_i
  a = gets.chomp.split(" ").map(&:to_i)

  k = 0
  idx = 1
  while idx < a.size
    prev_idx = idx - 1
    # puts "#{prev_idx}-#{idx}-#{a.size}"
    sum = a[prev_idx] + a[idx]

    if sum.even?
      idx += 1
      next
    else
      # if a[prev_idx].odd?
      #   a.delete_at(prev_idx)
      # elsif a[idx].odd?
      #   a.delete_at(idx)
      # end
      # if a[idx].odd?
        a.delete_at(idx)
      # end
      k += 1
    end
  end

  puts k

# rescue => e
#   puts a.inspect
#   exit(1)
end

t = gets.to_i

(0...t).each do
  n = gets.chomp.to_i
  a = gets.chomp.split(" ").map(&:to_i)

  k = 0
  idx = 1
  while idx < a.size
    prev_idx = idx - 1
    # puts "#{prev_idx}-#{idx}-#{a.size}"
    sum = a[prev_idx] + a[idx]

    if sum.even?
      idx += 2
      next
    else
      if a[prev_idx].odd?
        a.delete_at(prev_idx)
      elsif a[idx].odd?
        a.delete_at(idx)
      end
      k += 1
    end
  end

  puts k

# rescue => e
#   puts a.inspect
#   exit(1)
end

t = gets.to_i

(0...t).each do
  n = gets.chomp.to_i
  a = gets.chomp.split(" ").map(&:to_i)

  k = 0
  idx = 1
  while idx <= a.size
    prev_idx = idx - 1
    sum = a[prev_idx] + a[idx]

    if sum.even?
      idx += 2
      next
    else
      if a[prev_idx].odd?
        a.delete_at(prev_idx)
      elsif a[idx].odd?
        a.delete_at(idx)
      end
      k += 1
    end
  end

  puts k

rescue => e
  puts e
end


N = gets.to_i
as = gets.split.map(&:to_i)
ans = 0
while as.length > 0
    a = as.shift
    ans += as.index(a)
    as.delete(a)
end
puts ans

n = gets.to_i
ary = gets.strip.split.map(&:to_i)

sz = ary.size
ans = 0
0.upto(n-1).each do |i|
  ai = i*2
  e = ary[ai]
  eni = ai + 1
  (ai+1).upto(sz-1).each do |j|
    if ary[j] == e
      eni = j
      break
    end
  end
  eni.downto(ai+2).each do |j|
    ary[j] = ary[j-1]
    ary[j-1] = e
    ans += 1
  end
end
puts ans