r,c=gets.chomp.split.map(&:to_i)
a,b,fr,fc,dr,dc=Array.new(r,0),Array.new(c,0),0,0,0,0
r.times do |i|
	x=gets.chomp.split.map(&:to_i)
	x.each_index do |j|
		if x[j]==1
			a[i]+=1
			b[j]+=1
		end
	end
end
=begin
print a
puts
print b
puts
=end
r.times { |i| (a[i]==c) ? fr+=1 : dr+=a[i] }
c.times { |i| (b[i]==r) ? fc+=1 : dc+=b[i] }
#puts "#{dr} #{dc} #{fr} #{fc} #{r} #{c}"
if (dr!=(r-fr)*fc)||(dc!=(c-fc)*fr)||(fr==0&&fc!=0)||(fr!=0&&fc==0)
	puts "NO"
else
	puts "YES"
	r.times do |i|
		c.times do |j|
			print (a[i]==c && b[j]==r) ? "1 " : "0 "
		end
		puts
	end
end

row_num, col_num = STDIN.readline.split.map {|s| s.to_i }
b = []
row_num.times do
	b.push(STDIN.readline.split.map {|s| s.to_i })
end

or_row = row_num.times.map { 1 }
or_col = col_num.times.map { 1 }

b.each_with_index do |row, r|
	row.each_with_index do |x, c|
		or_row[r] = or_col[c] = 0 if x == 0
	end
end

if or_row.inject(:+) == 0
	col_num.times {|c| or_col[c] = 0 }
end
if or_col.inject(:+) == 0
	row_num.times {|r| or_row[r] = 0 }
end

b.each_with_index do |row, r|
	row.each_with_index do |x, c|
		if x == 1 and or_row[r] == 0 and or_col[c] == 0
			puts 'NO'
			exit
		end
	end
end

a = row_num.times.map { col_num.times.map { 1 } }
or_row.each_with_index do |x, r|
	col_num.times { |c| a[r][c] = 0 } if x == 0
end
or_col.each_with_index do |x, c|
	row_num.times { |r| a[r][c] = 0 } if x == 0
end

puts 'YES'
row_num.times {|r| puts a[r].join ' ' }

M, N = gets.split.map(&:to_i)
val = []
M.times{|m|
    val[m] = gets.split.map(&:to_i).map{|i| i==1}
}

Mallone = []
M.times{|m|
    Mallone[m] = true
    N.times{|n|
        Mallone[m] &= (val[m][n])
       # puts "Mallone[#{m}]=#{Mallone[m]}"
    }
}

Nallone = []
N.times{|n|
    Nallone[n] = true
    M.times{|m|
        Nallone[n] &= (val[m][n])
       # puts "Nallone[#{n}]=#{Nallone[n]}"
    }
}

result = []
M.times{|m|
    result[m] = []
    N.times{|n|
        result[m][n] = false
    }
}

M.times{|m|
    if Mallone[m]
        flag = false
        N.times{|n|
            if Nallone[n]
                flag = true
                result[m][n] = true
            end
        }
        if !flag
            puts "NO"
            exit 0
        end
    else
        N.times{|n|
            if val[m][n]
                if ! Nallone[n]
                    puts "NO"
                    exit 0
                end
            end
        }
    end
}

N.times{|n|
    if Nallone[n]
        flag = false
        M.times{|m|
            if Mallone[m]
                flag = true
                result[m][n] = true
            end
        }
        if !flag
            puts "NO"
            exit 0
        end
    else
        M.times{|m|
            if val[m][n]
                if ! Mallone[m]
                    puts "NO"
                    exit 0
                end
            end
        }
    end
}
puts "YES"
M.times{|m|
    puts result[m].map{|b| (b&&1||0)}.join(" ")
}

def make_zero(i,j,n,m)
	@arr[i] = Array.new(m,0)
	(0..(n-1)).each do |row|
		@arr[row][j] = 0
	end
end

def calc(i,j,n,m)
	row_or = @arr[i].inject(0,&:|)
	col_or = 0
	(0..(n-1)).each do |row|
		col_or |= @arr[row][j]
	end
	row_or | col_or
end

n,m = gets.split(" ").map(&:to_i)
b_arr = []
@arr = []
n.times do
	b_arr << gets.split(" ").map(&:to_i)
	@arr << Array.new(m,1)
end

n.times do |i|
	m.times do |j|
		make_zero(i,j,n,m) if b_arr[i][j] ==0
	end
end

@new_arr = []
n.times do |i|
	@new_arr << []
	m.times do |j|
		@new_arr[i][j] = calc(i,j,n,m)
	end
end
# puts "#{@arr}"
# puts "barr #{b_arr}"
# puts "new #{@new_arr}"
if b_arr == @new_arr
	puts "YES"
	@arr.each do |a|
		puts a.join(" ")
	end
else
	puts "NO"
end

def read_line
  gets.chomp.split.map &:to_i
end

n, m = read_line
a, b = [], []

n.times {
  a << Array.new(m,1)
  b << read_line
}

n.times { |i|
  a[i] = Array.new(m, 0) if b[i].include? 0
}

m.times { |j|
  a.each { |x| x[j] = 0 } if (0...n).map { |i| b[i][j] }.include? 0
}

c = Array.new(n)
n.times { |i|
  c[i] = Array.new(m)
  m.times { |j|
    c[i][j] = (a[i] + (0...n).map { |k| a[k][j] }).inject { |x, y| x|y }
  }
}

if c == b
	puts 'YES'
	a.each { |x|
		puts x*' '
	}
else
	puts 'NO'
end

# 486B

def read_line
  gets.chomp.split.map &:to_i
end

m, n = read_line
a, b = [], []
m.times {
  a << Array.new(n, 1)
  b << read_line
}

m.times { |i|
  a[i] = Array.new(n, 0) if b[i].include? 0
}

n.times { |j|
  a.each { |x| x[j] = 0 } if (0...m).map { |i| b[i][j] }.include? 0
}

c = Array.new(m)
m.times { |i|
  c[i] = Array.new(n)
  n.times { |j|
    c[i][j] = (a[i] + (0...m).map { |k| a[k][j] }).inject { |x, y| x | y }
  }
}

if c == b
  puts 'YES'
  a.each { |x|
    puts x * ' '
  }
else
  puts 'NO'
end

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

b = []
a = Array.new(m) {Array.new(n)}
correct_b = Array.new(m) {Array.new(n, 0)}

m.times do |i|
  b[i] = gets.split.map(&:to_i)
end

b.each_with_index do |arr, i|
  arr.each_with_index do |el, j|
    if el == 0
      m.times {|line| a[line][j] = 0}
      n.times {|col| a[i][col] = 0}
    end
  end
end

is_correct = true

m.times do |i|
  n.times do |j|
    unless a[i][j]
      a[i][j] = 1

      m.times {|line| correct_b[line][j] = 1 }
      n.times {|col| correct_b[i][col] = 1  }
    end
  end
end

m.times do |i|
  break unless is_correct
  n.times do |j|
    if b[i][j] != correct_b[i][j]
      is_correct = false
      break
    end
  end
end

if is_correct
  puts 'YES'
  a.each {|k| puts k.join(' ')}
else
  puts 'NO'
end

# puts
# correct_b.each {|k| puts k.join(' ')}

row_num, col_num = STDIN.readline.split.map {|s| s.to_i }
b = []
row_num.times do
	b.push(STDIN.readline.split.map {|s| s.to_i })
end

or_row = row_num.times.map { 1 }
or_col = col_num.times.map { 1 }

b.each_with_index do |row, r|
	row.each_with_index do |x, c|
		if x == 0
			or_row[r] = 0
			or_col[c] = 0
		end
	end
end

if or_row.inject(:+) == 0
	col_num.times {|c| or_col[c] = 0 }
end
if or_col.inject(:+) == 0
	row_num.times {|r| or_row[r] = 0 }
end

b.each_with_index do |row, r|
	row.each_with_index do |x, c|
		if x == 1
			if or_row[r] == 0 and or_col[c] == 0
				puts 'NO'
				exit
			end
		end
	end
end

a = row_num.times.map { col_num.times.map { 1 } }
or_row.each_with_index do |x, r|
	col_num.times { |c| a[r][c] = 0 } if x == 0
end
or_col.each_with_index do |x, c|
	row_num.times { |r| a[r][c] = 0 } if x == 0
end

puts 'YES'
row_num.times {|r| puts a[r].join ' ' }

row_num, col_num = STDIN.readline.split.map {|s| s.to_i }
b = []
row_num.times do
	b.push(STDIN.readline.split.map {|s| s.to_i })
end

or_row = row_num.times.map { 1 }
or_col = col_num.times.map { 1 }

b.each_with_index do |row, r|
	row.each_with_index do |x, c|
		if x == 0
			or_row[r] = 0
			or_col[c] = 0
		end
	end
end

b.each_with_index do |row, r|
	row.each_with_index do |x, c|
		if x == 1
			if or_row[r] == 0 and or_col[c] == 0
				puts 'NO'
				exit
			end
		end
	end
end

if row_num >= col_num and or_row.inject(:+) == 0
	col_num.times {|c| or_col[c] = 0 }
end
if col_num >= row_num and or_col.inject(:+) == 0
	row_num.times {|r| or_row[r] = 0 }
end

a = row_num.times.map { col_num.times.map { 1 } }
or_row.each_with_index do |x, r|
	col_num.times { |c| a[r][c] = 0 } if x == 0
end
or_col.each_with_index do |x, c|
	row_num.times { |r| a[r][c] = 0 } if x == 0
end

puts 'YES'
row_num.times {|r| puts a[r].join ' ' }

row_num, col_num = STDIN.readline.split.map {|s| s.to_i }
b = []
row_num.times do
	b.push(STDIN.readline.split.map {|s| s.to_i })
end

or_row = row_num.times.map { 1 }
or_col = col_num.times.map { 1 }

b.each_with_index do |row, r|
	row.each_with_index do |x, c|
		if x == 0
			or_row[r] = 0
			or_col[c] = 0
		end
	end
end

b.each_with_index do |row, r|
	row.each_with_index do |x, c|
		if x == 1
			if or_row[r] == 0 and or_col[c] == 0
				puts 'NO'
				exit
			end
		end
	end
end

a = row_num.times.map { col_num.times.map { 1 } }
or_row.each_with_index do |x, r|
	col_num.times { |c| a[r][c] = 0 } if x == 0
end
or_col.each_with_index do |x, c|
	row_num.times { |r| a[r][c] = 0 } if x == 0
end

puts 'YES'
row_num.times {|r| puts a[r].join ' ' }

row_num, col_num = STDIN.readline.split.map {|s| s.to_i }
b = []
row_num.times do
	b.push(STDIN.readline.split.map {|s| s.to_i })
end

or_row = row_num.times.map { 1 }
or_col = col_num.times.map { 1 }

b.each_with_index do |row, r|
	row.each_with_index do |x, c|
		if x == 0
			or_row[r] = 0
			or_col[c] = 0
		end
	end
end

if row_num >= col_num and or_row.inject(:+) == 0
	col_num.times {|c| or_col[c] = 0 }
end
if col_num >= row_num and or_col.inject(:+) == 0
	row_num.times {|r| or_row[r] = 0 }
end

b.each_with_index do |row, r|
	row.each_with_index do |x, c|
		if x == 1
			if or_row[r] == 0 and or_col[c] == 0
				puts 'NO'
				exit
			end
		end
	end
end

a = row_num.times.map { col_num.times.map { 1 } }
or_row.each_with_index do |x, r|
	col_num.times { |c| a[r][c] = 0 } if x == 0
end
or_col.each_with_index do |x, c|
	row_num.times { |r| a[r][c] = 0 } if x == 0
end

puts 'YES'
row_num.times {|r| puts a[r].join ' ' }


a = gets.strip
b = gets.strip
if a == b
    puts "-1"
else
    puts [a.length, b.length].max
end

#!/usr/bin/ruby
a,b=$<.map &:chomp
p a==b ? -1 : [a.size,b.size].max

a = gets.chomp
b = gets.chomp
if a == b
  puts -1
else
  puts [a, b].map(&:length).max
end

a = gets.chomp()
b = gets.chomp()

if a == b
  puts "-1"
elsif a.size > b.size
  puts a.size
else
  puts b.size
end

TEST_MODE = ARGV[0] == 't'

def evaluate(actual, expected); actual == expected ? "OK" : "**NG**"; end
def tests(); yield if TEST_MODE; end
def to_array(val); val.kind_of?(Array) ? val : [val]; end
def parse_int_array(str); str.split(" ").collect{|s| s.to_i}; end
def p(*arg); puts "DEBUG: " + arg.to_s if TEST_MODE; end

def t(input, expected_output)
  input = to_array(input)
  expected_output = to_array(expected_output)
  output = []
  solve(input, output)
  puts "#{evaluate(output, expected_output)} : input=#{input.to_s}, expected=#{expected_output.to_s}, actual=#{output.to_s}"
end

# actual codes here

def solve(input, output)
  a, b = input[0], input[1]
  if a == b
    output << "-1"
  elsif a.length == b.length
    output << a.length
  else
    output << [a.length, b.length].max.to_s
  end
end

tests do
  # test cases
  t(["abcd", "defgh"], "5")
  t(["a", "a"], "-1")
  t(["abc", "b"], "3")
  t(["abcde", "acde"], "5")
end

puts solve(STDIN.read.split("\n"), []) unless TEST_MODE

def f(a,b)
	p1=0
	p2=0
	while p1<b.size && p2<a.size
		if a[p2]==b[p1] then
			p1+=1
		end
		p2+=1
	end
	return p1
end
a=gets.chomp
b=gets.chomp
b1=f(a,b)
a1=f(b,a)

if b1==b.size && a1==a.size then
	puts -1
else
	puts [a.size,b.size].max
end

a=gets.chomp
b=gets.chomp
n=a.size
m=b.size

if a==b
    puts -1
else
    puts [n, m].max
end

first = gets.chop
second = gets.chop
last_name = "asdsad"

if first == second
        print "-1"
    else
        print [first.length, second.length].max
    end

while !STDIN.eof? && (string1 = STDIN.readline.strip) && (string2 = STDIN.readline.strip)
  if string1 == string2
    puts "-1"
  else
    puts [string1.length, string2.length].max
  end
end

# coding: utf-8

# if __FILE__ == $0
# end

s1 = gets.chomp
s2 = gets.chomp

if s1 == s2
    puts -1
else
    puts [s1.length, s2.length].max
end

module Memoizer
  def memoize(methodname)
    raise ArgumentError, "statefull method [#{methodname}]should not be memoized" if methodname.match(/\!\z/)
    unbound_method = instance_method(methodname)
    raise ArgumentError, "method [#{methodname}] with argument cannot be memoized" if unbound_method.arity > 0
    memoized_object_name = "@_memoized_#{methodname}"
    define_method(methodname) do
      if(instance_variable_get(memoized_object_name).nil?)
        instance_variable_set(memoized_object_name, unbound_method.bind(self).call)
      end
      instance_variable_get(memoized_object_name)
    end
  end
end

class Problem
  attr_reader(:a, :b)
  def initialize(a, b)
    @a = a
    @b = b
  end

  def ans
    return -1 if a == b
    return [a.length, b.length].max
  end

  def print!
    puts(ans)
  end
end

a = gets.strip
b = gets.strip
Problem.new(a, b).print!

a = gets.chomp
b = gets.chomp

def max a, b
	if a > b
		return a
	else
		return b
	end
end

if a == b
	puts -1
else
	puts max(a.size, b.size)
end

#!/usr/bin/ruby

a = gets.chomp
b = gets.chomp

if a.size > b.size then
	puts a.size
elsif a.size < b.size then
	puts b.size
elsif a == b then
	puts -1
else
	puts a.size
end

#!/usr/bin/env ruby

a = gets.strip
b = gets.strip

a_size = a.size
b_size = b.size

cmp = a_size <=> b_size
if cmp == 1
  puts a_size
elsif cmp == -1
  puts b_size
elsif a == b
  puts -1
else
  puts a_size
end

exit 0

x = gets.chomp
y = gets.chomp
res = x != y ? (x.size < y.size ? y.size : x.size) : -1
puts res

a = gets.chop
b = gets.chop
if a == b
  puts -1
else
  puts [a.size, b.size].max
end

a = gets.strip
b = gets.strip
n = [a.length, b.length].max
ans = count = 0
n.times do |i|
    if a[i] != b[i]
        count += 1
    else
        count = 0
    end
    ans = [ans, count].max
end
ans = -1 if ans == 0
puts ans

a=gets.chomp
b=gets.chomp

p1=0
p2=0

while p1<b.size && p2<a.size
	if a[p2]==b[p1] then
		p1+=1
	end
	p2+=1
end
if p1==b.size then
	puts -1
else
	puts b.size
end

x = gets.chomp
y = gets.chomp
res = x != y ? (x.size < y.size ? y : x) : -1
puts res


gets.to_i.times do
  a, b, c, x, y = gets.split.map(&:to_i)
  x = [x - a, 0].max
  y = [y - b, 0].max
  puts x + y <= c ? "YES" : "NO"
end

t=gets.chomp.to_i
t.times do
    s= gets.chomp.split
    a=s[0].to_i
    b=s[1].to_i
    c=s[2].to_i
    x=s[3].to_i
    y=s[4].to_i
    ax = (a-x)
    by = (b-y)
    if(a>=x)
        summaA = 0
    else
        summaA=a-x
    end
    if(b>=y)
        summaB=0
    else
        summaB=b-y
    end
    summa = summaA+summaB+c
    if(summa>=0)
        puts "YES"
    else
        puts "NO"
    end
end


#!/usr/bin/ruby
n,*a=$<.read.split.map(&:to_i)
b=a.sort.each_cons(2).map{|x,y|y-x}
m=b.min
puts '%d %d'%[m,b.count(m)]

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

dis = (0..n-2).map { |i| data[i+1] - data[i] }
ans = dis.min
print "#{ans} #{dis.select { |v| v == ans }.length}\n"

n=gets.to_i
aa=gets.split.map(&:to_i).sort
aa=aa[1, n-1].zip(aa).map { |a, b| a-b }
m=aa.min
print m, ' ', aa.count(m)

gets
pos = gets.split.map{|x| x.to_i}.sort

for idx in (0...pos.length-1) do
  pos[idx] = pos[idx+1]-pos[idx]
end

pos.pop

m = pos.min
puts "#{m} #{pos.count(m)}"

n = gets.chomp.to_i
cities = gets.chomp.split().map { |e| e.to_i }
distances = []
cities.sort!
for i in 0 ... (n - 1)
  distances << (cities[i] - cities[i + 1]).abs
end
minDis = distances.min
repeat = distances.count(minDis)
print "#{minDis} #{repeat}\n"

n=gets.to_i
a=gets.split.map{ |x| x.to_i }
a.sort!
min_distance=2*10**9+1
min_count=0
for i in 1...a.length
	if a[i]-a[i-1] < min_distance
		min_distance=a[i]-a[i-1]
		min_count=1
	elsif a[i]-a[i-1] == min_distance
		min_count+=1
	end
end
puts "#{min_distance} #{min_count}"

n=3
aa=gets.split.map(&:to_i).sort
aa=aa[1, n-1].zip(aa).map { |a, b| a-b }
m=aa.min
print m, ' ', aa.count(m)


gets
b=gets.split
z=gets.split.map &:to_i
s=0
r=z.map{0}
b.map{|c|s+=c.to_i
while(t=z.select{|i|i<=s})[0]
r[z.index m=t.max]+=s/m
s%=m
end}
puts r*" ",s

def koukan()
    return -1 if $sum < $aa[0]
    5.downto(1) { |i|
        return i-1 if $aa[i-1] <= $sum
    }
end

$n = gets.chomp.to_i
$sa = gets.chomp.split(/ /).map!{|x| x.to_i}
$aa = gets.chomp.split(/ /).map!{|x| x.to_i}
$sum = 0
$suma = Array.new(5).map!{|x| x = 0}
for i in 0..$n-1
    $sum += $sa[i]
    while (true)
        if $sum > $aa[4]
            wka = $sum.divmod($aa[4])
            $suma[4] += wka[0]
            $sum -= $aa[4] * wka[0]
        end
        ret = koukan()
        break if ret < 0
        $suma[ret] += 1
        $sum -= $aa[ret]
    end
end
puts $suma.join(" ")
puts $sum

gets
bats = gets.split
prizes = gets.split.map &:to_i
score = 0
res = prizes.map{0}
for bat in bats
    score += bat.to_i
    loop do
        can = prizes.select{ |e| e <= score }
        break if can.empty?
        x = can.max
        res[prizes.index x] += score / x
        score %= x
    end
end

puts res.join " "
puts score

gets
bats = gets.split
prizes = gets.split.map.with_index{|e,i|[e.to_i,i]}
score = 0
res = prizes.map{0}
for bat in bats
    score += bat.to_i
    loop do
        can = prizes.select{ |e,i| e <= score }
        break if can.empty?
        x = can.max_by(&:first)
        res[x.last] += score / x.first
        score %= x.first
    end
end

puts res.join " "
puts score

n = gets.to_i
p = gets.split.map(&:to_i)
st = gets.split.map(&:to_i)
res = Array.new(5,0)

r = p.inject(0) { |acc, x|  t = acc + x; (0..4).reverse_each { |j| res[j] += t / st[j]; t %= st[j] }; t }
puts res*' '
puts r

n = gets.to_i
p = gets.split.map{|e| e.to_i }
cost = gets.split.map{|e| e.to_i }
count = Array.new(5){0}
remain = 0
cost.reverse!
p.each do |i|
    remain += i
    5.times do |j|
        if remain >= cost[j]
            tmp = (remain / cost[j]).truncate
            remain -= cost[j] * tmp
            count[4-j] += tmp
        end
    end
end
puts "#{count[0]} #{count[1]} #{count[2]} #{count[3]} #{count[4]}"
puts remain

n = gets.to_i
a = gets.split.map { |s| s.to_i }
b = gets.split.map { |s| s.to_i }
s, r, i = 0, b.map {0}
n.times { |i|
   s = s + a[i] if i < n
   loop {
      c = b.select { |p| s >= p }
      break if c.empty?
      m = c.max
      r[b.index(m)] += (s / m)
      s = s - ((s / m) * m)
   }
   i = i + 1
}
puts r.join(" ")
puts s

n = gets.to_i
a = gets.split.map { |s| s.to_i }
b = gets.split.map { |s| s.to_i }
s, r, i = 0, b.map {0}
n.times { |i|
   s = s + a[i] if i < n
   loop {
      m, o = 0, -1
      5.times { |j|
         if s >= b[j]
            m = b[j]
            o = j
         end
      }
      break if o == -1
      r[o] = r[o] + (s / m)
      s = s - ((s / m) * m)
   }
   i = i + 1
}
puts r.join(" ")
puts s

#!/usr/bin/ruby
n = gets.chomp.to_i
p = gets.chomp.split(' ').map{|x| x.to_i}
a,b,c,d,e = gets.chomp.split(' ').map{|x| x.to_i}
na = 0
nb = 0
nc = 0
nd = 0
ne = 0
curr = 0
for i in 0..p.size-1
	curr += p[i]
	while curr >= a
		if curr >= e
			ne += curr/e
			curr=curr%e
		elsif curr >= d
			nd +=curr/d
			curr =curr%d
		elsif curr >= c
			nc += curr/c
			curr = curr%c
		elsif curr >= b
			nb += curr/b
			curr =curr%b
		elsif curr >= a
			na +=curr/a
			curr =curr%a
		end
	end
end
print "#{na} #{nb} #{nc} #{nd} #{ne}\n"
puts curr

require "scanf"
n = gets.to_i
list = []
gets.split(" ").each {
    |i| list.push(i.to_i)
}
cost = scanf("%d%d%d%d%d")
cnt = [0,0,0,0,0]
tot = 0
list.each { |i|
    tot += i
    5.times { |j|
        cnt[4 - j] += tot / cost[4 - j]
        tot %= cost[4 - j]
    }
}
cnt.each{ |i| print i, " "}
print "\n", tot

n = gets.to_i
p = gets.split.collect{|i| i.to_i}
c = gets.split.collect{|i| i.to_i}
ans = Array.new(5,0)
points = 0
p.each do |pp|
    points += pp
    4.downto(0).each do |i|
        ans[i] += points/c[i]
        points = points%c[i]
    end
end
puts ans.join(" ")
puts points

n,=gets.split.map{|e| e.to_i}
a=gets.split.map{|e| e.to_i}
b=gets.split.map{|e| e.to_i}
c=Array.new(5,0)
i=0
k=0
while i<n do
  k+=a[i]
  4.downto(0) {|j|
    c[j]+=k/b[j]
    k=k%b[j]
  }
  i+=1
end
for i in 0..4 do
  print c[i],' '
end
puts "\n", k

def read_next_line
  gets.split.map(&:to_i)
end

gets
p = read_next_line
a = read_next_line

t = Array.new(5, 0)
s = 0
p.each { |x|
  s += x
  4.downto(0).each { |i|
    t[i] += s / a[i]
    s %= a[i]
  }
}

puts t * ' '
puts s

#!/usr/bin/ruby

while line = gets
	n = line.to_i
	ps = gets.split.map(&:to_i)
	as = gets.split.map(&:to_i)
	ans = Array.new(as.length, 0)
	now = 0
	ps.each do |p|
		now += p
		(as.length - 1).downto(0) do |i|
			tmp = now / as[i]
			ans[i] += tmp
			now -= tmp * as[i]
		end
	end
	as.length.times do |i|
		if i > 0
			print " "
		end
		print ans[i]
	end
	puts
	puts now
end

gets
bats = gets.split.map &:to_i
prizes = gets.split.map &:to_i
score = 0
res = prizes.map{0}
for bat in bats
    score += bat

    can = prizes.select{ |prize| prize <= score }
    break if can.empty?
    res[prizes.index(can.max)] += score / can.max
    score %= can.max

end
puts res.join " "
puts score

#!/usr/bin/ruby
n = gets.chomp.to_i
p = gets.chomp.split(' ').map{|x| x.to_i}
a,b,c,d,e = gets.chomp.split(' ').map{|x| x.to_i}
na = 0
nb = 0
nc = 0
nd = 0
ne = 0
curr = 0
for i in 0..p.size-1
	curr += p[i]
	while curr >= a
		if curr >= e
			curr-=e
			ne += 1
		elsif curr >= d
			curr -= d
			nd +=1
		elsif curr >= c
			curr -= c
			nc += 1
		elsif curr >= b
			curr -= b
			nb += 1
		elsif curr >= a
			curr -=a
			na +=1
		end
	end
end
print "#{na} #{nb} #{nc} #{nd} #{ne}\n"

require "scanf"
n = gets.to_i
list = []
gets.split(" ").each {
    |i| list.push(i.to_i)
}
cost = scanf("%d%d%d%d%d")
cnt = [0,0,0,0,0]
tot = 0
list.each { |i|
    tot += i
    5.times { |j|
        p j
        while tot >= cost[4 - j]
            cnt[4 - j] += 1
            tot -= cost[4 - j]
        end
    }
}
cnt.each{ |i|
    print i , " "}
print "\n",tot

n,=gets.split.map{|e| e.to_i}
a=gets.split.map{|e| e.to_i}
b=gets.split.map{|e| e.to_i}
c=Array.new(5,0)
i=0
k=0
while i<n do
  k+=a[i]
  puts k
  for j in 0..4 do
    if b[j]>k then break end
  end
  j-=1
  if j>=0 then c[j]+=1; k-=b[j] end
  i+=1
end
for i in 0..4 do
  print c[i],' '
end
puts "\n", k

def read_next_line
  gets.split.map(&:to_i)
end

gets
p = read_next_line
a = read_next_line

t = Array.new(5, 0)
s = 0
p.each { |x|
  s += x
  t[4] += s / a[4]
  s %= a[4]
  3.downto(0).each { |i|
    if a[i] <= s
      s -= a[i]
      t[i] += 1
    end
  }
}

puts t * ' '
puts s


# Input
n, r = gets.split(" ").map { |e| e.to_f }

# Processing
alpha = 2 * Math::PI / n
v = Math.sin(alpha / 2)
result = r * v / (1 - v)

# Output
puts result

n, r = gets.split.map(&:to_f)
l = 0.0
rt = 10e5
eps = 10e-8
while (rt - l > eps)
  m = (rt - l) / 2 + l # радиус r
  rr = m + r
  d = Math.acos((rr ** 2 + rr ** 2 - (m * 2) ** 2)/(2 * rr * rr))
  if (d / Math::PI * 180 > (360.0 / n))
    rt = m
  else
    l = m
  end
end
puts rt

n,r = gets.split.map(&:to_f)
s = Math.sin(Math::PI/n)
puts r*s/(1-s)


N = gets.to_i

#01234567
#aaffaaff
#bbggbbgg
#cddhhcchh
#ceeiiddii

str = []
str[0] = "aaff" * 25
str[1] = "bbgg" * 25
str[2] = "cdd" + "hhcc" * 25
str[3] = "cee" + "iidd" * 25

if N == 1
  print "a\na\nb\nb\n"
elsif N == 2
  puts "aa"
  puts "bb"
  puts "cd"
  puts "cd"
else
#  p str
  if (N % 2 == 1)
    str[1][N - 1] = str[0][N - 1]
  else
    str[3][N - 1] = str[2][N - 1]
  end
  4.times do |i|
    puts str[i][0..(N - 1)]
  end
end

n = gets.to_i

def dig2str(dig)
    p = 'a'
    (dig%26).times { p = p.succ }
    p
end

def solve(n)
    return ['a', 'a', 'b', 'b'] if n == 1
    return ['aa', 'bb', 'cc', 'dd'] if n == 2
    a = Array.new(4) { Array.new(n) }
    dig = 0
    0.step(n-2, 2) do |i|
        a[0][i] = a[0][i+1] = dig2str(dig)
        dig += 1
        a[1][i] = a[1][i+1] = dig2str(dig)
        dig += 1
    end
    dig = 10
    1.step(n-2, 2) do |i|
        a[2][i] = a[2][i+1] = dig2str(dig)
        dig += 1
        a[3][i] = a[3][i+1] = dig2str(dig)
        dig += 1
    end
    if n%2==0
        a[2][-1] = a[3][-1] = dig2str(dig)
    else
        a[0][-1] = a[1][-1] = dig2str(dig)
    end
    a[2][0] = a[3][0] = dig2str(9)
    return a.map(&:join)
end

puts solve(n)

def build_line(s1, s2, len)
  (1..len).map { |i| i.odd? ? s1 : s2 }.join ''
end

def build_odd(n)
  line1 = build_line('aa', 'bb', (n - 1) / 2)
  line2 = build_line('cc', 'dd', (n - 1) / 2)
  puts line1 + 'x'
  puts line2 + 'x'
  puts 'y' + line1
  puts 'y' + line2
end

def build_even(n)
  line1 = build_line('aa', 'bb', n / 2)
  puts line1
  puts 'x' + build_line('cc', 'dd', n / 2 - 1) + 'y'
  puts 'x' + build_line('ee', 'ff', n / 2 - 1) + 'y'
  puts line1
end

n = gets.to_i
if n.odd?
  build_odd(n)
else
  build_even(n)
end

N = gets.to_i

#01234567
#aaffaaff
#bbggbbgg
#cddhhcchh
#ceeiiddii

str = []
str[0] = "aaff" * 25
str[1] = "bbgg" * 25
str[2] = "cdd" + "hhcc" * 25
str[3] = "cee" + "iidd" * 25

if N == 1
  print "a\na\nb\nb\n"
elsif N == 2
  puts "aa"
  puts "bb"
  puts "cd"
  puts "cd"
else
  p str
  if (N % 2 == 1)
    str[1][N - 1] = str[0][N - 1]
  else
    str[3][N - 1] = str[2][N - 1]
  end
  4.times do |i|
    puts str[i][0..(N - 1)]
  end
end


s=(gets.to_i+1)/2
a=gets(p).scan /(\S+) (.+)/
x=a.flatten.sort.each_slice([a.size/2,1].max).map(&:first).select{|i|a.count{|j,k|j==i||k==i}>=s}
p x[0]?[s-x.map{|i|a.map(&:first).count i}.max,0].max: -1

s2 = (gets.to_i+1)/2
a = gets(p).scan(/(\S+) (\S+)/)
af,al = a.transpose

as = a.flatten.sort.each_slice([a.size/2,1].max).map(&:first)
x = as.select{ |i|
    a.count{ |j,k| j == i || k == i } >= s2
}

p x.empty? ? -1 : [(
    a1 = af.group_by{|i|i}
    #s2 - x.map{ |i| a1[i] ? a1[i].size : 0 }.max
    s2 - x.map{ |i| af.count i }.max
), 0].max

n = gets.to_i
o = (n - 1) / 2 + 1
@m = 1 << 30
h, k = {}, {}
def f(a, b, p, q)
   a.each_pair { |y, v|
      if b.has_key?(y) and v + b[y] >= p
         @m = q == 0 ? [@m, p - v].min : [@m, p - b[y]].min
      elsif v >= p
         @m = [@m, q].min
      end
   }
end
n.times { |x|
   a, b = gets.split.map { |i| i.to_i }
   h[a] = h.has_key?(a) ? h[a] + 1 : 1
   k[b] = k.has_key?(b) ? k[b] + 1 : 1 if a != b
}
f(h, k, o, 0)
f(k, h, o, o)
puts @m == 1 << 30 ? -1 : [0, @m].max

n = gets.to_i
o = (n - 1) / 2 + 1
b = []
h, k = {}, {}
n.times { |x|
   b << gets.split.map { |i| i.to_i }
   h[b[x][0]] = h.has_key?(b[x][0]) ? h[b[x][0]] + 1 : 1
   k[b[x][1]] = k.has_key?(b[x][1]) ? k[b[x][1]] + 1 : 1 if b[x][1] != b[x][0]
}
m = 1 << 30
h.each_pair { |y, v|
   if k.has_key?(y) and v + k[y] >= o
      m = [m, o - v].min
   elsif v >= o
      puts 0
      exit
   end
}
k.each_pair { |y, v|
   if h.has_key?(y) and v + h[y] >= o
      m = [m, o - h[y]].min
   elsif v >=o
      puts o
      exit
   end
}
puts m == 1 << 30 ? -1 : [0, m].max

s2 = (gets.to_i+1)/2
a = gets(p).scan(/(\S+) (\S+)/)
af,al = a.transpose

as = a.flatten.sort.each_slice([a.size/2,1].max).map(&:first)
x = as.select{ |i|
    a.count{ |j,k| j == i || k == i } >= s2
}

p x.empty? ? -1 : [(
    a1 = af.group_by{|i|i}
    s2 - x.map{ |i| a1[i] ? a1[i].size : 0 }.max
    #s2 - x.map{ |i| af.count i }.max
), 0].max

s2 = (gets.to_i+1)/2
a = gets(p).scan(/(\S+) (\S+)/)
af,al = a.transpose

as = a.flatten.sort
x = [as[0],as[as.size/2]].select{ |i|
    a.count{ |j,k| j == i || k == i } >= s2
}

p x.empty? ? -1 : [(
    a1 = af.group_by{|i|i}
    s2 - x.map{ |i| a1[i] ? a1[i].size : 0 }.max
    #s2 - x.map{ |i| af.count i }.max
), 0].max

n = gets.to_i
o = (n - 1) / 2 + 1
b = []
h, k = {}, {}
n.times { |x|
   b << gets.split.map { |i| i.to_i }
   h[b[x][0]] = h.has_key?(b[x][0]) ? h[b[x][0]] + 1 : 1
   k[b[x][1]] = k.has_key?(b[x][1]) ? k[b[x][1]] + 1 : 1
}
m = 1 << 30
h.each_pair { |y, v|
   if k.has_key?(y) and v + k[y] >= o
      m = [m, o - v].min
   elsif v >= o
      puts 0
      exit
   end
}
k.each_pair { |y, v|
   if h.has_key?(y) and v + h[y] >= o
      m = [m, o - h[y]].min
   elsif v >=o
      puts o
      exit
   end
}
puts [0, m].max

n = gets.to_i
o = (n - 1) / 2 + 1
b = []
h, k = {}, {}
n.times { |x|
   b << gets.split.map { |i| i.to_i }
   h[b[x][0]] = h.has_key?(b[x][0]) ? h[b[x][0]] + 1 : 1
   k[b[x][1]] = k.has_key?(b[x][1]) ? k[b[x][1]] + 1 : 1
}
m = 1 << 30
h.each_pair { |y, v|
   if k.has_key?(y) and v + k[y] >= o
      m = [m, o - v].min
   elsif v >= 0
      puts 0
      exit
   end
}
k.each_pair { |y, v|
   if h.has_key?(y) and v + h[y] >= o
      m = [m, o - h[y]].min
   elsif v >=o
      puts 0
      exit
   end
}
puts [0, m].max

n = gets.to_i
o = (n - 1) / 2 + 1
@m = 1 << 30
h, k = {}, {}
def f(a, b, p, q)
   a.each_pair { |y, v|
      if b.has_key?(y) and v + b[y] >= p
         @m = [@m, p - v].min
      elsif v >= p
         @m = [@m, q].min
      end
   }
end
n.times { |x|
   a, b = gets.split.map { |i| i.to_i }
   h[a] = h.has_key?(a) ? h[a] + 1 : 1
   k[b] = k.has_key?(b) ? k[b] + 1 : 1 if a != b
}
f(h, k, o, 0)
f(h, k, o, o)
puts @m == 1 << 30 ? -1 : [0, @m].max

n = gets.to_i
o = (n - 1) / 2 + 1
@m = 1 << 30
h, k = {}, {}
def f(a, b, p, q)
   a.each_pair { |y, v|
      if b.has_key?(y) and v + b[y] >= p
         @m = [@m, p - v].min
      elsif v >= p
         @m = [@m, q].min
      end
   }
end
n.times { |x|
   a, b = gets.split.map { |i| i.to_i }
   h[a] = h.has_key?(a) ? h[a] + 1 : 1
   k[b] = k.has_key?(b) ? k[b] + 1 : 1 if a != b
}
f(h, k, o, 0)
f(k, h, o, o)
puts @m == 1 << 30 ? -1 : [0, @m].max

n = gets.to_i
o = (n - 1) / 2 + 1
b = []
h, k = {}, {}
n.times { |x|
   b << gets.split.map { |i| i.to_i }
   h[b[x][0]] = h.has_key?(b[x][0]) ? h[b[x][0]] + 1 : 1
   k[b[x][1]] = k.has_key?(b[x][1]) ? k[b[x][1]] + 1 : 1
}
m = 1 << 30
h.each_pair { |y, v|
   if k.has_key?(y) and v + k[y] >= o
      m = [m, o - v].min
   elsif v >= o
      puts 0
      exit
   end
}
k.each_pair { |y, v|
   if h.has_key?(y) and v + h[y] >= o
      m = [m, o - h[y]].min
   elsif v >=o
      puts o
      exit
   end
}
puts m == 1 << 30 ? -1 : [0, m].max


I = open('input.txt')
O = open('output.txt','w')
n, m, k = I.gets.split.map &:to_i
t = [nil] * (n *= m)
k.times do
	a, b, c, d = I.gets.split
	if /-1/ then
		c = t.index b
		if c then
			t[c] = nil
			O.puts "#{c / m + 1} #{c % m + 1}"
		else
			O.puts '-1 -1'
		end
	else
		b = (b.to_i - 1) * m + (c.to_i - 1)
		b += 1 while b < n && t[b]
		t[b] = d if b < n
	end
end

f = open("input.txt", "r")
fw = open("output.txt", "w")
n,m,k = f.gets.chomp.split(/ /).map!{|x|x.to_i}
sh = Array.new(n)
sh.map!{|x| Array.new(m) }
h = Hash.new
for i in 0..k-1
	ar = f.gets.chomp.split(/ /)
	if ar[0] == "+1"
		x = ar[1].to_i-1
		y = ar[2].to_i-1
		while true
			if sh[x][y] == nil
				h[ar[3]] = (x+1).to_s +  " " + (y+1).to_s
				sh[x][y] = ar[3]
				break
			end
			y += 1
			if y == m
				x += 1
				y = 0
			end
			if x == n
				break
			end
		end
	else
		if h.has_key?( ar[1] )
			fw.puts h[ar[1]]
			x,y = h[ar[1]].split(/ /).map!{|x|x.to_i-1}
			sh[x][y] = nil
			h.delete(ar[1])
		else
			fw.puts "-1 -1"
		end
	end
end
f.close
fw.close

I = open('input.txt')
O = open('output.txt','w')
n, m, k = I.gets.split.map &:to_i
t = Array.new(n *= m)
k.times do
	a, b, c, d = I.gets.split
	if /-1/ then
		c = t.index b
		if c then
			t[c] = nil
			O.puts "#{c / m + 1} #{c % m + 1}"
		else
			O.puts '-1 -1'
		end
	else
		b = (b.to_i - 1) * m + (c.to_i - 1)
		c = 0
		b, c = b + 1, c + 1 while t[b % n] && c < n
		t[b % n] = d unless t[b % n]
	end
end


gets
a, b = gets.split.map(&:to_i), [2000]
while a.any? && p = a.index(b.size) do
	b << b[-1] + p + 1
	a = a[p + 1 .. -1]
end
b[0] = b.size - 1
puts b * ' '

n = gets.to_i

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

ans ||= []

pre = 0
a.each_index do |x|
  if a[x] == pre + 1
    ans << x + 2001
    pre = a[x]
  end
end

puts ans.size
puts ans * " " if ans.any?

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

years = Array.new
seq = 1
for i in 0..n-1
	if a[i] == seq
		years.push((2001+i).to_s)
		seq += 1
	end
end

if years.length == 0
	puts "0"
else
	puts years.length.to_s
	puts years.join(' ')
end


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

# c.rb

def main()
    n, m = gets.chomp.split(" ").map{|s| next s.to_i }
    d = [0] * m
    h = [0] * m

    m.times do |i|
        d[i], h[i] = gets.chomp.split(" ").map{|s| next s.to_i }
    end

    r = h[0]
    1.upto(m-1) do |i|
        dd = d[i] - d[i-1]
        dh = h[i] - h[i-1]
        if dd.abs < dh.abs then
            puts("IMPOSSIBLE")
            return
        else
            r = [r, h[i]].max
            r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max
        end
    end
    r = [r, h[-1] + (n - d[-1])].max
    r = [r, h[0] + (d[0] - 1)].max
    puts r
end

if __FILE__ == $0 then
    main()
end

n, m = gets.split.map {|i| i.to_i}
b1, a1 = gets.split.map {|i| i.to_i}
maxx = a1 + b1 - 1
for i in 0...m - 1
  b, a = gets.split.map {|i| i.to_i}
  maxx = [maxx, (b - b1 + a1 + a) / 2].max
  if (a1 - a).abs > (b1 - b).abs
    print 'IMPOSSIBLE'
    exit(0)
  end
  a1, b1 = a, b
end
print [maxx, a1 + n - b1].max
# a - a1 + a - a2 <= b2 - b1
# a + a <= b2 - b1 + a1 + a2
# a <= (b2 - b1)

n, m = gets.strip.split(" ").map(&:to_i)
record = Array.new
m.times do |i|
  d, h = gets.strip.split(" ").map(&:to_i)
  record.push [d, h]
end
possible = true
maxh = 0
(m-1).times do |i|
  dd = record[i+1][0] - record[i][0]
  dh = record[i+1][1] - record[i][1]
  if dh.abs > dd
    possible = false
  end
  hh = (dd - dh.abs) / 2
  maxh = [maxh, record[i+1][1] + hh, record[i][1] + hh].max
end

c1 = record[0][0] + record[0][1] - 1
c2 = (n - record[-1][0]) + record[-1][1]
maxh = [c1, c2, maxh].max

if possible
  puts maxh
else
  puts "IMPOSSIBLE"
end

n, m = gets.split.collect{|i| i.to_i}
pre = gets.split.collect{|i| i.to_i}
ans = pre[1]+pre[0]-1
(1...m).each do
    tmp = gets.split.collect{|i| i.to_i}
    t = tmp[0]-pre[0]
    v1, v2 = [pre[1], tmp[1]].minmax
    x = (1.0*t+v2-v1)/2
    if x < 0 or x > t
        puts "IMPOSSIBLE"
        exit
    end
    x = x.round
    ans = [ans,v1+x-1,v2+t-x].max
    pre = tmp
end
ans = [ans,pre[1]+n-pre[0]].max
puts ans

n, m = gets.strip.split(" ").map(&:to_i)
record = Array.new
m.times do |i|
  d, h = gets.strip.split(" ").map(&:to_i)
  record.push [d, h]
end
possible = true
maxh = 0
(m-1).times do |i|
  dd = record[i+1][0] - record[i][0]
  dh = record[i+1][1] - record[i][1]
  if dh.abs > dd
    possible = false
  end
  hh = (dd - dh.abs) / 2
  maxh = [maxh, record[i+1][1] + hh, record[i][1] + hh].max
end

c1 = record[0][0] + record[0][1]
c2 = (n - record[-1][0]) + record[-1][1]
maxh = [c1, c2, maxh].max

if possible
  puts maxh
else
  puts "IMPOSSIBLE"
end

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

# c.rb

def main()
    n, m = gets.chomp.split(" ").map{|s| next s.to_i }
    d = [0] * m
    h = [0] * m

    m.times do |i|
        d[i], h[i] = gets.chomp.split(" ").map{|s| next s.to_i }
    end

    r = h[0]
    1.upto(m-1) do |i|
        dd = d[i] - d[i-1]
        dh = h[i] - h[i-1]
        if dd.abs < dh.abs then
            puts("IMPOSSIBLE")
            return
        else
            r = [r, h[i]].max
            r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max
        end
    end
    puts r
end

if __FILE__ == $0 then
    main()
end

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

# c.rb

def main()
    n, m = gets.chomp.split(" ").map{|s| next s.to_i }
    d = [0] * m
    h = [0] * m

    m.times do |i|
        d[i], h[i] = gets.chomp.split(" ").map{|s| next s.to_i }
    end

    r = h[0] + d[0]
    1.upto(m-1) do |i|
        dd = d[i] - d[i-1]
        dh = h[i] - h[i-1]
        if dd.abs < dh.abs then
            puts("IMPOSSIBLE")
            return
        else
            r = [r, h[i]].max
            r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max
        end
    end
    puts r
end

if __FILE__ == $0 then
    main()
end

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

# c.rb

def main()
    n, m = gets.chomp.split(" ").map{|s| next s.to_i }
    d = [0] * (m + 1)
    h = [0] * (m + 1)

    m.times do |i|
        d[i+1], h[i+1] = gets.chomp.split(" ").map{|s| next s.to_i }
    end

    r = 0
    1.upto(m) do |i|
        dd = d[i] - d[i-1]
        dh = h[i] - h[i-1]
        if dd.abs < dh.abs then
            puts("IMPOSSIBLE")
            return
        else
            r = [r, h[i]].max
            r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max
        end
    end
    puts r
end

if __FILE__ == $0 then
    main()
end

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

# c.rb

def main()
    n, m = gets.chomp.split(" ").map{|s| next s.to_i }
    d = [0] * m
    h = [0] * m

    m.times do |i|
        d[i], h[i] = gets.chomp.split(" ").map{|s| next s.to_i }
    end

    r = 0
    1.upto(m-1) do |i|
        dd = d[i] - d[i-1]
        dh = h[i] - h[i-1]
        if dd.abs < dh.abs then
            puts("IMPOSSIBLE")
            return
        else
            r = [r, h[i]].max
            r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max
        end
    end
    puts r
end

if __FILE__ == $0 then
    main()
end

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

# c.rb

def main()
    n, m = gets.chomp.split(" ").map{|s| next s.to_i }
    d = [0] * m
    h = [0] * m

    m.times do |i|
        d[i], h[i] = gets.chomp.split(" ").map{|s| next s.to_i }
    end

    r = h[0]
    1.upto(m-1) do |i|
        dd = d[i] - d[i-1]
        dh = h[i] - h[i-1]
        if dd.abs < dh.abs then
            puts("IMPOSSIBLE")
            return
        else
            r = [r, h[i]].max
            r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max
        end
    end
    r = [r, h[-1] + (n - d[-1])].max
    puts r
end

if __FILE__ == $0 then
    main()
end

n, m = gets.split.collect{|i| i.to_i}
pre = gets.split.collect{|i| i.to_i}
ans = pre[1]+pre[0]-1
(1...m).each do
    tmp = gets.split.collect{|i| i.to_i}
    t = tmp[0]-pre[0]
    v1, v2 = [pre[1], tmp[1]].minmax
    x = (1.0*t+v2-v1)/2
    if x < 0 or x > t
        puts "IMPOSSIBLE"
        exit
    end
    if x%1 >= 0.5
        x = x.ceil
    else
        x = x.floor
    end
    ans = [ans,v1+x-1,tmp[1]].max
    pre = tmp
end
ans = pre[1]+n-pre[0] if ans < pre[1]+n-pre[0]
puts ans

n, m = gets.split.collect{|i| i.to_i}
pre = gets.split.collect{|i| i.to_i}
ans = pre[1]+pre[0]-1
(1...m).each do
    tmp = gets.split.collect{|i| i.to_i}
    t = tmp[0]-pre[0]
    v1, v2 = [pre[1], tmp[1]].minmax
    x = (1.0*t+v2-v1)/2
    if x < 0 or x > t
        puts "IMPOSSIBLE"
        exit
    end
    if x%1 >= 0.5
        x = x.ceil
    else
        x = x.floor
    end
    ans = [v1+x-1,tmp[1]].max
    pre = tmp
end
ans = pre[1]+n-pre[0] if ans < pre[1]+n-pre[0]
puts ans

n, m = gets.split.collect{|i| i.to_i}
pre = gets.split.collect{|i| i.to_i}
ans = pre[1]+pre[0]-1
(1...m).each do
    tmp = gets.split.collect{|i| i.to_i}
    t = tmp[0]-pre[0]-1
    v1, v2 = [pre[1], tmp[1]].minmax
    x = (1.0*t+v2-v1)/2
    if x < 0 or x > t
        puts "IMPOSSIBLE"
        exit
    end
    if x%1 >= 0.5
        x = x.ceil
    else
        x = x.floor
    end
    ans = v1+x if ans < v1+x
    pre = tmp
end
ans = pre[1]+n-pre[0] if ans < pre[1]+n-pre[0]
puts ans

n, m = gets.split.collect{|i| i.to_i}
pre = gets.split.collect{|i| i.to_i}
ans = pre[1]+pre[0]-1
(1...m).each do
    tmp = gets.split.collect{|i| i.to_i}
    t = tmp[0]-pre[0]
    v1, v2 = [pre[1], tmp[1]].minmax
    x = (1.0*t+v2-v1)/2
    if x < 0 or x > t
        puts "IMPOSSIBLE"
        exit
    end
    if x%1 >= 0.5
        x = x.ceil
    else
        x = x.floor
    end
    ans = v1+x-1 if ans < v1+x-1
    pre = tmp
end
ans = pre[1]+n-pre[0] if ans < pre[1]+n-pre[0]
puts ans


t = gets.to_i
for j in 1..t do
	n, k = gets.split.map(&:to_i)
	tmp = 1; res = 0
	for i in 0..31 do
		if (k&(1<<i)) != 0
			res = (res + tmp) % 1000000007
		end
		tmp = (tmp * n) % 1000000007
	end
	puts res
end

t = gets
t = t.to_i
for j in 1..t do
	n, k = gets.split.map(&:to_i)
	tmp = 1; res = 0
	for i in 0..31 do
		if (k&(1<<i)) != 0
			res = (res + tmp) % 1000000007
		end
		tmp = (tmp * n) % 1000000007
	end
	puts res
end

t = gets
t = t.to_i
for j in 1..t do
	n = gets; k = gets
	n = n.to_i; k = k.to_i
	tmp = 1; res = 0
	for i in 0..31 do
		if (k&(1<<i)) != 0
			res = (res + tmp) % 1000000007
		end
		tmp = (tmp * n) % 1000000007
	end
	puts res
end


gets.to_i.times do
  n = gets.to_i
  if (n % 2 == 1) or (n<3)
    puts -1
  else
    mn = n / 6
    if n % 6 != 0 then
      mn += 1
    end
    mx = n / 4
    puts "#{mn} #{mx}"
  end
end

t=gets
n = t.to_i
n.times do
    t2=gets
    x=t2.to_i
    if x%4==1 || x%4==3 || x==2
        puts -1
    elsif x % 6==0
        print x/6, ' ', x/4, "\n"
    else
        print x/6+1, ' ', x/4, "\n"
    end
end


n = gets.to_i
puts gets.chomp.split.map{|s| s.split('').sort.uniq.join}.sort.uniq.size

class Manuscrito
	def initialize(count, string)
		@count = count
		@string = string
	end

	def decipher
		string = @string.dup
		words = string.split
		words.map! do |word|
			word.split('').sort.join.squeeze
		end
		puts objects = words.uniq.size
	end

end

n = gets
string = gets

manuscrito = Manuscrito.new(n, string)
manuscrito.decipher

gets;
ar = gets.split
res = 0;
ar.map!{|s| s.chars.uniq.sort.join()}
p ar.uniq.size

n = gets
str = gets
str = str.split(" ")
mas = Array.new(n.to_i)
str.size.times do |i|
	mas[i] = str[i].split("").uniq.sort.join
end
puts mas.uniq.size

gets
puts gets.split.map{|s| s.chars.uniq.sort.join}.uniq.size

class Manuscrito
	def initialize(count, string)
		@count = count
		@string = string
	end

	def decipher
		string = @string.dup
		words = string.split
		roots = find_roots(words)
		words = delete_same(words, roots)
		objects = roots.size + words.size
		puts objects
	end

	private

	def find_roots(words)
		roots = Array.new
		words.each do |word|
			sorted_word = word.split('').sort.join.squeeze
			if sorted_word.size == word.size
				if !(roots.include? sorted_word)
					roots << sorted_word
				end
			end
		end
		roots
	end

	def delete_same(words, roots)
		same = Array.new
		words.each do |word|
			roots.each do |root|
				if word.split('').sort.join.squeeze == root
					same << word
				end
			end
		end
		words = words - same
		words
	end
end

n = gets
string = gets

manuscrito = Manuscrito.new(n, string)
manuscrito.decipher


result = 1000000.0
my_x, my_y = gets.split.map(&:to_i)
n = gets.chomp.to_i

1.upto(n) do
	x, y, v = gets.split.map(&:to_i)

	d = Math.sqrt((my_x-x)**2 + (my_y-y)**2)/v

	result = [d, result].min
end

puts result.round(8)

a,b=gets.split.map(&:to_f)
p gets.to_i.times.map{
x,y,z=gets.split.map(&:to_f)
	Math.hypot(a-x,b-y)/z
}.min

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

ans = []
n.times do
   x,y,v = gets.split.map(&:to_f)
   ans << Math.sqrt(((x-a)*(x-a)).abs + ((y-b)*(y-b)).abs) / v
end

puts ans.min

x, y = gets.chomp.split.map(&:to_f)
n = gets.chomp.to_i

arr = []
n.times do
  x2, y2, v = gets.chomp.split.map(&:to_f)
  arr << Math.sqrt(((x2 - x)**2).abs + ((y2 - y)**2).abs) / v
end
puts arr.min

class Point

  attr_reader :x, :y
  def initialize(x,y)
    @x=x
    @y=y
  end

  def distance(p1)
    Math.sqrt((@x-p1.x)**2 + (@y-p1.y)**2)
  end
end

class Car

  def initialize(point_car, point_res, speed)
    @point_car = point_car
    @point_res = point_res
    @speed = speed
  end

  def res()
    @point_car.distance(@point_res)/@speed
  end
end

a, b = gets.split.map{|x| x.to_f}
user_point = Point.new(a,b)
res =  Float::INFINITY
eps = 10**-6
gets.to_i.times do |n|
  a,b,v =  gets.split.map{|x| x.to_f}
  car_point = Point.new(a,b)
  car = Car.new(car_point, user_point, v)
  if ((res - car.res) > eps)
    res = car.res
  end
end

puts res

a,b=gets.split.map(&:to_i)
n=gets.to_i
time=10000000
n.times do
  xi,yi,vi=gets.split.map(&:to_i)
  x=(xi-a).abs
  y=(yi-b).abs
  t=Math.sqrt(x**2+y**2)/vi
  if(t<time)
    time=t
  end
end
puts time

x,y=gets.split.map{|e| e.to_f}
n=gets.to_i
ans=0
n.times{|i|
	x1,y1,z1=gets.split.map{|e| e.to_f}
	t=Math.hypot(x-x1,y-y1)/z1
	if i==0 || ans>t then
		ans=t
	end
}
puts ans

ab = gets.chomp.split.map(&:to_i)
n = gets.chomp.to_i
arr = []
d = []
for i in (0..n-1)
	arr << gets.chomp.split.map(&:to_i)
	d << Math.sqrt((arr[i][0] - ab[0])**2 + (arr[i][1] - ab[1])**2)/arr[i][2]
end

print d.min

a,b = gets.split.map &:to_f
n = gets.to_i
puts (1..n).map{|_|
  x,y,v=gets.split.map &:to_f
  Math.sqrt((x-a)**2+(y-b)**2)/v
}.min

def dist x1, x2, y1, y2
	((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
end

charX, charY = gets.chomp.split(' ').map(&:to_i)

taxiNum = gets.chomp.to_i
distArr = []

taxiNum.times do
	taxiX, taxiY, velocity = gets.chomp.split(' ').map(&:to_i)
	value = dist charX, taxiX, charY, taxiY
	distArr << value / velocity
end

puts distArr.min

a,b=gets.split.map(&:to_f)
p gets.to_i.times.map{
x,y,z=gets.split.map(&:to_f)
((a-x).abs+(b-y).abs)/z
}.min

x, y = gets.chomp.split.map(&:to_f)
n = gets.chomp.to_i

arr = []
n.times do
  x2, y2, v = gets.chomp.split.map(&:to_f)
  arr << ((x2 - x).abs + (y2 - y).abs) / v
end
puts arr.min


n, m = gets.split.map(&:to_i)
ans = []
lrs = []
n.times do
  l, r = gets.split.map(&:to_i)
  lrs << (l..r).to_a
end

(1..m).each do |i|
  inclusive = false
  lrs.each do |lr|
    if lr.to_a.include?(i)
      inclusive = true
    end
  end
  ans << i unless inclusive
end
puts ans.size
puts ans.join(" ")

a,b=gets.split.map &:to_i
r=[*1..b]
a.times{x,y=gets.split.map &:to_i;r-=[*x..y]}
p r.size
puts r*' '

N, M = gets.chomp.split(/ /)
range = *(1..M.to_i)

N.to_i.times do |i|
  u, l = gets.chomp.split(/ /)

  (u.to_i..l.to_i).each do |i|
    if range.include? i
      range.delete(i)
    end
  end
end

p range.length

if range.length > 0
  range.each{|i| print "#{i} "}
end

def solution
  n, m = read_ints

  starts = Hash.new { |h, k| h[k] = 0 }
  ends   = Hash.new { |h, k| h[k] = 0 }

  n.times do
    l, r = read_ints
    starts[l] += 1
    ends[r] += 1
  end

  crt = 0

  alone = []

  1.upto(m) do |i|
    crt += starts[i]
    alone << i if crt == 0
    crt -= ends[i]
  end

  puts alone.size
  puts alone.join(' ')

end

def read_int
  gets.to_i
end

def read_ints
  gets.split.map(&:to_i)
end

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

N, M = gets.split.map(&:to_i)
imos = Array.new(M+2, 0)
N.times do
    l, r = gets.split.map(&:to_i)
    imos[l] += 1
    imos[r+1] -= 1
end
(1..M).each do |i|
    imos[i] += imos[i-1]
end
ans = (1..M).select{|i|imos[i] == 0}
puts ans.length
puts ans.join(' ') if ans.length > 0

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

ary = Array.new(m+1, 0)
n.times do
  l, r = gets.strip.split.map(&:to_i)
  l.upto(r).each {|i| ary[i]=1}
end

ans=[]
1.upto(m).each {|i| ans << i if ary[i]==0 }

puts ans.size
puts ans.join(" ")

def solution(m, segments)
  o = {}
  c = {}

  segments.each do |segment|
    o[segment[0]] = o.fetch(segment[0], 0) + 1
    c[segment[1]] = c.fetch(segment[1], 0) + 1
  end

  s = []
  x = 0
  (1..m).each do |i|
    x += o.fetch(i, 0)
    s << i if x == 0
    x -= c.fetch(i, 0)
  end
  return s
end

n, m = gets.chomp.split(" ").map(&:to_i)
segments = []
n.times { segments << gets.chomp.split(" ").map(&:to_i) }

s = solution(m, segments)

puts s.length
puts s.join(" ")

n, m = gets.split.map{|x| x.to_i}
segs = []
n.times{segs << gets.split.map{|x| x.to_i}}
points = []
(1..m).each do
    |x| points << x if segs.inject(true) do |s, y|
        s &&= !x.between?(y[0], y[1])
    end
end
puts(points.size, points.join(' '))

N, M = gets.chomp.split(/ /)
range = *(1..5)
am = 0

N.to_i.times do |i|
  u, l = gets.chomp.split(/ /)

  (u.to_i..l.to_i).each do |i|
    if range.include? i
      range.delete(i)
    end
  end
end

if range.length > 0
  p range.length
  p range
end

N, M = gets.chomp.split(/ /)
range = *(1..M.to_i)
am = 0

N.to_i.times do |i|
  u, l = gets.chomp.split(/ /)

  (u.to_i..l.to_i).each do |i|
    if range.include? i
      range.delete(i)
    end
  end
end

if range.length > 0
  p range.length
  range.each{|i| print "#{i} "}
end


n,m=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
g=Array.new(n){Array.new(n,0)}
m.times{
        p,q=gets.split.map(&:to_i)
        g[p-1][q-1]=g[q-1][p-1]=1
}
ans=9999999999
0.upto(n-1){|i|
        (i+1).upto(n-1){|j|
                if g[i][j]==0 then
                        next
                end
                (j+1).upto(n-1){|k|
                        if g[i][k]==0 || g[j][k]==0 then
                                next
                        end
                        ans=[ans,a[i]+a[j]+a[k]].min
                }
        }
}
puts (ans>=999999999)?-1:ans

n, m = gets.split("\s").map(&:to_i)
cost = gets.split("\s").map(&:to_i)
arr = Array.new(111) { Array.new }
m.times do
	u, v = gets.split("\s").map(&:to_i)
	arr[u] << v
	arr[v] << u
end

res = 1e10
for i in 1..n
	for j in (i+1)..n
		for k in (j+1)..n
			res = [res, cost[i-1]+cost[j-1]+cost[k-1]].min if arr[i].include?(j) && arr[j].include?(k) && arr[k].include?(i)
		end
	end
end

res = -1 if res == 1e10
puts res

sn, sm = gets.chop.split(/\s+/)

count = sn.to_i
countRelation = sm.to_i

costs = Array.new(0)
scosts = gets.chop.split(/\s+/)
(0..count-1).each { |i|
  costs[i] = scosts[i].to_i
}

relations = Array.new()
(0..count-1).each { |i|
  relations[i] = Array.new()
  (0..count-1).each { |j|
    relations[i][j] = false
  }
}

(0..countRelation-1).each { |i|
  su, sv = gets.chop.split(/\s+/)
  u = su.to_i - 1;
  v = sv.to_i - 1;
  relations[u][v] = true
  relations[v][u] = true
}

solves = Array.new(0)

#(0..count-2).each { |row|
##  (row..count-2).each { |col|
#    if (relations[row][col] == true and relations[row][col+1] == true and relations[row+1][col+1] == true)
#      solves[solves.length] =  costs[row] + costs[col] + costs[col+1]
#    end
#  }
#}

(0..count-1).each { |i|
  (0..count-1).each { |j|
    (0..count-1).each { |k|
      if (relations[i][j] == true and relations[j][k] == true and relations[i][k] == true)
        solves[solves.length] =  costs[i] + costs[j] + costs[k]
      end
    }
  }
}

if (solves.length > 0)
  puts solves.min
else
  puts "-1"
end

sn, sm = gets.chop.split(/\s+/)

count = sn.to_i
countRelation = sm.to_i

costs = Array.new(0)
scosts = gets.chop.split(/\s+/)
(0..count-1).each { |i|
  costs[i] = scosts[i].to_i
}

relations = Array.new()
(0..count-1).each { |i|
  relations[i] = Array.new()
  (0..count-1).each { |j|
    relations[i][j] = false
  }
}

(0..countRelation-1).each { |i|
  su, sv = gets.chop.split(/\s+/)
  u = su.to_i - 1;
  v = sv.to_i - 1;
  relations[u][v] = true
  relations[v][u] = true
}

solves = Array.new(0)

(0..count-2).each { |row|
  (row..count-2).each { |col|
    if (relations[row][col] == true and relations[row][col+1] == true and relations[row+1][col+1] == true)
      solves[solves.length] =  costs[row] + costs[col] + costs[col+1]
    end
  }
}

if (solves.length > 0)
  puts solves.min
else
  puts "-1"
end


main = -> {
  times = []
  (0 ... 24).each do |h|
      h0 = h / 10
      h1 = h % 10
      (0 ... 60).each do |m|
          m0 = m / 10
          m1 = m % 10
          times << (h0 == m1 && h1 == m0)
      end
  end

  t = int
  t.times do
      c, _x = strings
      x = _x.to_i
      time = c[0, 2].to_i * 60 + c[3, 2].to_i
      visited = [false] * (24 * 60)
      count = 0
      until visited[time]
        visited[time] = true
        count += 1 if times[time]
        time = (time + x) % (24 * 60)
      end
      puts count
  end
}

DEBUG = false
MOD = 10**9+7
INF = 10**18
$yesno = %w(No Yes)
$YesNo = %w(No Yes)
$YESNO = %w(NO YES)

require "set"
require "prime"

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

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

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

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

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

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

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

main[]

def solve(time, period)
  period = period.to_i
  h, m = time.split(":").map(&:to_i)
  a = h * 60 + m

  hash = Hash.new(false)

  result = 0

  while !hash[a]
    result += 1 if palindrome?(a)
    hash[a] = true
    a = (a + period) % 1440
  end

  result
end

def palindrome?(n)
  a = n / 60
  b = n - 60 * a

  a = a.to_s
  while a.size < 2
    a = "0" + a
  end

  b = b.to_s
  while b.size < 2
    b = "0" + b
  end

  s = [a, b].join

  s.reverse == s
end

n = gets.to_i

n.times do
  time, period = gets.split

  p solve(time, period)
end

$cnt = []

def solve
    s = gets.split('')
    hh = []
    mm = []
    hh.append(s[0])
    hh.append(s[1])
    mm.append(s[3])
    mm.append(s[4])
    h = 0
    m = 0
    k = 0
    i = 6
    hh.each do |x|
        h = h * 10
        dig = x.to_i
        h = h + dig
    end
    mm.each do |x|
        m = m * 10
        dig = x.to_i
        m = m + dig
    end
    while i < s.length - 1
        k = k * 10
        dig = s[i].to_i
        k = k + dig
        i = i + 1
    end
    guy = (h * 100) + m
    ans = 0
    while $cnt[guy] == 0
        $cnt[guy] = 1
        s = []
        if h < 10
            s.append(0)
            s.append(h)
        else
            s.append(h / 10)
            s.append(h % 10)
        end
        if m < 10
            s.append(0)
            s.append(m)
        else
            s.append(m / 10)
            s.append(m % 10)
        end
        if s[0] == s[3] and s[1] == s[2]
            ans = ans + 1
        end
        m = m + k
        h = h + (m / 60)
        h = h % 24
        m = m % 60
        guy = (h * 100) + m
    end
    puts ans
    while $cnt[guy] != 0
        $cnt[guy] = 0
        m = m + k
        h = h + (m / 60)
        h = h % 24
        m = m % 60
        guy = (h * 100) + m
    end
end

maxn = 100005
maxn.times do
    $cnt.append(0)
end
gets.to_i.times do
    solve
end

gets.to_i.times do
  t, n = gets.chomp.split(" ")
  n = n.to_i
  h = n / 60
  m = n % 60
  hash = {}
  ans = 0
  i = 0
  while hash[t].nil?
    hash[t] = i
    ans += 1 if t == t.reverse
    s1 = (t[0,2].to_i + h) % 24
    s2 = (t[3,2].to_i + m)
    if s2 >= 60
      s1 += 1
      s1 %= 24
      s2 %= 60
    end
    if s1 / 10 == 0
      s1 = '0' + s1.to_s
    else
      s1 = s1.to_s
    end
    if s2 / 10 == 0
      s2 = '0' + s2.to_s
    else
      s2 = s2.to_s
    end
    t[0,2] = s1
    t[3,2] = s2
    i += 1
  end
  puts ans
end

gets.to_i.times do
  ans = 0
  a, b = gets.chomp.split
  h, m = a.split(':').map(&:to_i)
  hh = h
  mm = m
  b = b.to_i

  loop do
    g = ''
    g += '0' if h < 10
    g += h.to_s
    g += '0' if m < 10
    g += m.to_s
    ans += 1 if g == g.reverse

    h = (h + (m + b) / 60) % 24
    m = (m + b) % 60

    break if h == hh && m == mm
  end
  puts ans
end

require "set"
palindromes = Set.new

0.upto(23).each do |h|
  0.upto(59).each do |m|
    time = "%02d:%02d" % [h, m]
    if time == time.reverse
      palindromes << h * 60 + m
    end
  end
end

gets.to_i.times do
  s, x = gets.split
  h, m = s.split(":").map(&:to_i)
  x = x.to_i
  start = h * 60 + m
  now = start
  count = 0
  loop do
    count += 1 if palindromes.include?(now)
    now += x
    now %= 1440
    break if now == start
  end
  p count
end


gets
a = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}
i = a.index [a.transpose[0].min, a.transpose[1].max]
puts i.nil? ? -1 : i+1

# Link: http://codeforces.com/problemset/problem/242/B
# Ý tưởng:
# Mỗi segment sẽ là 1 array [l, r], lưu hết segments vào 1 array.
# Đầu tiên ta gắn min_l, max_r lần lượt là l và r của segments đầu tiên.
# Lặp từ phần tử thứ 2 tới cuối mảng segments để tìm ra min_l và max_r.
# Tiếp tục lặp lần nữa từ đầu tới cuối mảng, kiểm tra xem trong mảng có phần tử nào có l, r
# bằng với min_l và max_r vừa tìm được không.
# Nếu có tồn tại ta ngay lập tức trả về index của phần tử đó + thêm 1 (vì thứ tự của segment trong input bắt đầu từ 1).
# Nếu không tìm ra phần tử nào thì trả về -1

num_of_segments = gets.chomp.to_i
segments_description = []

# save descriptions of the segments ([left, right]) into an array
for i in 0..(num_of_segments - 1)
  segment = gets.chomp.split.map(&:to_i)
  segments_description << segment
end

def index_of_segment_container(segments)
  min_l, max_r = segments[0][0], segments[0][1]

  for i in 1..(segments.length - 1)
    min_l = segments[i][0] if segments[i][0] < min_l
    max_r = segments[i][1] if segments[i][1] > max_r
  end

  for i in 0..(segments.length - 1)
    return i + 1 if segments[i][0] == min_l && segments[i][1] == max_r
  end

  return -1
end

puts index_of_segment_container(segments_description)

num_of_segments = gets.chomp.to_i
segments_description = []

# save descriptions of the segments ([left, right]) into an array
for i in 0..(num_of_segments - 1)
  segment = gets.chomp.split.map(&:to_i)
  segments_description << segment
end

def index_of_segment_container(segments)
  min_l = segments[0][0]
  max_l = segments[0][1]

  for i in 1..(segments.length - 1)
    min_l = segments[i][0] if segments[i][0] < min_l
    max_l = segments[i][1] if segments[i][1] > max_l
  end

  for i in 0..(segments.length - 1)
    return i + 1 if segments[i][0] == min_l && segments[i][1] == max_l
  end

  return -1
end

puts index_of_segment_container(segments_description)

def check(number)
  segments = []
  (1..number.to_i).each do |i|
    segments << gets.split().map(&:to_i)
  end

  step_1 = 0
  step_2 = 0

  for i in (0..segments.size - 1)
    next unless segments[i + 1]
    if (segments[step_1][0] >= segments[i + 1][0]) && (segments[step_2][1] <= segments[i + 1][1])
      step_1 = i + 1
      step_2 = i + 1
    elsif segments[step_2][1] < segments[i + 1][1]
      step_2 = i + 1
    elsif segments[step_1][0] > segments[i + 1][0]
      step_1 = i + 1
    end
  end

  puts (step_1 == step_2) ? step_1 + 1 : -1
end

check(gets)

a=Array.new{|el| el=Array.new}
gets.to_i.times{a<<gets.split.map(&:to_i)}
a.include?([a.min_by{|e|e[0]}[0],a.max_by{|e|e[1]}[1]])? (puts a.index([a.min_by{|e|e[0]}[0],a.max_by{|e|e[1]}[1]])+1):(puts -1)

#CF Round 149. Div II Prob. B - Big Segment
n = gets.chomp.to_i
seg, curr = gets.chomp.split(/ /).map(&:to_i), 1
min, max = seg[0], seg[1]
(2..n).each {|i|
    nextSeg = gets.chomp.split(/ /).map(&:to_i)
    min, max = [min, nextSeg[0]].min, [max, nextSeg[1]].max
    seg, curr = nextSeg, i if seg[0]> nextSeg[0] || seg[1] < nextSeg[1]
}
puts (min < seg[0] || max > seg[1]) ? "-1" : curr.to_s

a=Array.new{|el| el=Array.new}
gets.to_i.times{a<<gets.split.map(&:to_i)}
k,l=a.min_by{|e|e[0]}[0],a.max_by{|e|e[1]}[1]
a.include?([k,l])? (puts a.index([k,l])+1):(puts -1)

n = gets.strip.to_i

min = nil
max = nil
array = []

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

  min ||= left
  max ||= right

  min = left if left  < min
  max = right if right > max

  array << [left, right]
end

result = -1
array.each_with_index do |row, index|
  if row[0] <= min and row[1] >= max
    result =  index + 1
    break
  end
end

puts result

n = gets.to_i
min = 1000000001
max = 0
seg = -1
x, y = [0, 0]
for i in 1..n
  l, r = gets.split.map(&:to_i)
  if l <= min && r >= max
    seg = i
	x = l
	y = r
  elsif l < x || r > y
    seg = -1
  end
  min = [l, min].min
  max = [r, max].max
end
print seg

n = gets.to_i
ar = []
n.times{ar<<gets.split.map(&:to_i)}
min_ar = []
max_ar = []
ar.each{|a,b| min_ar << a; max_ar << b}
min = min_ar.min
max = max_ar.max
res = ar.rindex{|a,b| a==min && b==max}
puts res ? res+1 : -1

segment_number = gets.to_i

left_coordinates = []
right_coordinates = []
segments = []

for i in (0..segment_number - 1)
  segment = gets.split(' ')
  left_coordinate = segment[0].to_i
  right_coordinate = segment[1].to_i
  left_coordinates << left_coordinate
  right_coordinates << right_coordinate
  segments << [left_coordinate, right_coordinate]
end

min_left = left_coordinates.min
max_right = right_coordinates.max

check_result = [min_left, max_right]

result = (segments.find_index(check_result) || -2) + 1

puts result

segment_number = gets.to_i

segments = []

for i in (0..segment_number - 1)
  segment = gets.split(' ')
  left_coordinate = segment[0].to_i
  right_coordinate = segment[1].to_i
  segments << [left_coordinate, right_coordinate]
end

min_left = -1
max_right = -1

segments.each do |segment|
  min_left = segment[0] if segment[0] < min_left || min_left < 0
  max_right = segment[1] if segment[1] > max_right || max_right < 0
end

check_result = [min_left, max_right]

result = (segments.find_index(check_result) || -2) + 1

puts result

segment_number = gets.to_i

segments = []

for i in (0..segment_number - 1)
  segment = gets.split(' ')
  left_coordinate = segment[0].to_i
  right_coordinate = segment[1].to_i
  segments << [left_coordinate, right_coordinate]
end

min_left = -1
max_right = -1

segments.each do |segment|
  min_left = segment[0] if segment[0] < min_left || min_left < 0
  max_right = segment[1] if segment[1] > max_right || max_right < 0
end

check_result = [min_left, max_right]

result = (segments.index { |segment| segment[0] == check_result[0] && segment[1] == check_result[1] } || -2) + 1

puts result

segment_number = gets.to_i

segments = []

for i in (0..segment_number - 1)
  segment = gets.split(' ')
  left_coordinate = segment[0].to_i
  right_coordinate = segment[1].to_i
  segments << [left_coordinate, right_coordinate]
end

min_left = segments.map(&:first).min
max_right = segments.map(&:last).max

check_result = [min_left, max_right]

result = (segments.find_index(check_result) || -2) + 1

puts result

require 'set'

def read_next_line
  gets.split.map { |x| x.to_i }
end

min, max = 10 ** 9 + 1, 0
mins, maxs = Set.new, Set.new
for i in 1..gets.to_i
  a, b = read_next_line
  if a < min
    min = a
    mins.clear
    mins << i
  elsif a == min
    mins << i
  end
  if b > max
    max = b
    maxs.clear
    maxs << i
  elsif b == max
    maxs << i
  end
end
r = mins & maxs
puts(r.empty? ? -1 : r.first)

def check(number)
  segments = []
  (1..number.to_i).each do |i|
    segments << gets.split().map(&:to_i)
  end

  step = 1
  current_1 = segments[0].min
  current_2 = segments[0].max
  for i in (1..segments.size - 1)
    break if segments[i + 1].nil?
    if (segments[i].min <= current_1 ) && (segments[i].max >= current_2)
      current_1 = segments[i].min
      current_2 = segments[i].max
      step = i + 1
    else
      step = -1
    end
  end
  puts step
end

check(gets)

def check(number)
  segments = []
  (1..number.to_i).each do |i|
    segments << gets.split().map(&:to_i)
  end

  step = 1
  current_1 = segments[0].min
  current_2 = segments[0].max
  for i in (1..segments.size - 1)
    break if segments[i + 1].nil?
    if (segments[i].min <= current_1 ) && (segments[i].max >= current_2)
      current_1 = segments[i].min
      current_2 = segments[i].max
      step = i + 1
    elsif (segments[i].min > current_1 ) && (segments[i].max > current_2)
      step = -1
    end
  end
  puts step
end

check(gets)

def check(number)
  segments = []
  (1..number.to_i).each do |i|
    segments << gets.split().map(&:to_i)
  end

  step = -1
  current_1 = segments[0].min
  current_2 = segments[0].max
  for i in (1..segments.size - 1)
    break if segments[i + 1].nil?
    if (segments[i].min <= current_1 ) && (segments[i].max >= current_2)
      current_1 = segments[i].min
      current_2 = segments[i].max
      step = i + 1
    end
  end
  puts step
end

check(gets)

def check(number)
  segments = []
  (1..number.to_i).each do |i|
    segments << gets.split().map(&:to_i)
  end

  step_1 = 0
  step_2 = 0

  for i in (0..segments.size - 1)
    next unless segments[i + 1]
    if (segments[step_1][0] >= segments[i + 1][0]) && (segments[step_2][1] < segments[i + 1][1])
      step_1 = i + 1
      step_2 = i + 1
    elsif segments[step_2][1] < segments[i + 1][1]
      step_2 = i + 1
    elsif segments[step_1][0] > segments[i + 1][0]
      step_1 = i + 1
    end
  end

  puts (step_1 == step_2) ? step_1 + 1 : -1
end

check(gets)

n = gets.to_i
min = 1000000001
max = 0
seg = -1
x, y = [0, 0]
for i in 1..n
  l, r = gets.split.map(&:to_i)
  if l <= min && r >= max
    seg = i
	x = l
	y = r
  elsif l < x && r > y
    seg = -1
  end
  min = [l, min].min
  max = [r, max].max
end
print seg

#CF Round 149. Div II Prob. B - Big Segment
n = gets.chomp.to_i
seg, curr, min, max = gets.chomp.split(/ /).map(&:to_i), 1, 1000000000, 0;
(2..n).each {|i|
    nextSeg = gets.chomp.split(/ /).map(&:to_i)
    min, max = [min, nextSeg[0]].min, [max, nextSeg[1]].max
    seg, curr = nextSeg, i if seg[0]> nextSeg[0] || seg[1] < nextSeg[1]
}
puts (min < seg[0] || max > seg[1]) ? "-1" : curr.to_s


n = gets
seq = gets.split.map(&:to_i)
seq << 0
curr = 0
l = []
r = []
seq.each_with_index do |num, idx|
	while curr < num do
		l << idx+1
		curr += 1
	end
	while curr > num do
		r<< idx
		curr -= 1
	end
end

puts l.length
l.length.times do |idx|
	puts "#{l[idx]} #{r[idx]}"
end

gets;s=[];z=[];a=gets.split.map &:to_i;a<<0;0.upto(a.size-1){|i|(a[i-1]-a[i]).times{z<<[s.pop,i]};(a[i]-a[i-1]).times{s<<i}};p z.size;z.map{|i|puts i.join' '}


def cross? (board, x, y, n)
	if (x == 0) or (x == n) or (y > n-2)
		return false
	elsif ( board[(y+1)*(n+1)+x] != "#" or
		    board[(y+2)*(n+1)+x] != "#" or
		    board[(y+1)*(n+1)+(x-1)] != "#" or
		    board[(y+1)*(n+1)+(x+1)] != "#" )
		return false
	else
		return true
	end
end

def check_board (board, n)
	for y in 0..n
		for x in 0..n
			if (board[y*(n+1)+x] == "#")
				if (cross?(board, x, y, n))
					board[(y+1)*(n+1)+x] = "."
					board[(y+2)*(n+1)+x] = "."
					board[(y+1)*(n+1)+(x-1)] = "."
					board[(y+1)*(n+1)+(x+1)] = "."
				else
					return "NO"
				end
			end
		end
	end
	return "YES"
end

board = []
n = (gets()).to_i - 1
for i in 0..n
	(gets()).each_char{ |c| board << c}
	board.pop
end

puts check_board(board, n)


#!/usr/bin/env ruby

input = gets.chomp.split

N = Integer input[0]
K = Integer input[1]
input = gets.chomp.split

C = Integer input[0]

numbers = Array.new
for i in 0...C do
  numbers[i] = Integer input[i+1]
end
numbers[C] = N + 1

last_present = 0
ans = 0
for i in 0..C do
  if (numbers[i] - last_present > K)
    ans += (numbers[i] - 1 - last_present) / K
  end
  last_present = numbers[i]
end

ans += C
puts ans

N, K = gets.split.map(&:to_i)
t, *H = gets.split.map(&:to_i)
h = Array.new(N+1, false)
H.each { |i| h[i] = true }
ret = 0
p = 1
(1..N).each do |i|
  if p >= K
    p = 0
    ret += 1
  elsif h[i]
    ret += 1
    p = 0
  end
  p += 1
end
puts ret

n,k=gets.split.map(&:to_i)
t=gets.split.map(&:to_i)
t.shift;
t.push(n+100)
back=0;ans=0
for i in 1..n
        if t[0]==i then
                back=i
                ans+=1
                t.shift
        elsif i-back>=k then
                back=i
                ans+=1
        end
end
p ans

n,k=gets.split.map(&:to_i)
t=gets.split.map(&:to_i)
t.shift;
t.push(n+100)
back=0;ans=0
for i in 1..n
        if t[0]==i then
                back=i
                ans+=1
        elsif i-back>=k then
                back=i
                ans+=1
        end
end
p ans

N, K = gets.split.map(&:to_i)
t, *H = gets.split.map(&:to_i)
h = Array.new(N, false)
h[0] = true
H.each { |i| h[i] = true }
ret = 0
p = 0
(0...N).each do |i|
  if p >= K
    p = 0
    ret += 1
  elsif h[i]
    ret += 1
    p = 0
  end
  p += 1
end
puts ret


n, m = gets.split.map(&:to_i)
f = 1
for i in 0...n
	if(i % 2 == 0)
		m.times do
			print "#"
		end
	else
		if(f == 1)
			(m - 1).times do
				print "."
			end
			print "#"
			f = 0
		else
			print "#"
			(m - 1).times do
				print "."
			end
			f = 1
		end
	end
	puts ""
end

n,m = gets.split.map(&:to_i)
table = []
n.times do
	table.push(['.']*m)
end

for i in 0...n
	if (i%2==0)
		for j in 0...m
			table[i][j] = '#'
		end
	else
		if (i/2)%2 == 0
			table[i][-1] = '#'
		else
			table[i][0] = '#'
		end
	end
end

for i in 0...n
	for j in 0...m
		print table[i][j]
	end
	puts
end

def string_to_int(string, delim = " ")
  result = []
  string = string.split(delim)
  for i in 0...string.length
    result[i] = string[i].to_i
  end
  return result
end

input = string_to_int(gets.chomp)
n = input[0]
m = input[1]

snake = Array.new(n){Array.new(m){"."}}
for i in 0...snake.length
	for j in 0...snake[0].length
		if (i+1)%4 == 0
			snake[i][0] = "#"
			break
		elsif (i+1)%2 == 0
			snake[i][snake[0].length - 1] = "#"
			break
		else
			snake[i][j] = "#"
		end
	end
end

for i in 0...snake.length
	for j in 0...snake[0].length
		if j != snake[0].length - 1
			print snake[i][j]
		else
			puts snake[i][j]
		end
	end
end

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

a = []
counter = 0
for i in 0..n-1
  a[i] = []
  for j in 0..m-1
    if i%2 == 0
      a[i][j] = "#"
    else
      a[i][j] = "."
    end
  end

  counter+=1 if i%2 != 0

  if counter%2 == 0
    a[i][0] = "#"
  else
    a[i][-1] = "#"
  end
end

for i in 0..n-1
  for j in 0..m-1
    print "#{a[i][j]}"
  end
  puts
end

n, m = $stdin.readline.split.map(&:to_i)

d = 0
for i in 1..n
    case d
    when 0,2
        m.times {print '#'}
    when 1
        (m-1).times {print '.'}
        print '#'
    when 3
        print '#'
        (m-1).times {print '.'}
    end
    puts ''
    d = (d+1)%4
end

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

n, m = gets.split.map(&:to_i)
n.times {|i|
  if i % 2 == 0
    puts '#' * m
  elsif i % 4 == 1
    puts '.' * (m - 1) + '#'
  elsif
    puts '#' + '.' * (m - 1)
  end
}

n, m = gets.split.map(&:to_i)
right_or_left = 0
n.times do |i|
  x = if i % 2 == 0
        '#' * m
      elsif right_or_left % 2 == 0
        result = '.' * m
        result[-1] = '#'
        right_or_left += 1
        result
      else
        result = '.' * m
        result[0] = '#'
        right_or_left += 1
        result
      end
      puts x
end

n, m = gets.chomp.split.map &:to_i
ind = 'r'
n.times do |x|
  if (x + 1) % 2 != 0
    m.times do
      print "#"
    end
    puts
  else
    if ind == 'r'
      (m - 1).times do
        print '.'
       end
    print '#'
    puts
    ind = 'l'
    else
      print '#'
      (m - 1).times do
        print '.'
      end
      puts
      ind = 'r'
    end
  end
end

n, m = gets.split.map(&:to_i)
(1..n).each { |i|
  (1..m).each { |j|
    if i&1==1
      print '#'
    else
      if (i&3==2 && j==m) || (i&3==0 && j==1)
        print '#'
      else
        print '.'
      end
    end
  }
  print "\n"
}

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

(0..n-1).each do |i|
  line = ""
  (0..m-1).each do |j|
    if i.even? || (j == m - 1) && (i % 4 == 1) || (j == 0) && (i % 4 == 3)
      line += "#"
    else
      line += "."
    end
  end
  puts line
end

n, m = gets.chomp.split(/ /).map(&:to_i)
left = true
right = false
(1..n).each do |row|
    line = nil
    if row.odd?
        line = Array.new(m, "#")
    else
        line = Array.new(m, ".")
        if left && !right
            line[m - 1] = "#"
            left = false
            right = true
        elsif right && !left
            line[0] = "#"
            left = true
            right = false
        end
    end
    puts line.join
end

a=gets.chomp.split(" ")
tok=0
a[0]=a[0].to_i
a[1]=a[1].to_i
hash=""
a[1].times do
    hash=hash+"#"
end
to=""
(a[1]-1).times do
    to=to+"."
end
to=to+"#"
four="#"
(a[1]-1).times do
    four=four+"."
end
1.upto(a[0]) do |i|
if (i%2==1)
    puts "#{hash}"
elsif (i%2==0 and tok==0)
    puts "#{to}"
    tok=1
else
    puts "#{four}"
    tok=0
end
end

h,w=gets.chomp.split.map{|e| e.to_i}

h.times{|i|
	if i%2==0 then
		puts ("#"*w)
	elsif i%4==1 then
		puts (("."*(w-1))+"#")
	else
		puts ("#"+("."*(w-1)))
	end
}

n,m = gets.chomp.split.map(&:to_i)
j = 0
n.times.each do |i|
  if i % 2 ==0
    puts '#' * m
  else
    s = '.' * m
    if j % 2 == 0
      s[m-1] = '#'
    else
      s[0] = '#'
    end
    j += 1
    puts s
  end
end

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

for i in 1..n do
  puts '#'.rjust(m, '#') if i.odd?
  puts '#'.rjust(m, '.') if i%4 != 0 and i.even?
  puts '#'.ljust(m, '.') if i%4 == 0
end

#!/usr/bin/ruby

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

def filled(input)
  puts '#' * input.last
end

def points_left(input)
  puts "#{ '.' * (input.last - 1)}#"
end

def points_right(input)
  puts "##{ '.' * (input.last - 1 )}"
end

sign = -1

input.first.times do |i|
  if i % 2 == 0
    filled input
  elsif sign > 0
    sign *= -1
    points_right input
  else
    sign *= -1
    points_left input
  end
end

n,m=gets.split(" ").map {|x| x.to_i}
i = 1
while i<=n do
  if i%4==0
    print "#"
    j = 2
    while j<=m do
      print "."
      j+=1
    end
  elsif i%4==2
    j=1
    while j<m do
      print "."
      j+=1
    end
    print "#"
  else
    j=1
    while j<=m do
      print "#"
      j+=1
    end
  end
  puts("")
  i+=1
end

a = gets.split(" ")
n = a[0].to_i
m = a[1].to_i
k = 0
for i in 1..n do
    if i % 2 == 0 then
        k = k + 1
        if k % 2 == 1 then
            puts "." * (m - 1) + "#"
        else
            puts "#" + "." * (m - 1)

        end
    else
        puts "#" * m
    end
end

input = gets.chomp
snake = input.split

rows = snake[0].to_i
cells = snake[1].to_i

spaces = []
for i in (1..rows)
	if i%2 == 0
		spaces << i
	end
end

for i in (1..rows)
	if i%2 != 0
		for cell in (1..cells)
			if cell == cells
				puts "#"
			else
				print "#"
			end
		end
	else
		x = spaces.find_index(i)
		if (x+1)%2 != 0
			for space in (1..cells)
				if space == cells
					puts "#"
				else
					print "."
				end
			end
		else
			for space in (1..cells)
				if space == 1
					print "#"
				elsif space > 1 && space < cells
					print "."
				else
					puts "."
				end
			end
		end
	end
end

R,C=gets.split.map(&:to_i)
R.times{|i|
	if i%4==1
		puts '.'*(C-1)+'#'
	elsif i%4==3
		puts '#'+'.'*(C-1)
	else
		puts '#'*C
	end
}

class Snake
  def initialize(n, m)
    @n, @m = n, m
  end

  def draw
    rows.each { |row| puts row.join }
  end

  private

  def rows
    [].tap do |rows|
      @n.times do |idx|
        case
        when idx.even?
          rows << ['#'] * @m
        when rows[idx - 2].nil?, rows[idx - 2].first == '#'
          rows << ['.'] * (@m - 1) + ['#']
        else
          rows << ['#'] + ['.'] * (@m - 1)
        end
      end
    end
  end
end

n, m = gets.chomp.split.map(&:to_i)
snake = Snake.new(n, m)
snake.draw

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

n.times do |i|
	#puts i
	m.times do |j|
		#print "#{j} "
		if i%2==0
			print "#"
		elsif i/2%2==0 && j==m-1
			print "#"
		elsif i/2%2==1 && j==0
			print "#"
		else
			print "."
		end
	end
	print "\n"
end

n,m = gets.split(" ").map(&:to_i)
flip = 0
res = []
(1..n).each do |i|
  if i % 2 != 0
    res << ["#"] * m
  else
    row = ["."] * (m - 1)
    if flip == 1
      row.unshift("#")
    else
      row << "#"
    end
    res << row
    flip ^= 1
  end
end
res.each do |r|
  puts r.join
end

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

def getLine(i)
  return "#"*$m if i%2==0
  return "."*($m-1)+"#" if i%4==1
  return "#"+"."*($m-1)
end

puts Array.new($n) {|i| getLine(i)+"\n"}.inject(:+)

n, m = gets.split.map(&:to_i)
a = Array.new(n){Array.new(m, ".")}
for i in 0...n
  if i % 2 == 0
    a[i] = Array.new(m, "#")
  elsif i % 4 == 1
    a[i][m - 1] = "#"
  elsif i % 4 == 3
    a[i][0] = "#"
  end
end
puts a.map{|x| x.join("")}

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

arr = []
for i in 0..n-1
    arr[i] = Array.new(m)
    arr[i].fill('.')
end

i = 0
while i < n do
    arr[i].fill('#')
    i += 2
end

right = true
i = 1
while i < n do
    if right
        arr[i][m-1] = '#'
    else
        arr[i][0] = '#'
    end
    right = !right
    i += 2
end

arr.each do |row|
    puts row.join
end

#!/usr/bin/env ruby

input = ARGF.gets

n, m = input.split " "
n = n.to_i
m = m.to_i

right = true

ni = 0
n.times do
    if ni.even?
        puts '#' * m
    else
        if right
            right = false
            print '.' * (m - 1)
            puts '#'
        else
            right = true
            print '#'
            puts '.' * (m-1)
        end

    end

    ni += 1

end

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

1.upto(n) do |i|
  case i.modulo(4)
  when 1, 3
    puts '#'*m
  when 2
    puts "#{'.'*(m-1)}\#"
  when 0
    puts "\##{'.'*(m-1)}"
  end
end

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

right = true
n.times do |i|
  if i%2 == 0
    puts '#' * m
  else
    str = '.' * (m - 1)
    str.insert(right ? -1 : 0, '#' )
    right = !right
    puts str
  end
end

a=Array.new
str=gets.to_s
a=str.split
n=a[0].to_i
m=a[1].to_i
i=h=0
while i<n
  j=0
  while j<m
    print "#"
    j+=1
  end
  puts
  i+=1
  if i>=n
    break
  end
  if h==0
    j=0
    while j<m-1
      print "."
      j+=1
    end
    puts "#"
    h=1
  elsif h==1
    j=0
    print "#"
    while j<m-1
      print "."
      j+=1
    end
    puts
    h=0
  end
  i+=1
 end

n,m=gets.split().map{|i| Integer(i)}
count=0
for i in (0...n)
	if i%2==0
		puts "#"*m
	else
		count+=1
		if count%2!=0
			puts "."*(m-1)+"#"
		else
			puts "#"+"."*(m-1)
		end
	end
end

n, m = gets.chomp.split(' ').map { |i| i.to_i }
n.times do |iter|
    if iter.even?
        m.times { print '#' }
    else
        print '#' if iter % 4 == 3
        (m - 1).times { print '.' }
        print '#' if iter % 4 == 1
    end

    puts ''
end

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

a = []
counter = 0
for i in 0..n-1
  a[i] = []
  for j in 0..m-1
    if i%2 == 0
      a[i][j] = "#"
    else
      a[i][j] = "."
    end
  end

  counter+=1 if i%2 != 0

  if counter%2 == 0
    a[i][0] = "#"
  else
    a[i][-1] = "#"
  end
end

puts
for i in 0..n-1
  for j in 0..m-1
    print "#{a[i][j]}"
  end
  puts
end

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

arr = []
for i in 0..n-1
    arr[i] = Array.new(m)
    arr[i].fill(".")
end

i = 0
while i < n do
    arr[i].fill("#")
    i += 2
end

right = true
i = 1
while i < n do
    if right
        arr[i][m-1] = "#"
    else
        arr[i][0] = "#"
    end
    right = !right
    i += 2
end

arr.each do |row|
    p row.join
end

n, m = gets.chomp.split(' ').map { |i| i.to_i }
n.times do |iter|
    if iter.even?
        m.times { print '#' }
    else
        print '#' if iter % 3 == 1
        (m - 1).times { print '.' }
        print '#' if iter % 3 == 0
    end

    puts ''
end

n, m = gets.chomp.split(' ').map { |i| i.to_i }
n.times do |iter|
    if iter.even?
        m.times { print '#' }
    else
        print '#' if iter % 4 == 1
        (m - 1).times { print '.' }
        print '#' if iter % 4 == 3
    end

    puts ''
end

n, m = gets.chomp.split(' ').map { |i| i.to_i }
n.times do |iter|
    if iter.even?
        m.times { print '#' }
    else
        print '#' if iter % 3 == 0
        (m - 1).times { print '.' }
        print '#' if iter % 3 >= 1
    end

    puts ''
end

n, m = gets.chomp.split(' ').map { |i| i.to_i }
n.times do |iter|
    if iter.even?
        m.times { print '#' }
    else
        print '#' if iter % 3 == 0
        (m - 1).times { print '.' }
        print '#' if iter % 3 == 1
    end

    puts ''
end


#!/usr/bin/ruby
h=Hash.new(0)
r=0
gets
gets.split.map(&:to_i).each{|e|h[e]+=1}
h.each{|k,v|
	x=2
	while x<=0x80000000
		y=x-k
		if k==y
			r+=v*(v-1)/2
		elsif k<y
			r+=v*h[y]
		end
		x*=2
	end
}
p r

n = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)
h = Hash.new { 0 }.tap do |hash|
  a.each { |k| hash[k] += 1 }
end
max = [1, a.max].max * 2
pows = (0..Math.log(max, 2).ceil).map { |i| 2**i }
ans = 0
h.keys.each do |k|
  pows.each do |pow|
    next if pow < k
    if h.key?(pow - k) && (pow - k) == k
      ans += h[k] * (h[pow - k] - 1)
    else
      ans += h[k] * h[pow - k]
    end
  end
end
puts ans / 2

n = gets.chomp.to_i
as = gets.chomp.split.map(&:to_i)
hs = Hash.new(0)
amax = 1

as.each do |a|
  hs[a] += 1
  amax = [amax, a].max
end

ans = 0
hs.each do |k, v|
  i = 1
  while 2 ** i <= amax * 2
    if k == 2 ** (i - 1)
      ans += v * (v - 1)
    else
      ans += v * hs[2**i - k]
    end

    i += 1
  end
end

puts ans / 2


t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  ans << (n+2)/2
end
ans.each do |x|
  puts x
end

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  if n == 1
    ans << 1
  elsif n == 2 || n == 3
    ans << 2
  else
    ans << (n+3)/2
  end
end
ans.each do |x|
  puts x
end


def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end

def scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end

def na(n=0,d=0) Array.new(n,d)end

def na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end

def na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def bit(n) n.to_s(2).split("").map(&:to_i) end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def c(i, now)
  if(i==10)
    @anss << now
    return
  end
  c(i+1,now)
  c(i+1,now + @t3[i])
end

@t3 = []
(0..9).each{|d| @t3 << 3**d}
@anss = []
c(0,0)
@anss.sort!
n = inp[0]
n.times do
  q = inp[0]
  puts @anss.bsearch{|x| x >=  q }
end

def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end

def scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end

def na(n=0,d=0) Array.new(n,d)end

def na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end

def na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def bit(n) n.to_s(2).split("").map(&:to_i) end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

t3 = []
(0..44).each{|d|t3<<3**d}

n = inp[0]
n.times do
  q = inp[0]
  t = []
  while (q > 0)
    t << q % 3
    q /= 3
  end
  t.reverse!
  ind = -1
  t.size.times do |i|
    if(t[i] == 2)
      ind = i + 1
      break
    end
  end
  t.unshift(0)
  ans = 0

  (0...(t.size)).reverse_each do |i|
    break if ind < 0
    if(ind <= i)
      t[i] = 0
    end
    if(ind > i)
      if(t[i] == 0)
        t[i] = 1
        break
      else
        t[i] = 0
      end
    end
  end

(0...(t.size)).each do |i|
    ans += t3[t.size-i-1] if(t[i] >= 1)
  end
  p ans
end


class Array
  def findB
    x0 = 0
    x1 = size-1
    return self[x0] if yield(self[x0])
    return false unless yield(self[x1])

    while x1-x0 > 1
      x2 = (x0+x1)/2

      if yield(self[x2])
        x1 = x2
      else
        x0 = x2
      end
    end
    return self[x1]
  end
end

def ceil(a, b)
  (a+b-1)/b
end

def solve(from, to)
  chrs = {}
  0.upto(from.size-1) do |i|
    chrs[from[i]] = true
  end

  unless to.each_char.all?{|x|chrs[x]}
    return -1
  end

  tbl = {}
  chrs.keys.each do |c|
    idx = 0
    o = tbl[c] = [0]*from.size
    0.upto(from.size*2-1) do |i|
      if from[i%from.size] == c
        idx.upto(i-1) do |j|
          o[j%from.size] = i%from.size
        end
        break if i >= from.size
        idx = i
      end
    end
  end

  result = 1
  pos = -1
  ptr = Hash.new(-1)
  to.each_char do |x|
    new_pos = tbl[x][pos]
    #p [x, pos, new_pos, result, tbl[x]]
    result += 1 if new_pos <= pos
    pos = new_pos
  end

  result
end

from = gets.chomp
to = gets.chomp
p solve(from, to)

def error
	p -1
	exit
end
s,t=gets.chomp,gets.chomp
next_pos={}
closest_pos={}
i,pos,ans=s.length,-1,1
s.reverse.each_byte{|b|
	i-=1
	closest_pos.each{|k,v|next_pos[(i<<7)+k]=v}
	closest_pos[b]=i
}
closest_pos.each{|k,v|next_pos[(-1<<7)+k]=v}

t.each_byte{|b|
	pos=next_pos[(pos<<7)+b]
	unless pos
		ans+=1
		pos=next_pos[(-1<<7)+b]
		error unless pos
	end
}
p ans

def error
	p -1
	exit
end
s,t=gets.chomp,gets.chomp
error if s.size>t.size
next_pos={}
closest_pos={}
i,pos,ans=s.length,-1,1
s.reverse.each_byte{|b|
	i-=1
	closest_pos.each{|k,v|next_pos[[k,i]]=v}
	closest_pos[b]=i
}
closest_pos.each{|k,v|next_pos[[k,-1]]=v}

t.each_byte{|b|
	pos=next_pos[[b,pos]]
	unless pos
		ans+=1
		pos=next_pos[[b,-1]]
		error unless pos
	end
}
p ans


n, m = gets.split.collect{|i| i.to_i}
v = gets.split.collect{|i| i.to_i}
ans = 0
(1..m).each do
    tmp = gets.split.collect{|i| i.to_i}
    ans += [v[tmp[0]-1],v[tmp[1]-1]].min
end
puts ans

# Create reachability matrix
def reach(links,n)
    reachability = Array.new(n) {[]}
    for i in 0..(links.length - 1) do
        node = links[i].map{|c| c - 1}
        reachability[node[0]].push(node[1])
        reachability[node[1]].push(node[0])
    end
    return reachability
end

# Cost of removing a given node
def cost(node, energies, connections, dead_nodes)
    cost = 0
    for i in connections[node] do
        cost = cost + energies[i] unless dead_nodes[i]
    end
    return cost
end

# Initialize variables
links = []; energies = []; connections = []; dead_nodes = []; energy_needed = 0;

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

# Create connectivity matrix
connections = reach(links, n)

# Initialize dead_node structures
dead_nodes = Array.new(n,false)

# Loop to find min total cost
order = energies.map.with_index.sort.reverse.map(&:last)
for i in 0..(order.length - 1) do
    energy_needed = energy_needed + cost(order[i], energies, connections, dead_nodes)
    dead_nodes[order[i]] = true
end
puts energy_needed

# Create reachability matrix
def reach(links,n)
    reachability = Array.new(n) {[]}
    for i in 0..(links.length - 1) do
        node = links[i].map{|c| c - 1}
        reachability[node[0]].push(node[1])
        reachability[node[1]].push(node[0])
    end
    return reachability
end

# Cost of removing a given node
def cost(node, energies, connections, dead_nodes)
    cost = 0
    for i in connections[node] do
        cost = cost + energies[i] unless dead_nodes[i]
    end
    return cost
end

# Initialize variables
links = []; energies = []; connections = []; dead_nodes = []; energy_needed = 0;

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

# Create connectivity matrix
connections = reach(links, n)

# Initialize dead_node structures
dead_nodes = Array.new(n,false)

# Sorting of nodes by their energies
order = energies.map.with_index.sort.reverse.map(&:last)

# Loop over nodes to get min energy
for i in 0..(order.length - 1) do
    energy_needed = energy_needed + cost(order[i], energies, connections, dead_nodes)
    dead_nodes[order[i]] = true
end
puts energy_needed

require 'set'
r=lambda{ gets.split.map(&:to_i) }
n,m=r.call
a=r.call
g=Array.new(n){ Set.new }
m.times{
	x,y=r.call
	g[x-1].add(y-1)
	g[y-1].add(x-1)
}
s=0
a.map.with_index.sort_by(&:first).map(&:last).reverse.each{ |e|
	g[e].each{|v| s+=a[v];g[v].delete e}
}
p s

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

m.times do
	u, v = gets.split.map { |x| x.to_i }
	ans += [f[u-1], f[v-1]].min
end

print ans, ?\n

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

puts (0...m).map { |_|
	a,b = gets.split.map(&:to_i)
	[v[a-1], v[b-1]].min
}.inject(0, &:+)

# Create reachability matrix
def reach(links,n)
    reachability = Array.new(n) {[]}
    for i in 0..(links.length - 1) do
        node = links[i].map{|c| c - 1}
        reachability[node[0]].push(node[1])
        reachability[node[1]].push(node[0])
    end
    return reachability
end

# Cost of removing a given node
def cost(node, energies, connections, dead_nodes)
    cost = 0
    for i in connections[node] do
        cost = cost + energies[i] unless dead_nodes[i]
    end
    return cost
end

# Costs for removing each node in a given configuration
def run_round(energies, connections, dead_nodes)
    total_cost = 0; costs = Array.new(energies.length, 0);
    for i in 0..(energies.length - 1) do
        unless dead_nodes[i]
            costs[i] = cost(i, energies, connections, dead_nodes)
            total_cost = total_cost + costs[i]
        end
    end
    return total_cost, costs
end

def active_links(node, connections, dead_node_indices)
    return (connections[node] - dead_node_indices).length
end

# Initialize variables
links = []; energies = []; connections = []; energy_needed = 0;

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

# Create connectivity matrix
connections = reach(links, n)

# Initialize dead_node structures
dead_nodes = Array.new(n,false); dead_node_indices = [];

# Loop to find min total cost
while dead_nodes.include?(false)
    total_cost, costs = run_round(energies, connections, dead_nodes)
    cost_ratio = Array.new(energies.length, Float::INFINITY);
    for i in 0..(n- 1) do
        unless dead_nodes[i]
            cost_ratio[i] = (costs[i] == 0) ? 0 : (1.0 * costs[i]) / (energies[i] * active_links(i, connections, dead_node_indices) + costs[i])
        end
    end
    best_bet = cost_ratio.index(cost_ratio.min)
    dead_nodes[best_bet] = true; dead_node_indices.push(best_bet);
    energy_needed = energy_needed + costs[cost_ratio.index(cost_ratio.min)]
end

puts energy_needed

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

puts (0...m).map { |_|
	a,b = gets.split.map(&:to_i)
	[v[a-1], v[b-1]].min
}.inject(&:+)


n = gets.to_i
if n%3==0
    print "1 1 #{n-2}"
else
    print "1 2 #{n-3}"
end

n = gets.to_i

if n % 3 == 0
  puts "1 1 #{n-2}"
else
  puts "1 2 #{n-3}"
end

n=gets.to_i
fn=sn=tn=0
if n%2==0
	fn=2
	n-=2
else
	fn=1
	n-=1
end

sn=tn=n/2

if sn%3==0
	sn-=1
	tn+=1
end

puts "#{fn} #{sn} #{tn}"

n = gets.to_i

if n % 3 == 0
    puts "1 1 #{n - 2}"
else
    puts "1 2 #{n - 3}"
end