#! /usr/bin/ruby

$n = gets.to_i
$cards = gets.split(" ")

def card_compare( a, b )
  a[0] == b[0] or a[1] == b[1]
end

def ff( a, i )
  a[i] = a[0]
  a.shift
  return a
end

def f( a )
  if a.length == 1
    return true
  end
  #####################
  if card_compare( a[0], a[1] )
    b = a.clone
    b = ff( b, 1 )
    if f( b )
      return true
    end
  end
  #####################
  if a.length > 3
    if card_compare( a[0], a[3] )
      b = a.clone
      b = ff( b, 3 )
      if f( b )
	return true
      end
    end
  end
  #####################
  return false
end

if f( $cards.reverse! )
  puts "YES"
else
  puts "NO"
end


#f = File.open("b.in")
@n = gets.to_i
@a = gets.split

def doit(b, k, c)
   return true if c == 1
   return false if c == 0
   r = false

   (k - 1).downto(0) { |i|
      if b[k][0] == b[i][0] or b[k][1] == b[i][1]
         d = b.dup
         d[i] = d[k]
         r = r | doit(d, k - 1, c - 1)
      end
   }

   return r
end

r = doit(@a, @n - 1, @n)
puts "YES" if r
puts "NO" if not r


vp,vd,t,f,c=gets.to_i,gets.to_i,gets.to_i,gets.to_i,gets.to_i
if vp>vd then
        puts 0
        exit
end
time=Rational(t,1)
for ans in 0..100000
        #t*vp+time*vp=(vd-vp)*t
        #t*(vp-vd)=-vp*time
        tmp=Rational(-vp*time,vp-vd)
        #p [tmp,vp,vd]
        if tmp+time>=Rational(c,vp) then
                puts ans
                exit
        end
        time+=tmp*2+f
end


u, v, t, f, c = 5.times.map{gets.to_f}
d, ans = u * t, 0

if v > u
    loop do
        d += d / (v - u) * u
        break if d >= c
        d += (d / v + f) * u
        k += 1
    end
end
p ans


def escape
  vp = STDIN.gets.to_i
  vd = STDIN.gets.to_i
  t  = STDIN.gets.to_i
  f  = STDIN.gets.to_i
  c  = STDIN.gets.to_i

  arr = { 0 => [0,0], 1 => [vp,0] }
  ind = 1
  while arr[ind][0] < c
    ind += 1
    arr[ind] = [arr[ind-1][0] + vp,0]
  end

  b = i = 0
  while i < arr.size
    if i > t
      arr[i][1] += arr[i-1][1] + vd
    end

    if arr[i][0] == arr[i][1] and arr[i][0] != 0
      b += 1
      while i < arr.size-1 and arr[i][1] > 0
        i += 1
        arr[i][1] = [arr[i-1][1] - vd, 0].max
      end
      f.times do
        i += 1
        break if i >= arr.size
        arr[i][1] = 0
      end
    end

    i += 1
  end
  puts b
end

escape


vp = gets.to_i
vd = gets.to_i
t = gets.to_i
f = gets.to_i
c = gets.to_i

if tp >= td
    dem = 0
else
    mile = c/vp
    dem = 0
    T = []
    T[dem] = t*vp/(vd-vp)

    while t < mile do
        t += T[dem]
        if t < mile
            t += T[dem] + f
            dem += 1
            T[dem] = t*vp/(vd-vp)
        end
    end
end

puts dem


args = gets.split(" ").map{ |i| i.to_i}
a = args[0]
b = args[1]
res = 0
heap = [0, a]
current = 1
finish = false
while !finish
  for i in current...current * 2
    if (heap[i] > 0)
      left = heap[i] - 1
      right = 2 * heap[i]
      heap[2*i] = left
      heap[2*i+1] = right
      if (left == b || right == b)
        puts Math.log2(i).to_i + 1
        finish = true
        break
      end
    end
  end
  current *= 2
end


args = gets.split(" ").map{ |i| i.to_i}
a = args[0]
b = args[1]
res = 0
heap = [0, a]
current = 1
finish = false
while !finish
  for i in current...current * 2
    if (heap[i] > 0)
      left = heap[i] - 1
      right = 2 * heap[i]
      heap[2*i] = left
      heap[2*i+1] = right
      if (left == b || right == b)
        puts Math.log2(i).to_i + 1
        finish = true
        break
      end
    else
      heap[2*i] = 0
      heap[2*i+1] = 0
    end
  end
  current *= 2
end


class StrangeDevice
  def initialize(n, m)
    @m = m
    @array = [n]
  end

  def minimum_times
    t = 0
    until @array.include?(@m)
      operate
      t += 1
    end

    t
  end

  def self.minimum_times(n, m)
    new(n, m).minimum_times
  end

  private

  def operate
    @array = [].tap do |out|
      @array.each do |a|
        out << a * 2
        out << a - 1
      end
    end
  end
end

n, m = gets.chomp.split.map(&:to_i)
puts StrangeDevice.minimum_times(n, m)


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

n, m = gets.split.map &:to_i
t = 0
while n < m do
  m = ((m & 1) ? m + 1 : m / 2)
  t += 1
end
puts t + (n - m).abs


data = gets.chomp.split

a = data[0].to_i
b = data[1].to_i

n=0

if b<a
	n=a-b
else
	while b!=a
		if b%2!=0
			b+=1
			n+=1
		else
			b/=2
			n+=1
		end
	end
end

puts n


a=gets.split(' ').map(&:to_i)
q = Queue.new
q.push([a[0],0])
while true
  val = q.pop
  if val[0]==a[1]
    puts val[1]
    exit
  end
  q.push([val[0]<<1,val[1]+1])
  q.push([val[0]-1,val[1]+1])
end


arr = []
for i in 1..3
  arr.push gets.chomp.to_i
end
res = []

res.push  (arr[0]+ arr[1]) * arr[2]
res.push  arr[0] * arr[1]* arr[2]
res.push  arr[0] + arr[1] + arr[2]

print res.max


def prime?( x )
    return false if x <= 1
    (2..x).each do |i|
        break if i * i > x
        return false if x % i == 0
    end
    return true
end
def next_prime( x )
    return 2 if x <= 2
    n = x + 1
    while !prime? n
        n += 1
    end
    n
end
n, m = gets.strip.split.map(&:to_i)
a = next_prime n
if a == m
    puts "YES"
else
    puts "NO"
end


require 'prime'
#-------------------

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

for i in n+1..m
	if i.prime?
		(i==m) ? (puts "YES") : (puts "NO")
		break
	end
end


require 'date'

x, y, z=gets.split
x=x.to_i

res=0
a=Date.new(2016, 1, 1)
365.times do
    if z=="week" and a.cwday==x then
        res+=1
    elsif z=="month" and a.day==x then
        res+=1
    end
    a=a.next
end
p res


s=gets.chomp.to_s
s=s.split(" ")
r=s[0].to_i
c=s[1].to_i
row_straw=[]
col_strar=[]
rows= Array.new(r)
r.times{|iter|
	rows[iter]=gets.chomp.to_s
	c.times{|iter2|
		if (rows[iter][iter2]=='S')
			#record
			col_strar.unshift(iter2)
			row_straw.unshift(iter)
	}
}
puts((r-row_straw.size)*c+(c-col_straw.count)*(row_straw.count))


s=gets.chomp.to_s
s=s.split(" ")
r=s[0].to_i
c=s[1].to_i
row_straw=[]
col_straw=[]
rows= Array.new(r)
r.times{|iter|
	rows[iter]=gets.chomp.to_s
	c.times{|iter2|
		if (rows[iter][iter2]=='S')
			#record
			col_straw.unshift(iter2)
			row_straw.unshift(iter)
		end
	}
}
puts((r-row_straw.size)*c+(c-col_straw.count)*(row_straw.count))


rc = gets.chomp.split.map(&:to_i)
r = rc[0]
c = rc[1]
cake = []
for i in (0..r-1)
	cake << gets.chomp.split("")
end

count = 0
evil = []
for i in (0..r-1)
	if cake[i].include?("S") == false
		count += c
		r -= 1
	else
		if evil.include?(cake[i].index("S")) == false
			evil << cake[i].index("S")
		end
	end
end

count += (c-evil.size)*r

print count


args = STDIN.gets.split.map do |i|
	i.to_i
end
x = 1
y = 1
xx = Array.new(args[0], 0)
yy = Array.new(args[1], 0)
#s = {}
args[0].times do
	l = STDIN.gets
	args[1].times do |i|
		if 'S' == l[i]
			#s.store((y*10)+i+1, 1)
			xx[i] = 1
			yy[y-1] = 1
		else
			#s.store((y*10)+i+1, 0)
		end
	end
	y += 1
end

res = 0
tmp = 0
yyy = Array.new(args[1], 0)

args[0].times do |x|
	args[1].times do |y|
		next if yy[y] == 1
		tmp += 1
		yyy[y] = 1
	end
end
args[1].times do |y|
	args[0].times do |x|
		next if xx[x] == 1 or yyy[y] == 1
		tmp += 1
	end
end
res = tmp if res < tmp
#puts res
tmp = 0
xxx = Array.new(args[0], 0)
args[1].times do |y|
	args[0].times do |x|
		next if xx[x] == 1
		tmp += 1
		xxx[x] = 1
	end
end
args[0].times do |x|
	args[1].times do |y|
		next if yy[y] == 1 or xxx[x] == 1
		tmp += 1
		yyy[y] = 1
	end
end
#puts res
res = tmp if res < tmp
#puts res


p = ('H','Q','+','9').to_a
s = gets.to_s.split(//)
s = s & p
if s.size>0
    print 'YES'
else
    print 'NO'
end


n = gets.chomp.to_i
dominos = []
n.times do
    dominos << gets.chomp.split(/ /).map(&:to_i)
end
time = row = 0
while true
    if row == n
        time = -1
        break
    end
    upper = lower = 0
    dominos.each do |domino|
        u = domino[0]
        l = domino[1]
        upper += u
        lower += l
    end
    if upper.even? && lower.even?
        break
    else
        dominos[row][0], dominos[row][1] = dominos[row][1], dominos[row][0]
        time += 1
        row += 1
    end
end
puts time


odd=0
l1=0
r1=0
n=gets.to_i
n.times{
	a,b=gets.split.map{|e| e.to_i}
	l1+=a%2
	r1+=b%2
	odd+=1 if (a+b)%2==1
}
if (l1+r1)!=0 then
	if odd<2 then
		puts -1
	else
		puts 1
	end
else
	puts 0
end


n = gets.chomp.to_i
matrixs = []
n.times do |i|
  matrixs << gets.chomp.split(" ").map{|x| x.to_i }
end
even_even = 0
odd_odd = 0
even_odd = 0
odd_even = 0
matrixs.each do |domino|
  a, b = domino
  if a % 2 == 0
    if b % 2 == 0
      even_even += 1
    else
      even_odd += 1
    end
  else
    if b % 2 == 0
      odd_even += 1
    else
      odd_odd += 1
    end
  end
end

if odd_odd % 2 == 1
  if even_odd == 0 || odd_even == 0
    puts -1
  elsif even_odd % 2 == 0
    if odd_even % 2 == 0
      puts 1
    else
      puts -1
    end
  else
    if odd_even % 2 == 0
      puts -1
    else
      if (odd_even + odd_odd) % 2 == 0 && (even_odd + odd_odd) % 2 == 0
        puts 0
      else
        puts -1
      end
    end
  end
elsif even_odd % 2 == 0
  if odd_even % 2 == 0
    puts 0
  else
    puts -1
  end
else
  if odd_even % 2 == 0
    puts -1
  else
    puts 1
  end
end


n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}
c = Array.new(4, 0)
a.each do |x, y|
  case [x.even?, y.even?]
  when [true, true]
    c[0] += 1
  when [true, false]
    c[1] += 1
  when [false, true]
    c[2] += 1
  when [false, false]
    c[3] += 1
  end
end
if (c[2] + c[3]) % 2 == 0 && (c[1] + c[3]) % 2 == 0
  puts 0
elsif (c[2] + c[3] + 1) % 2 == 0 && (c[1] + c[3] - 1) % 2 == 0
  puts 1
else
  puts -1
end


wh,wm=gets.chomp.split(':').collect{|a| a.to_i}
sh,sm=gets.chomp.split(':').collect{|a| a.to_i}
rh=0
rm=0
if wm<sm
 wh-=1
 wm+=60
end
rm=wm-sm
if wh== -1
 wh=0
end
rh=wh-sh
if rh <0
  rh=24 +rh
end
res_h=rh.to_s
if(rh<10)
res_h="0" +res_h
end
res_m=rm.to_s
if(rm<10)
res_m="0"+res_m
end
res=res_h+":"+res_m
puts res


def getTime(time)
	hour = (time[0].to_i - '0'.to_i) * 10 + time[1].to_i - '0'.to_i
	minute = (time[3].to_i - '0'.to_i) * 10 + time[4].to_i - '0'.to_i
	return [hour, minute]
end

def getFellAsleep(wake_up, slep)
	fell_asleep = [wake_up[0] - slept[0], wake_up[1] - slept[1]]

	if fell_asleep[1] < 0
		fell_asleep[1] += 60
		fell_asleep[0] -= 1
	end

	fell_asleep[0] += 24 if fell_asleep[0] < 0

	return fell_asleep
end

wake_up = getTime(gets.chomp)
slept = getTime(gets.chomp)

fell_asleep = getFellAsleep(wake_up, slept)

puts fell_asleep[0].to_s.rjust(2, "0") + ":" + fell_asleep[1].to_s.rjust(2, "0")


# s = current time, first line hh:mm
# t = time slept, seconde line hh:mm

s = gets.strip.split(":").map! {|i| i.to_i}
shh = s[0]
smm = s[1]

t = gets.strip.split(":").map! {|i| i.to_i}
thh = t[0]
tmm = t[1]

ahh = (shh-thh)
amm = (smm-tmm)
if amm < 0
  ahh = ahh - 1
  ahh = 60 + ahh
end
if ahh < 0
  ahh = 24 + ahh
end
puts "%02d:%02d" % [ahh, amm]
#puts (shh - thh).to_s + ":" + (smm-tmm).to_s


#!/usr/bin/env ruby
T='CODEFORCES'
S=gets.chomp
S.size.times{|i|
	i.step(S.size-1){|j|
		if S[0,i]+S[j..-1]==T
			puts :YES
			exit
		end
	}
}
puts :NO


#!/usr/bin/env ruby
T='CODEFORCES'
S=gets.chomp
0.step(S.size).times{|i|
	i.step(S.size){|j|
		if S[0,i]+S[j..-1]==T
			puts :YES
			exit
		end
	}
}
puts :NO


def run
  t = "CODEFORCES".split(//)
  i = 0

  $stdin.gets.split(//).each do |c|
    i += 1 if c == t[i]
  end

  puts i == t.length ? "YES" : "NO"
end

run if $0 == __FILE__


s = gets.chomp.chars
t = 'CODEFORCES'
i = 0
s.each do |x|
  i += 1 if x == t[i]
end
puts i == 10 ? 'YES' : 'NO'


a = gets
b = gets

b, a = a,b if a > b

count = 1
pow = a * a

while pow < b do
  pow *= a
  count += 1
end

if pow == b
    puts "YES"
    puts count
else
    puts "NO"
end


n, k = gets.chomp.split().map { |e|  e.to_i }
LIMIT = Math.sqrt(n)
factors = []
for i in 2 .. LIMIT
	while n % i == 0 && k != 1
		factors << i
		n /= i
		k -= 1
	end
end

if k != 1
	puts "-1"
else
	for i in factors
		print(i," ")
	end
	puts n
end


n,k=gets.split.map(&:to_i)
a=n.prime_division.flat_map{|n,p|[n]*p}
puts a.size<k ? -1 : (a[0,k-1]+[a[k-1..-1].reduce(:*)])*' '


s=gets.chomp()
a=s.uniq
if a.length%2==0
    puts "Ignore him"
else
    puts "Chat with her"
end


puts get.chomp.length.even? ? "CHAT WITH HER!" : "IGNORE HIM"


#$stdin=File.open('input.txt')
#$stdout=File.open('output.txt', 'w')
s=gets.to_s
if a.size%2==0 then
puts "CHAT WITH HER!"
else
puts "IGNORE HIM!"
end


p(gets.chomp.to_a.uniq.size.odd? ? 'IGNORE HIM!' : 'CHAT WITH HER!')


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

sum = 0
i = 0
while i < w do
    i = i+1
    sum = sum+(i*k)
end

s = sum - n

if s < 0
    puts o
else
    puts s
end


k, n, w = STDIN.read.split
total = (w + 1) * k /2
lack = total - n
print lack


Chord = ["C","C#","D","D#","E","F","F#","G","G#","A","B","H"]
ary = gets.split

if (Chord.index(ary[0]) - Chord.index(ary[1])).abs == 3 || (Chord.index(ary[0]) - Chord.index(ary[1])).abs == 9
  if (Chord.index(ary[0]) - Chord.index(ary[2])).abs == 4 || (Chord.index(ary[0]) - Chord.index(ary[2])).abs == 8
   puts "minor"
  else
    puts "strange"
  end
elsif (Chord.index(ary[0]) - Chord.index(ary[1])).abs == 4 || (Chord.index(ary[0]) - Chord.index(ary[1])).abs == 8
  if (Chord.index(ary[0]) - Chord.index(ary[2])).abs == 3 || (Chord.index(ary[0]) - Chord.index(ary[2])).abs == 9
    puts "major"
  else
    puts "strange"
  end
else
  puts "strange"
end


notes = %W(C C# D D# E F F# G G# A B H)

def check(chord)
  chord.permutation do |c|
    diff1 = c[1] - c[0]
    diff1 += 12 if diff1 < 0
    diff2 = c[2] - c[1]
    diff2 += 12 if diff1 < 0
    if diff1 == 4 && diff2 == 3
      return "major"
    elsif diff1 == 3 && diff2 == 4
      return "minor"
    end
  end
  return "strange"
end

puts check(gets.strip.split.map{|s| notes.index(s)})


n = gets.chomp.to_i
if n%4==0 or n%7==0 or n%44==0 or n%47==0 or n%74==0 or n%77==0
    puts "YES"
else
    puts "NO"


require 'byebug'
n = gets.chomp.to_i
lucky_numbers = [4, 7, 44, 47, 74, 77, 444, 474, 477, 744, 747, 777]
res = 'NO'
lucky_numbers.each do |ln|
  if n % ln == 0
    res = 'YES'
    break
  end
end
puts res


n, m, a = STDIN.gets.split(" ")
puts "(n, m, a) = (#{n}, #{m}, #{a})"

x = Integer(n) * Integer(n)

puts Integer(x % a) + 1


n = gets.chomp.to_i
m = gets.chomp.to_i
a = gets.chomp.to_i
puts (n + a - 1) / a * (m + a - 1) / a


n = gets.chomp.to_f
m = gets.chomp.to_f
a = gets.chomp.to_f

x = (n/a).ceil
y = (m/a).ceil

puts x*y


width = gets.to_f
height = gets.to_f
block_size = gets.to_f

widthNumber = (width/block_size).ceil
heightNumber = (height/block_size).ceil

puts widthNumber * heightNumber


n=gets.to_i
m=gets.to_i
a=gets.to_f

n =(n/a).ceil
m =(m/a).ceil

puts n*m


#!/usr/bin/env ruby
"""(c) gorlum0 [at] gmail.com"""
begin fin = open('A.in')
rescue fin = STDIN
end

def flagstones(n, m, a)
    (n/a).ceil * (m/a).ceil
end

for line in fin
    n, m, a = line.split.map(&:to_i)
    puts flagstones(n, m, a)
end


# http://codeforces.com/contest/592/problem/A
# A. PawnChess

cols = [].tap do |arr|
  8.times { arr << gets.chomp.chars }
end
b_idx = []
w_idx = []
rows = cols.transpose.reject do |row|
  b = row.rindex('B')
  w = row.rindex('W')
  if b && (w.nil? || (b > w))
    b_idx << 7 - b
  end

  b = row.index('B')
  w = row.index('W')
  if w && (b.nil? || (w > b))
    w_idx << w
  end
end

if !b_idx.empty? && (w_idx.empty? || b_idx.min < w_idx.min)
  puts 'B'
else
  puts 'A'
end


# http://codeforces.com/contest/592/problem/A
# A. PawnChess

cols = [].tap do |arr|
  8.times { arr << gets.chomp.chars }
end
rows = cols.transpose.reject do |row|
  row.include?('B') && row.include?('W')
end

b_indexes = rows.map { |row| row.rindex('B') }.compact.map { |idx| 7 - idx }
w_indexes = rows.map { |row| row.index('W') }.compact

if b_indexes.min < w_indexes.min
  puts 'B'
else
  puts 'A'
end


field = []
while line = gets
  field << line.chomp.split("")
end
field = field.transpose
stepA = []
stepB = []
field.each do |r|
  a = r.rindex("W")
  b = r.rindex("B")
  unless b.nil? then
    if a.nil? or (a < b) then
      stepB << 7 - b
    end
  end
  a = r.index("W")
  b = r.index("B")
  unless a.nil? then
    if b.nil? or (a < b) then
      stepA << 7 - a
    end
  end
end
if stepB.empty? or (stepA.min < stepB.min) then
  puts "A"
elsif stepA.empty? or (stepA.min > stepB.min) then
  puts "B"
end


a = Array.new(8)
for x in 0..7
    a[x] = gets
end
for x in 0..7
    for y in 0..7
        if a[7 - x][y] == 'B'
            print "B\n"
            exit
        end
        if a[7 - x][y] == 'W'
            print "A\n"
            exit
        end
    end
end


$stdin = File.open "input.txt"

def min(m, s)
	a = [0] * m
	a[0] = 1

	return -1 if s < 1

	ms = s - 1
	i = m - 1
	while ms > 0
		if i == 0
			if ms < 9
				a[i] += ms
				break
			else
				return -1
			end
		else
			a[i] = [9, ms].min
			ms -= a[i]
			i -= 1
		end
	end

	return a.join
end

def max(m, s)
	a = [0] * m

	return -1 if s < 1

	i = 0
	while s > 0
		if i == m - 1
			if s <= 9
				a[i] = s
				break
			else
				return -1
			end
		else
			a[i] = [9, s].min
			s -= a[i]
			i += 1
		end
	end

	return a.join
end

m, s = gets.split.map &:to_i
puts "#{min(m, s)} #{max(m, s)}"


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

def dp (m, mm, ss)
  return ss if mm == 1
  a = 0
  a = 1 if mm == m
  while ss - a > 9 * (mm - 1)
    a += 1
  end
  a * (10 ** (mm - 1)) + dp(m, mm - 1, ss - a)
end

if 9 * m < s || (m > 1 && s == 0)
  puts [-1, -1].join(" ")
else
  a = dp(m, m, s)
  puts [a, a.to_s.chars.sort.reverse.join("").to_i].join(" ")
end


def sum(n, num)
  total = 0
  n.times do |i|
    total += (num / 10 ** i) % 10
  end
  total
end
n, s = gets.chomp.split.map(&:to_i)
#p n = rand(1..5)
#p s = rand(0..n * 10)
mi = 10 ** (n - 1)

(10 ** n - 1).times do
  break if sum(n, mi) == s
  if mi >= 10 ** n - 1
    mi = -1
    break
  end
  mi += 1
end
mx = 10 ** n - 1
(10 ** n - 1).times do
  break if sum(n, mx) == s
  if mx <= mi && mi != -1
    mx = mi
    break
  elsif mx <= 10 ** (n - 1)
    mx = -1
    break
  end
  mx -= 1
end
puts [mi, mx].join(" ")


m,s=gets.split(" ").map{|x| x.to_i}
if 9*m<s
  puts "-1 -1"
else
  if s==0
    if m==1
      puts "0 0"
    else
      puts "-1 -1"
    end
    exit
  end
  d=[]
  t=s/9
  t.times{d.push(9)}
  if t*9<s
    d.push(s%9)
    (m-t-1).times{d.push(0)}
  else
    (m-t).times{d.push(0)}
  end
  maxValue=d.sort().reverse().join("")
  minValue=""
  d.clear()
  for i in 1..9
    if i+(m-1)*9>=s
      d.push(i)
      minValue=i.to_s
      s-=i
      break
    end
  end
  minValue+=([0]*(m-s/9-2)).join("")+(s%9).to_s+([9]*(s/9)).join("")
  puts "#{minValue} #{maxValue}"
end


#!/usr/bin/ruby

def readNextValues
  return gets.chomp.split.collect {|x| x.to_i }
end

a, c = readNextValues
as = a.to_s(3)
cs = c.to_s(3)
result = ""

n = as.size - cs.size
if n > 0
  result = as[0..n-1].map{|x| 3-x.to_i}.join('')
  as = as[n..-1]
elsif n < 0
  n = n.abs
  result = cs[0..n-1]
  cs = cs[n..-1]
end
for i in 0..as.size-1
  result << ((cs[i].to_i - as[i].to_i) % 3).to_s
end

puts result.to_i(3)


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

a = a.to_s(3)
c = c.to_s(3)

if a.size > c.size
  c = "0" * (a.size - c.size) + c
elsif a.size < c.size
  a = "0" * (c.size - a.size) + a
end

b = ""
a.size.times do |i|
  b << ((c[i] - a[i]) % 3).to_s
end

p b.to_i(3)


a, c = gets.split.map{ |x|
 (x.to_i.to_s(3).reverse+"0"*10).split("").map(&:to_i)
}
[a.length, c.length].min.times{ |i|
 c[i] = (c[i]-a[i]+3) % 3
}
p c.join("").reverse.to_i(3)


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

x,y,z = lscan

pl = x + z > y
mi = y + z > x

if pl && mi
  puts "?"
elsif pl
  puts "+"
elsif mi
  puts "-"
else
  puts "0"
end


n = gets.to_i
a = gets.to_i
if a % 2 == 1
	puts (a + 1) / 2
else
	puts (n - a) / 2 + 1
end


inp = gets
if inp.index('0000000') || inp.index('1111111'))
  puts "YES"
else
  puts "NO"
end


input = gets.chomp
if input == (input[0].downcase + input.reverse.chop.reverse.upcase)
  puts input.capitalize
elsif input.upcase == input ||
  puts input.downcase
else
  puts input
end


given_word = ARGV[0]

def upcase_match?(word)
  (word <=> word.upcase) == 0
end

if upcase_match?(given_word) || (!upcase_match?(given_word[0]) && upcase_match?(given_word[1..-1]))
  puts "#{given_word[0].upcase}#{given_word[1..-1].downcase}"
else
  puts given_word
end


s=gets.chomp;
s1=s.capitalize.swapcase;s2=s1.uppercase;
if (s==s1)or(s==s2)
  puts s.swapcase
else
  puts s
end


s = ARGF.read.strip

if s[1, -1] == s[1, -1].upcase
	puts s.swapcase
else
	puts s
end


n = $stdin.gets.to_i
s = $stdin.gets.chomp

if s.gsub(/[47]/, '') == ""
  first = 0
  second = 0

  (n / 2).times do |i|
    first += s[i,1].to_i
    second += s[-i,1].to_i
  end

  if first == second
    puts "YES"
  else
    puts "NO"
else
  puts "NO"
end


n = $stdin.gets.to_i
s = $stdin.gets.chomp

if s.gsub(/[47]/, '') == ""
  first  = 0
  second = 0

  (n / 2).times do |i|
    first += s[i,1].to_i
  end

  (n / 2).times do |i|
    second += s[~i,1].to_i
  end

  p first, second

  if first == second
    puts "YES"
  else
    puts "NO"
  end
else
  puts "NO"
end


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

if s.uniq.sort == [4,7]
  if s.each_slice(n/2).map{|x| x.inject(:+)}.uniq.size == 1
    puts "YES"
  else
    puts "NO"
  end
else
  puts "NO"
end


a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
cnt =0
(0..a.length - 1).each{|i|
    if a[i] == b[i]
        p "YES"
        break
    else
        cnt += 1
    end
}
if cnt == a.length
    p "NO"
end


v=1000000000;
input=gets.split.map(&:to_i);
a=input[0];b=input[1];m=input[2];
if a==v then
        a-=1
end
if b==v then
        b-=1
end
v%=m;
if b>=m || v==0 then
        puts 2
else
        el=[a,m].min
        j=v
        for i in 1..el
                if (j%m)+b>=m then

                else
                        printf "1 %09d\n",i
                        exit
                end
                j+=v
                if j>=m then
                        j-=m
                end
        end
        puts 2
end


n = gets.to_i
s = gets.chomp

l = s[0...n].split(//).map(&:to_i)
r = s[n...2*n].split(//).map(&:to_i)

lh, rh = true, true
for i in 0...n do
  lh = lh && l[i] < r[i]
  rh = rh && l[i] > r[i]
end

puts(if lh || rh then "YES" else "NO" end)


import sys
import math
import itertools

def to_numeric(c, r):
  ret = 0

  array = [i for i in c]
  array.reverse()
  for i in range(len(array)):
    tmp = 0
    if array[i].isalpha():
      tmp = ord(array[i]) - ord('A') + 10
    else:
      tmp = int(array[i])

    ret += tmp * r ** i

  return ret

if __name__ == '__main__':
  print to_numeric("2A", 2)
  """
  s = raw_input().rstrip()
  a = s.split(":")[0]
  b = s.split(":")[1]

  r = 3
  radixes = []
  for i in range(2, 61):
    a2 = to_numeric(a, i)
    b2 = to_numeric(b, i)

    if a2 < 24 and b2 < 60:
      radixes.append(i)

  if len(radixes) == 0:
    print 0
  else:
    if len(radixes) == 59:
      print -1
    else:
      print " ".join([str(i) for i in radixes])
  """


# http://codeforces.ru/problemset/problem/160/B

def run(input = STDIN.read)
  lines = input.split("\n")

  n       = lines[0].to_i
  ticket  = lines[1].split("").map(&:to_i)

  first   = ticket.take(ticket.size / 2).sort
  second  = ticket.drop(ticket.size / 2).sort

  if (0..n-1).all? { |i| first[i] < second[i] } or
     (0..n-1).all? { |i| first[i] > second[i] } then
    return 'YES'
  else
    return 'NO'
  end
end

begin
  require './testing.rb'
rescue LoadError
  run
end
__END__
2
2421
=
YES
---
2
0135
=
YES
---
2
3754
=
NO


t,s,x = gets.chomp.split

t = t.to_i
x = x.to_i
s = s.to_i

if x<t
  puts "NO"
elsif (x-t)%s==0 or (x-t-1)%s==0
  puts "YES"
else
  puts "NO"
end


t, s, x = gets.split.map(&:to_i)

time = t
if time == x
  puts 'YES'
  exit
end

until time > x
  time += s
  if time == x || time + 1 == x
    puts 'YES'
    exit
  end
end

puts 'NO'


#!/usr/bin/env ruby

t, s, x = ARGF.gets.chomp.split(" ").map { |s| s.to_i }

if t == x
    puts "YES"
    exit
end

i = 1
while (barktime = t + i * s) <= x + 1 do
    if x == barktime or x == barktime + 1
        puts "YES"
        exit
    end
    i += 1
end
puts "NO"


t, s, x = gets.chomp.split(' ').map(&:to_i)

num = t
oneTime = false
first = true

until num >= x
	num += s if first
	num += (s - 1) if !oneTime && !first
	num += 1 if oneTime

	oneTime = !oneTime
	first = false

	#puts "Num : #{num}"
end

if num == x
	puts 'YES'
else
	puts 'NO'
end


#out = Number.new
c = 0
a = 0
t = 0
b = Array.new()
count = 0
#a = Fixnum.new()
#c = Fixnum.new()
out = 0
gets.scan(/\d+/).each do |str|
if t == 0 then #first num
	t = t + 1
	a = str.to_i
	#puts "a = " + a.to_s
else	#sec num = c
	c = str.to_i
	#puts "c = " + c.to_s
	while c != 0 and a != 0
		if c%3 >= a%3 then
			b[count] = c%3 - a%3
		else
			b[count] = c%3+3-a%3
		end
		#puts count.to_s + " " + a.to_s + " " + c.to_s + " " + b[count].to_s
		c = c/3
		a = a/3
		count = count + 1
	end
	if c == 0 then
		while a != 0
			b[count] = (a != 0 ? 3-a%3 : 0)
			a = a/3
			count = count + 1
		end
	else
		while c!= 0
			b[count] = c%3
			c = c/3
			count = count+1
		end
	end
end
end
count = count - 1
n = 1
#p b
0.upto(count) do |i|
	out = out + n*b[i]
	#puts "i = " + i.to_s + "n = " + n.to_s + "out = " + out.to_s
	n = n*3
end
puts out
#p b


#!/usr/bin/ruby

def solution(n, arr)
  ans = 0
  m = arr[0]
  arr = arr.slice 1, arr.length
  while arr.any? {|a|a >= m}
    m += 1
    arr.sort!.reverse!
    arr[0]-=1
    ans += 1
  end
  puts ans
end

n = gets.to_i
arr = gets.split(' ')
solution n, arr
#
# solution 5, [5,1,11,2,8]
# solution 4, [1,8,8,8]
# solution 2, [7,6]


u = [0]*10000
a = gets.chomp.to_i
b = gets.chomp.split(' ')
print b


n = gets.to_i
a, *b = gets.split.map(&:to_i)
if a > b.max
  puts 0
else
  c = b.select{|x| x > a}.sort.reverse
  ans = 0
  while a <= c.max
    ans += 1
    a += 1
    c[0] -= 1
    c = c.sort.reverse if c[0] != c.max
  end
  puts ans
end


#include <cstdio>
#include <queue>

int main() {
  int tol = 0;
  scanf("%d", &tol);
  for (int o=0; o<tol; o++) {
    int n, x, first, ans = 0;
    std::priority_queue<int> q;
    scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
      scanf("%d", &x);

      if (i > 0) {
        q.push(x);
      } else {
        first = x;
      }
    }

    while (q.top() >= first) {
      int max = q.top();
      q.pop();
      q.push(max - 1);
      ++first, ++ans;
    }

    printf("%d\n", ans);
  }
  return 0;
}


#!/usr/bin/ruby

n = gets.to_i
x = 1
while x*2 <= n
  x*=2
end

puts 1 + n - x


a = gets.to_i
puts a.odd? && a != 1 ? 2 : 1


n = gets.to_i
a = Array.new(n)

for i in 0..n-1
 a[i] = gets.chomp.to_s
end

counter = 0
for i in 0..n-1
 if a[i] == a[i+1]
     counter+=1
 end
end
puts counter


stones = gets.chomp.to_i
colors = gets.chomp
colors = colors.split("")

for i in (0..stones-1)
	if i < stones - 1
		while colors[i] == colors[i+1] && colors.size > 1
			colors.delete_at(i+1)
		end
	end
end

print stones - colors.size


#!/usr/bin/ruby

funds, start, factor = STDIN.readline.split.collect {|s| s.to_i }

count = 0
length = start.to_s.length

while true
	limit = 10**(length)
	max_steps = limit-start

	if max_steps*length*factor >= funds
		count += funds/(length*factor)
		break
	end

	count += max_steps
	funds -= max_steps*length*factor
	length += 1
	start = limit-1
end

puts count


w,m,k=gets.split.map(&:to_i)
a=Math.log10(m).to_i+1
v=l=0
while k*(c=(b=10**a-1)-m+1)+v <=w do
  v+=k*c
  l+=c
  m=b+1
  a+=1
end
l+=(w-v)/(a*k)
p l


a=gets.chomp.split(" ")
s=a[1].to_i
q=a[2].to_i
t=a[0].to_i
ans=(Math.log(t/s,q)).ceil
puts "#{ans}"


#!/usr/bin/ruby

#f = File.open('input', 'r')

while line = f.gets do
  line.chomp!
  tt, ss, qq = line.scan(/\d+/).map(&:to_i)
  tt = tt.to_i
  cnt = 1
  while qq * ss < tt do
    cnt += 1
    ss = qq * ss
  end
  puts cnt
end

#f.close


a=(1..110).to_a
a=a.join
puts "#{a[gets.chomp.to_i-1]}"


#!/usr/bin/ruby
N,M=gets.to_i
p N==1 ? 1 : M-1<N-M ? M+1 : M-1


#!/usr/bin/ruby

def solution(n, m)
  return puts 1 if n==1
  return puts m-1 if n/2 < m
  puts m+1
end

n, m = gets.split(' ').each{|s|s.to_i}
solution n, m

# solution 3,1
# solution 4,3
# solution 3,2
# solution 3,1
# solution 3,3
# solution 2,1
# solution 2,2
#
# #
# # solution 4, [75, 150, 75, 50]
# # solution 3, [100, 150, 250]


n,m = gets.split(' ').map(&:to_f)
s = n/2
if n==1
  puts "1"
  exit
end
if s >=m
  m += 1
  puts m
  exit
end
m -= 1
puts m


# coding: utf-8

n, m = gets.split.map(&:to_i)
# m -> number picked by Misha
# c -> any random number range [1, n], uniform probability
# n -> n number range
# a -> number picked by Andrew
# |c - a| < |c - m|
# if distances are same, Misha wins
# Make highest probability for Andrew's winning

if n == 1
    puts "1" # no other possible case
else
    mid = n/2 + n%2
    if m < mid
        puts m + 1
    else
        puts m - 1
    end
end

# ex)
# 1 2 3 4 5 6 7 8
#   M A
# M wins at: 1, 2
# A wins at: 3, 4, 5, 6, 7, 9
# 1 2 3 4 5 6 7 8
#   M         A
# M wins at: 1, 2, 3, 4
# A wins at: 5, 6, 7, 8
# 1 2 3 4 5 6 7 8
#   M           A
# M wins at: 1, 2, 3, 4, 5
# A wins at: 6, 7, 8
# 1 2 3 4 5
#     A   M
# M wins: 4, 5
# A wins: 1, 2, 3
# 1 2 3 4 5
#       A M
# M wins: 5
# A wins: 1, 2, 3, 4


import sys
val=map(int,raw_input().split(" "))
val=sorted(val)
m=max(val[0]+val[1]-val[2],val[1]+val[2]-val[3])
print ["IMPOSSIBLE","SEGMENT","TRIANGLE"][(m>=0)+(m>0)]


# -*- coding: utf-8 -*-
def triangle(a, b, c)
  if a + b > c then
    return 1
    elsif a + b == c then
    return 0
    else
    return -1
  end
end

inputs = gets.split.map(&:to_i)
inputs.sort!
ans = -1
for i in 0...4 do
  for j in i+1...4 do
    for k in j+1...4 do
      work = triangle(inputs[i], inputs[j], inputs[k])
      if work > ans then ans = work end
      #print inputs[i], inputs[j], inputs[k] , " " , work
      puts
    end
  end
end
case ans
  when 1
  p "TRIANGLE"
  when  0
  p "SEGMENT"
  when -1
  p "IMPOSSIBLE"
end


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

Primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97].freeze

def get_abn(io)
  line = io.gets
  parts = line.split(' ')
  return parts[0], parts[1], parts[2]
end

# greatest common divisor
def gcd(x, y)
  x_divisor = divisor x
  y_divisor = divisor y
  common_divisor = x_divisor & y_divisor
  return common_divisor.max
end

def divisor(x)
  divisor = [1]
  Primes.each do |p|
    divisor << p if x%p == 0
  end
  unless Primes.include? x then
    divisor << x
  end
  return divisor
end

a, b, n = get_abn($stdin)
while true
  taken = gcd(a, n)
  if taken > n then
    puts 1
    break
  else
    n -= taken
  end

  taken = gcd(b, n)
  if taken > n then
    puts 0
    break
  else
    n -= taken
  end
end


include Math

def get_abn(io)
  line = io.gets
  parts = line.split(' ')
  return (parts[0], parts[1], parts[2])
end

a, b, n = get_abn($stdin)
print a,b,n


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

def get_abn(io)
  io.gets.split(' ').map! {|p| p.to_i}
end

# greatest common divisor
def gcd(x, y)
  m, n = [x, y].sort
  until n==0 do
    m, n = [n, m%n]
  end
  return m
end

if $0 == __FILE__ then
  a, b, n = get_abn($stdin)
  cnt = 1
  [a, b].cycle do |x|
    taken = gcd(x, n)
    if taken > n then
      puts cnt
      break
    end
    cnt += 1
  end
end


x,y,z=gets.chomp.split()
x = x.to_i
y = y.to_i
z = z.to_i
point = 0
while true
	if x>y
		if point==0
			z-=x.gcd(z)
			if z<0
				puts 0
				break
			end
			point+=1
		end
		if point==1
			z-=y.gcd(z)
			if z<0
				puts 1
				break
			end
			point-=1
		end
	end
end


n = gets.chomp.to_i;
ans = 1;
n.times{|i| ans += 6*(i+1)}
p ans


n = gets.strip.to_i
puts(1) and return if n == 0
puts(1 + (2*6 + 6*(n-1)*n)/2)


def dist(x1,y1,x2,y2)
  x1-=x2
  y1-=y2
  x1=x1>0?x1:-x1
  y1=y1>0?y1:-y1
  return x1+y1
end

posx=[]
posy=[]
delta=[]
sm=[]
args=gets.split.collect{|s|s.to_i}
x0,y0,ax,ay,bx,by=args[0],args[1],args[2],args[3],args[4],args[5]
args=gets.split.collect{|s|s.to_i}
xs,ys,t=args[0],args[1],args[2]
posx.push(x0)
posy.push(y0)
delta.push(0)
sm.push(0)
while delta[-1]<=t
  x0,y0=ax*posx[-1]+bx,ay*posy[-1]+by
  delta.push(dist(x0,y0,posx[-1],posy[-1]))
  sm.push(delta[-1]+sm[-1])
  posx.push(x0)
  posy.push(y0)
end

ma=0
l=posx.length
for i in 0..l-1
  king=dist(xs,ys,posx[i],posy[i])
  for j in 0..i
    miles=(sm[i]-sm[j])+king
    cnt=i-j+1
    if miles<=t&&cnt>ma
      ma=cnt
    end
    miles+=(sm[i]-sm[j])
    for k in i+1..l-1
      miles+=delta[k]
      if miles<=t
        cnt+=1
        if(cnt>ma)
          ma=cnt
        end
      else
        break
      end
    end
  end
end
puts ma


def dist(x1,y1,x2,y2)
  x1-=x2
  y1-=y2
  x1=x1>0?x1:-x1
  y1=y1>0?y1:-y1
  return x1+y1
end

posx=[]
posy=[]
delta=[]
sm=[]
args=gets.split.collect{|s|s.to_i}
x0,y0,ax,ay,bx,by=args[0],args[1],args[2],args[3],args[4],args[5]
args=gets.split.collect{|s|s.to_i}
xs,ys,t=args[0],args[1],args[2]
posx.push(x0)
posy.push(y0)
delta.push(0)
sm.push(0)
while delta[-1]<=t+x0+y0+xs+ys
  x0,y0=ax*posx[-1]+bx,ay*posy[-1]+by
  delta.push(dist(x0,y0,posx[-1],posy[-1]))
  sm.push(delta[-1]+sm[-1])
  posx.push(x0)
  posy.push(y0)
end

ma=0
l=posx.length
for i in 0..l-1
  king=dist(xs,ys,posx[i],posy[i])
  for j in 0..i
    miles=(sm[i]-sm[j])+king
    cnt=i-j+1
    if miles<=t&&cnt>ma
      ma=cnt
    end
    miles+=(sm[i]-sm[j])
    for k in i+1..l-1
      miles+=delta[k]
      if miles<=t
        cnt+=1
        if(cnt>ma)
          ma=cnt
        end
      else
        break
      end
    end
  end
end
puts ma


n = gets.to_i
k = gets.to_i
a = gets.to_i
b = gets.to_i
o = n
cnt = 0
until n%k == 0 || n==1 do
  n -= 1
  cnt += a
end
until n%k != 0 || n==1 do
  n = n/k
  cnt += b
end
cnt += a*(n-1)
puts [cnt, a*(o-1)].min


n = gets.to_i
k = gets.to_i
a = gets.to_i
b, ans = gets.to_i, 0
until n < k || n == 1 || k == 1
  m = n%k
  q = n/k
  n -= m
  ans += a*m
  ans += [a*(n-q), b].min
  n -= n-q
end
ans += a * (n-1)
put ans


def maxdiv(a,x)
	return -1 if a < x
	w = a.divmod(x)
	return x*w[0]
end

as,bs,xs,ys = gets.chomp.split(/ /)
a = as.to_i
b = bs.to_i
x = xs.to_i
y = ys.to_i
a = maxdiv(a,x)
b = maxdiv(b,y)
if a < 0 || b < 0
	puts "0 0"
else
	ax = a / x
	by = b / y
	wk = [ax, by].min
	puts sprintf("%d %d", x*wk, y*wk)
end


#!/usr/bin/env ruby
#codeforces.com/problemset/problem/16/C

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

a = input[0]
b = input[1]
x = input[2]
y = input[3]

def gcd(a1, b1)
  b1 == 0 ? a1 : gcd(b1, a1.modulo(b1))
end

if x>a and y > b
	gcdd = gcd(x,y)
	x = x/gcdd
	y = y/gcdd
end

multiply = [a/x, b/y].min

puts (x*multiply).to_s + " " +(y*multiply).to_s


a,b,x,y=gets.split.map(&:to_i)
if a.to_f/b >= x.to_f/y
	na = b * x/y
	nb = na * y/x
else
	nb = a * y/x
	na = nb * x/y
end

print(na, " ", nb, "\n")


a=gets.split.map(&:to_i)
if a.count(a.min)>=a.min
    puts "YES"
else
    puts "NO"
end


n, k = gets.split(" ").map{|n| n.to_i }
a = gets.split(" ").map{|n| n.to_i }
count = 0
all_solved = true
a.each {|n|
  count += 1 if n <= k
  all_solved = false
  break
}
if all_solved
  puts count
else
  a.reverse.each {|n|
    count += 1 if n <= k
    break
  }
end


def is_consonant(c)
  c =~ /[^aeiou]/
end

def is_bertlanese(word)
  next_must_be_vowel = false

  word.chars.each do |c|

    return false if next_must_be_vowel && is_consonant(c)

    next_must_be_vowel =
      c != ?a &&
      c != ?e &&
      c != ?i &&
      c != ?o &&
      c != ?u &&
      c != ?n
  end

  !next_must_be_vowel
end

def solution
  word = read_string
  puts is_bertlanese(word) ? "YES" : "NO"
end

solution if __FILE__ == $0

def rest_int
  gets.to_i
end

def read_ints
  gets.split.map &:to_i
end

def read_string
  gets.chomp
end


s = gets.chomp.downcase
puts (s =~ /[^aeioun][^aeiou]/ || s =~ /^[aeiou]/ || s=~ /[^aeioun]$/) ? "NO" : "YES"


#!/usr/bin/ruby

def isvowel(x)
  if x == 'a'||x=='e'||x=='i'||x=='o'||x=='u'
    return true
  end

end
name = gets().chomp()
flag = 0
for i in 0...name.length

 #puts(name[i])

  if(i == name.length-1)

      if(name[i] == 'n')
        flag = 0
      elsif (isvowel(name[i]))
        flag = 0
      else
        flag = 1
        break
      end

  elsif(!isvowel(i))

    if(name[i] == 'n')
      flag = 0

    else
      if(!isvowel(name[i+1]))
        flag = 1
        break
      end

    end

  end

end

if(flag == 0)
  puts("YES")
else
  puts("NO")
end


def run
  x, y, m = $stdin.gets.split.map(&:to_i)

  if x <= 0 and y <= 0 and m > 0
    puts "-1"
  else
    cnt = 0
    while [x, y].max < m
      x > y ? y += x : x += y
      cnt += 1
    end

    puts cnt
  end
end

run if $0 == __FILE__


a, b, m = gets.split(' ').map {|i| i.to_i}
ans = 0
while a < m && b < m do
  a, b = b, a if a > b
  if b <= 0 then
    ans = -1
    break
  end
  delta = (b - a + b - 1) / b
  ans += delta
  a += b * delta
end
p ans


n = gets.to_i

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

d = 1
s = 0
while s < n do
    s += days[d-1]
    d = d < 7 ? d + 1 : 1
end

d = d != 0 ? d - 1 : 7

puts d


pages = STDIN.gets.chomp.to_i
read = STDIN.gets.chomp.split(' ').map(&:to_i)
wol = pages-1

red = 0
week = 1
while true
  read.each_with_index do |pa,i|
    red += pa
    if red >= pages
      p i+1
      exit
    end
    if red >= wol
      p i+1
      exit
    end
  end
  week += 1
end


pages = gets
reading_speed = gets

#pages = "20"
#reading_speed = "5 3 7 2 1 6 4"

pages = pages.to_i
reading_speed = reading_speed.split(' ').map { |speed| speed.to_i }

while pages > 0
  day = 0
  reading_speed.each do |speed|
    pages -= speed
    day += 1
    if pages < 0
      puts day
      break
    end
  end
end


#pages = gets
#reading_speed = gets

#pages = "20"
#reading_speed = "5 3 7 2 1 6 4"
#pages = "100"
#reading_speed = "15 20 20 15 10 30 45"

pages = pages.to_i
reading_speed = reading_speed.split(' ').map { |speed| speed.to_i }

while pages > 0
  day = 0
  reading_speed.each do |speed|
    pages -= speed
    day += 1
    if pages <= 0
      break
    end
  end
end

puts day.to_i


A = [gets, gets, gets, gets]
def rr(t)
  t = t.chars.sort {|x,y| y <=> x}.join
  if (t[0] == t[1] and t[1] == t[2] and t[2] == t[3]) then return true end
  if t[0] != t[1] then return true end
  return false
end
def solve()
  i, j = 0, 0
  while i != 3
    if rr(A[i][j] + A[i][j+1] + A[i+1][j] + A[i+1][j+1]) then return "YES" end
    j = j+1
    if j == 3 then i, j = i+1, 0 end
  end
  return "NO"
end
puts solve()


m = Array.new

4.times do
  m << gets.chomp
end

3.times do |y|
  3.times do |x|
    if (m[y][x] + m[y+1][x] + m[y][x+1] + m[y+1][x+1]).delete(".").length >= 3
      puts 'YES'
      exit 0
    end
  end
end

puts 'NO'


n = gets.to_i
if n == 1; p 2; exit; end;
if n == 3; p 1; exit; end;
if n == 4; p 2; exit; end;
p rand(3) + 1


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

newarr = []

prev = [-1]

arr.each do |x|
  if x != prev.max+1
    newarr.push prev
    prev = [x]
  else
    prev.push x
  end
end
newarr.push prev
count = 0

newarr.each do |z|
  len =  z.length
  next if len == 1

  if z.include?(1) or z.include?(1000)
    count += len-1
  else
    count += len-2
  end
end

#p newarr
puts count


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

newarr = []

prev = [-1]

arr.each do |x|
  if x != prev.max+1
    newarr.push prev
    prev = [x]
  else
    prev.push x
  end
end
newarr.push prev
countarr = [0]

newarr.each do |z|
  len =  z.length
  next if len == 1

  if z.include?(1) or z.include?(1000)
    count.push len-1
  else
    count.push len-2
  end
end

#p newarr
puts countarr.max


def getmask(number)
  number_length = number.to_s.length
  i = 1
  result = ''
  while i <= number_length do
    digit = number % 10
    number = number / 10
    result << digit.to_s if [4,7].include?(digit)
    i+=1
  end

  result.reverse.to_i
end

number = gets.to_i + 1
funny_number = gets.to_i
mask = -1

while mask != funny_number do
  number += 1
  mask = getmask number
end

puts number


class Integer
	def lucky?
		s = self.to_s
		s.length == s.count("47")
	end
	def unmask
		s = self.to_s
		s.split('').select { |char| %w{4 7}.include? char }.join.to_i
	end
end

a, b = gets.strip.split.map &:to_i

if a == 1; print 7; exit; end
n = a + 1

until b == n.unmask; n += 1; end

p n


#!/usr/bin/ruby
n,m=gets.split.map(&:to_i)
m%=n*(n+1)/2
1.step(m){|i|
	if m<i
		puts m
		exit
	end
	m-=i
}


n,  m = gets.split.map{|x| x.to_i}
while m >= i
  m= m - i
  if i == n
    i = 1
  else
   i = i + 1
  end
end

p m


n,a,b,p,q = gets.split.map{|a| Integer(a)}
diva = n/a
divb = n/b
divab = n/(a*b)
puts [diva*p + (divb-divab)*q,  divb*q + (diva-divab)*p].max


n=gets().to_i
line=gets().split(" ")
c100=0
c200=0
for i in 0...line
  c100+=1 if line[i].to_i==100
  c200+=1 if line[i].to_i==200
end
if c100%2==0 and c100>=2 or c200%2==0 and c100%2==0
  puts "YES"
else
  puts "NO"
end


n = gets.to_i
app = gets.split.map(&:to_i)
if n == 1
	print "NO"
else
	wt = Hash.new
	app.each do |a|
		wt[a] = (wt[a] || 0) + 1
	end

	if n % 2 != 0
		print (wt[100] || 1) % 2 == 0 ? "YES" : "NO"
	else
		a100 = (wt[100] || 1) % 2 == 0
		print a100 ? "YES" : "NO"
	end
end


k = gets.to_i
a = gets.split(" ").map(&:to_i).sort {|x,y| y <=> x }
sum, i = 0, 0
while sum < k
  sum += a[i]
  i += 1
end
puts i


#!/usr/bin/ruby
def read_int; gets.chomp.to_i; end
def read_ints; gets.chomp.split.map(&:to_i); end

k = read_int
a = read_ints
a.sort!.reverse!

if k == 0 do
  puts 0
  exit
end

count = 0
sum = 0

a.each do |i|
  count += 1
  sum += i
  if k <= sum
    puts count
    exit
  end
end

puts -1


k, arr = gets.to_i, gets.split.map { |x| x.to_i }.sort
m, sum = 0, 0
while sum < k do
  sum += arr.pop
  m += 1
end
puts m


def mygcd(a, b)
  while a != 0
    a, b = b, a if a < b
    a = a % b
  end
  return b
end
def mylcm(a, b)
  a * b / mygcd(a, b)
end
n = gets.chomp.to_i
n
a = n-1 if n-1 >= 1
b = n-2 if n-2 >= 1
puts mylcm(mylcm(n, a), b)


n = gets.chomp.to_i
if n <= 2
  puts n
elsif n % 6 == 0
  puts n * (n-1) * (n-4)
elsif n % 2 == 0
  puts n * (n-1) * (n-3)
else
  puts n * (n-1) * (n-2)
end


n = gets().to_i
res = case n
  when 1..2: n
  when 3: 6
  when n % 2 == 0 : ( n - 1 ) * ( n - 2 ) * ( n - 3 )
  else n * (n - 1) * (n - 2)
end

puts res


#!/usr/bin/env ruby
gets;p gets.split.map(&:to_i).each_cons(3).map{|x,y,z|z-x}.min


n = gets.to_i

k = false

while (n > 0)
    if (n % 2 == 1)
        k = true
    end
    n /= 10
end

if (k)
    puts 1
else
    puts 0
end


n = gets.to_i

if(n&1)
  puts 1
else
  puts 0
end


#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

d1, d2, d3 = gets.chomp.split.map(&:to_i)

puts [d1 * 2 + d2 * 2, d2 * 2 + d3 * 2, d3 * 2 + d1 * 2].min


a,b,c = gets.split.map(&:to_i)
puts [a,b].min + [a + b,c].min + [a,b].max


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

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

def read_to_array
  read_line.to_a
end

def read_numbers_to_array
  read_to_array.r
end

a, b, c = read_numbers_to_array
if a * 2 + b * 2 < c
  puts a + a + b + b
else
  if b * 2 + c * 2 < a
    puts b + b + c + c
  else
    if c * 2 + a * 2 < b
      puts a + a + c + c
    else
      puts a + b + c
    end
  end
end


d1, d2, d3 = gets.chomp.split(" ").map(&:to_i)
puts [2*(d1+d2), d1+d2+d3].min


a,b = gets.split(/\s+/).map(&:to_i)
mod = 1000000007

    maxa = b - 1

ans = 0
  if maxa > 0
    x1 = (b + 1 + b + maxa) * maxa / 2
    xn = (a*b + 1 + a*b + maxa) * maxa / 2
    ans = (x1 + xn) * a / 2
  end

p ans % mod


n, m = gets.split(' ').map &:to_i
if n == 0
  puts m*3
elsif m == 0
  puts n*2
else
  for i in 1...5e6
    if i%3 == 0 && m > 0
      m -= 1
    elsif i%2 == 0 && n > 0
      n -= 1
    end

    if n == 0 && m == 0
      puts i
      break
    end
  end
end


n,a=$<.read.split.map &:to_i
p [*a,2*a.reduce(:+)/n+1].max


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

as_max = as.max

e = as.inject(:+)
aw = as.map { |a| as_max - a }.inject(:+)
ans = -1
(as_max..100).each do |a|
  if aw + (a-as_max)*n > e
    ans = a
    break
  end
end
puts ans


n = gets.to_i
a = gets.split.map &:to_i
f = a.inject { |s, x| s + x }
(a.max..201).each do |k|
  s = a.inject { |s, x| s + k - x }
  if s > f
    puts k
    exit
  end
end


# http://codeforces.com/problemset/problem/7/A

rows = 0
cols = 0
cols_painted  = false

until (row = gets.chomp) == ''
  num_painted = row.count('B')
  if num_painted == 8
    rows += 1
  else
    cols += num_painted unless cols_painted
    cols_painted = true
  end
end

puts rows + cols


resl = 0
resr = 0
8.times do |i|
  line = gets.chomp
  if line =~ /^B+\Z/
    resl += 1
  else
    line.size.times do |i|
      resr += 1 if line[i]=="B"[0]
    end
  end
end
puts resl
puts resr
print resl+(resr/(8-resl))
#
#def fstlr mat
#  resl = 8
#  8.times do |i|
#    8.times do |j|
#      resl = if mat[i][j] == 0
#    end
#  end
#end
#
#mat = Array.new(8)
#fstl = true
#fstr = true
#8.times do |i|
#  mat[i]=Array.new(8)
#  line = gets
#  8.times do |j|
#    if line[j] == "B"[0]
#      mat[i][j] = 1
#    else
#      mat[i][j] = 0
#    end
#    if i==0
#      fstl = false if mat[0][j] != 1
#    end
#  end
#  fstr = false if mat[i][0] != 1
#end
#res = 0
#if fstl
#  res += 1
#else
#  mat[0].each do |x|
#    res += 1 if x == 1
#  end
#  if fstr
#  else
#    mat.each_index do |i|
#     res += 1 if mat[i][0] == 1
#    end
#    print res
#    exit(0)
#  end
#end
#
#end
#while line = gets
#  lines << line.chomp
#end
#if lines[0] !~ /^BBBBBBBB\Z/
#  lines[0].each do |c|
#    res += 1 if c=="B"[0]
#  end
#
#end


resl = 0
resr = 0
8.times do |i|
  line = gets.chomp
  if line =~ /^B+\Z/
    resl += 1
  else
    line.size.times do |i|
      resr += 1 if line[i]=="B"[0]
    end
  end
end
print resl+(resr/(8-resl))
#
#def fstlr mat
#  resl = 8
#  8.times do |i|
#    8.times do |j|
#      resl = if mat[i][j] == 0
#    end
#  end
#end
#
#mat = Array.new(8)
#fstl = true
#fstr = true
#8.times do |i|
#  mat[i]=Array.new(8)
#  line = gets
#  8.times do |j|
#    if line[j] == "B"[0]
#      mat[i][j] = 1
#    else
#      mat[i][j] = 0
#    end
#    if i==0
#      fstl = false if mat[0][j] != 1
#    end
#  end
#  fstr = false if mat[i][0] != 1
#end
#res = 0
#if fstl
#  res += 1
#else
#  mat[0].each do |x|
#    res += 1 if x == 1
#  end
#  if fstr
#  else
#    mat.each_index do |i|
#     res += 1 if mat[i][0] == 1
#    end
#    print res
#    exit(0)
#  end
#end
#
#end
#while line = gets
#  lines << line.chomp
#end
#if lines[0] !~ /^BBBBBBBB\Z/
#  lines[0].each do |c|
#    res += 1 if c=="B"[0]
#  end
#
#end


#!/usr/bin/env ruby

board = Array.new

li = 0
ARGF.each do |line|
	board[li] = line.strip
	li += 1
end

stroke_count = 0
for li in 0..7 do
	stroke_needed = true
	for ci in 0..7
		if board[li].to_s[ci] != 'B'
			stroke_needed = false
			break
		end
	end
	stroke_count += 1 if stroke_needed
end

for ci in 0..7 do
	stroke_needed = true
	for li in 0..7
		if board[li].to_s[ci] != 'B'
			stroke_needed = false
			break
		end
	end
	stroke_count += 1 if stroke_needed && stroke_count < 8
end

puts stroke_count


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

n/=2
if [n, n+1].include?(x) || [n, n+1].include?(y)
	puts "NO"
else
	puts "YES"
end


input = $stdin.readline.split(' ')
n = input[0].to_i; x=input[1].to_i; y=input[2].to_i

if n%2==0 then
  center = (n/2)
  if x==center or x==center+1 or y==center or y==center+1 then puts "NO"
  else puts "YES" end
else
  puts "NO"
end


n, x, y = gets.strip.split.map &:to_i
c /= 2
print (x == c || x == c + 1) && (y == c || y == c + 1)  ? :NO : :YES


p [:No, :Yes][gets.split.uniq.size == 1? 1:0]


gets.strip.scan(/[47]/).size.to_s =~ /^[47]*$/ ? puts "YES" : puts "NO"


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

counter = 0
s.each do |x|
 if x == "4" || x == "7"
     counter+=1
 end
end
if counter == "7" || counter == "4"
    puts "YES"
else
    puts "NO"
end


s = gets.chomp

ans = 0
ve = %w(a i u e o 0 2 4 6 8)
s.each_char do |c|
  ans += 1 if  ve.include?(c)
end

puts ans


puts 'Enter number'
code = gets.chomp
return unless code.length == 5
code = (code[0] + code[2] + code[4] + code[3] + code[1])
puts (code.to_i ** 5).to_s.chars.last(5).join


#!/usr/bin/env ruby

def pow(n)
  n*n
end

class Circle
  attr_accessor :x, :y, :r
  def initialize(x, y, r)
    @x = x
    @y = y
    @r = r
  end

  def include?(other)
    pow(@x-other.x) + pow(@y-other.y) <= pow(@r-other.r)
  end

  def collide?(other)
    (not include?(other)) and pow(@x-other.x) + pow(@y-other.y) < pow(@r+other.r)
  end
end

circles = []
2.times do
  x, y, r1, r2 = gets.split.map(&:to_i)
  circles << Circle.new(x, y, r1)
  circles << Circle.new(x, y, r2)
end

cnt = 0
circles.each_with_index do |c1, i|
  valid = true
  circles.each_with_index do |c2, j|
    next if i == j
    if c1.collide?(c2)
      valid = false
      break
    end
  end
  cnt += 1 if valid
end

p cnt


#!/usr/bin/ruby

x, y, small_r, big_r = STDIN.readline.split.map {|s| s.to_i }
a, b = [x, y, big_r], [x, y, small_r]
x, y, small_r, big_r = STDIN.readline.split.map {|s| s.to_i }
c, d = [x, y, big_r], [x, y, small_r]

def intersects(a, b)
	x0, y0, r0 = a
	x1, y1, r1 = b
	return false if includes(a, b) or includes(b, a)
	dd = (x1-x0)**2 + (y1-y0)**2
	dd < (r0 + r1)**2
end

def includes(a, b)
	x0, y0, r0 = a
	x1, y1, r1 = b
	return false if r1 >= r0
	dd = (x1-x0)**2 + (y1-y0)**2
	dd <= (r1 - r0)**2
end

count = 0
[a, b].each do |x|
	next if intersects(x, c) or intersects(x, d)
	next if includes(c, x) and includes(x, d)
	count += 1
end
[c, d].each do |x|
	next if intersects(x, a) or intersects(x, b)
	next if includes(a, x) and includes(x, b)
	count += 1
end
puts count


a,b,c,d=gets.split.map &:to_i
e,f,g,h=gets.split.map &:to_i
$dd=Math.sqrt((a-e)**2+(b-f)**2)
def pf(r,r1,r2)
  return 1 if r1+r<=$dd&&(r2+r)**2<=$dd
  return 1 if r1-r>=$dd&&r2-r>=$dd
  return 1 if r-r1>=$dd&&r-r2>=$dd
  return 0
end
p pf(c,g,h)+pf(d,g,h)+pf(g,c,d)+pf(h,c,d)


def solution(n1, n2, k1, k2, flag)
  return 1 if n1 == 0 && n2 == 0

  return 0 if n1 == 0 && flag
  return 0 if n2 == 0 && !flag

  if flag
    return @beging_with_soldiers[n1][n2] if @beging_with_soldiers[n1][n2]
  else
    return @beging_with_horses[n1][n2] if @beging_with_horses[n1][n2]
  end

  k = 1
  possibilities = 0
  if flag
    while k <= k1 && n1 - k >= 0 do
      possibilities += solution(n1-k, n2, k1, k2, !flag)
      k += 1
    end
  else
    while k <= k2 && n2 - k >= 0 do
      possibilities += solution(n1, n2-k, k1, k2, !flag)
      k += 1
    end
  end

  possibilities = possibilities % 100000000
  if flag
    @beging_with_soldiers[n1][n2] = possibilities
  else
    @beging_with_horses[n1][n2] = possibilities
  end
  possibilities
end

n1, n2, k1, k2 = gets.strip().split().map(&:to_i)

max_n = [n1, n2].max + 1
@beging_with_soldiers = Array.new(max_n) { Array.new(max_n) { false } }
@beging_with_horses = Array.new(max_n) { Array.new(max_n) { false } }

solution(n1, n2, k1, k2, true)
solution(n1, n2, k1, k2, false)
puts @beging_with_soldiers[n1][n2] + @beging_with_horses[n1][n2]


a, b = gets.split
puts a.to_i + b.inverse.to_i


require 'prime'

# TEMPLATE BEGIN
def read_vector
    gets.chomp.split(/\s+/)
end
def read_vector_int
    read_vector.map(&:to_i)
end
# TEMPLATE END

n = gets.to_i
val = 2
while n > 0 do
    val += 1
    next unless val.prime?
    rev = val.to_s.reverse.to_i
    next if rev == val
    next unless rev.prime?
    n -= 1
end
puts val


def prime x
  return false if x==1
  i=2
  while (i*i<=x)
    return false if x%i==0
    i+=1
  end
  true
end

n=gets.to_i
1.upto(100) do |x|
  y = x.to_s.reverse.to_i
  if x != y && prime(x) && prime(y)
    n-=1
    if n==0
      p x
      break
    end
  end
end


def prime x
  return false if x==1
  i=2
  while (i*i<=x)
    return false if x%i==0
    i+=1
  end
  true
end

n=gets.to_i
st = 1
if (n>8000)
  n -= 8000
  st = 758672
elsif (n>5000)
  n -= 5000
  st = 328814
end
st.upto(10000000) do |x|
  if prime x
    y = x.to_s.reverse.to_i
    if x != y && prime(y)
        n-=1
        if n==0
          p x
          break
        end
    end
  end
end


a=Array.new(gets.split.map(&:to_i))
b=Array.new(gets.split.map(&:to_i))
n=gets.to_i

float n1, n2

n1=(a[0]+a[1]+a[2])/5
n2=(b[0]+b[1]+b[2])/10

if(n1.ceil+n2.ceil<=n)
    puts "YES"
else
    puts "NO"


a=Array.new(gets.split.map(&:to_i))
b=Array.new(gets.split.map(&:to_i))
n=gets.to_i

float n1, n2

n1=(a[0]+a[1]+a[2])/5
n2=(b[0]+b[1]+b[2])/10

if(n1.ceil+n2.ceil<=n)
    puts "YES"
else
    puts "NO"
end


a=Array.new(gets.split.map(&:to_i))
b=Array.new(gets.split.map(&:to_i))
n=gets.to_i

n1,n2=Float(0)
n1=(a[0]+a[1]+a[2])/5
n2=(b[0]+b[1]+b[2])/10

if(n1.ceil+n2.ceil<=n)
    puts "YES"
else
    puts "NO"
end


def pow(a,k)
  n = 1000000007
  b = 1
  while (k!= 0) do
    if (k % 2 == 0 ) then
      k/=2
      a = (a*a) % n
    else
      k-=1
      b = (b*a) % n
    end
  end
  return b
end

n = gets.to_i
if (n == 0) then
  puts "1"
else
  ans = (pow(4,n)/2 + pow(2,n-1)) % 1000000007
  puts ans
end


a = gets.split.map(&:to_i)
p [a.min, 3].max.times.map{|i|
  a.map{|j| (j - i) / 3}.inject(i, &:+)
}.max


def main()
	rbg=gets.chomp.split(" ")
	r=rbg[0].to_i
	b=rbg[1].to_i
	g=rbg[2].to_i
	rbgarra=[r,b,g]
	min=rbgarra.min();
	ans=(r+b+g)/3;
	if (r-min)%3+(b-min)%3+(g-min)%3==3
		ans-=1
	end
	if (r-min)%3+(b-min)%3+(g-min)%3==4 && t==0
		ans-=1
	end
	puts ans
end
main()


while(1)
    r,g,b = STDIN.readline.split.map(&:to_i)
    val0 = r/3 + b/3 + g/3
    if r > 0 and g > 0 and b > 0
        val1 = (r-1)/3 + (b-1)/3 + (g-1)/3 + 1
    else
        val1 = 0
    end

    if r > 1 and g > 1 and b > 1
        val2 = (r-2)/3 + (b-2)/3 + (g-2)/3 +2
    else
        val2 = 0
    end

    puts [val0,val1,val2].max

end


#!/usr/bin/ruby
matches_count = gets.chomp.to_i

for	i in 1..matches_count
	players_count = gets.chomp.to_i
	players_heights = gets.chomp.split(" ").map { |x| x.to_i }

	differences_sum = 0
	differences_count = 0
	players_heights.each_with_index do |height1, index1|

		players_heights.each_with_index do |height2, index2|
			if (index2 <= index1)
				puts "Idx1 #{index1} vs Index2 #{index2}"
				next
			else
				puts "Incrementing differences_count : #{differences_count}"
				differences_count+=1
				differences_sum += (height2 - height1).abs
		   end
		end

	end

	puts differences_sum / players_heights.length

end


input = gets.chomp

def lucky_number(str)
	if str.index(/[^47]/)== nil
		return true
	else
		return false
	end
end

if lucky_number(input.length.to_s)
	puts "YES"
else
	puts "NO"
end


#!/usr/bin/env ruby

ng = 0
ok = 0
STDIN.gets.chomp.each_byte do |i|
  n = format("%c",i)
  if n != "4" and n != "7"
    ng += 1
  else
    ok += 1
  end
end

if (ok == 4 or ok == 7 or ng == 0) and ok > 0
  ptus "YES"
else
  puts "NO"
end


def solve(m, d)
  month = [0,31,28,31,30,31,30,31,31,30,31,30,31]
  days = month[m]
  tot = 8 - d + 28
  rest = days - tot
  puts rest
  return 4 if rest == -7
  return 6 if rest > 0
  return 5
end
m, d = gets.strip.split(" ").map(&:to_i)
puts solve(m, d)


a = gets.split('')
b = gets.split('')
ai=0
for i in 0...a.size
	if (a[i] == '+')
		ai++
	elsif (a[i] == '-')
		ai--
	end
end
bi=[]
bi[0]=0
for i in b
	if i =='+'
		for j in 0...bi.size
			bi[j]++
		end
	elsif i == '-'
		for j in 0 ... bi.size
			bi[j]--
		end
	elsif i == '?'
		v = bi.size
		for j in 0...v
			bi[j]++
			bi << (bi[j]-2)
		end
	end
end
count = 0.0
for i in bi
	count++ if i == ai
end
count /=bi.size
printf("%.9f", count)


def solution(s1, s2)
  x = s1.count("+") - s1.count("-")
  @memo = Array.new(10) { Array.new(20) }

  def solve(s2, pos, i, x)
    return 0 if i < 0 || i > 9

    if @memo[i][pos+10]
      return @memo[i][pos+10]
    end

    if s2.size == i
      return 0 if pos != x
      return 1
    end

    if s2[i] == "?"
      @memo[i][pos+10] = solve(s2, pos+1, i+1, x) + solve(s2, pos-1, i+1, x)
      return @memo[i][pos+10]
    end

    @memo[i][pos+10] = solve(s2, pos+( s2[i] == "+" ? 1 : -1), i+1, x)
    @memo[i][pos+10]
  end

  if s2.count("?") == 0
    return Float(solve(s2, 0, 0, x))
  else
    return solve(s2, 0, 0, x) / Float(s2.count("?")**2)
  end
end

s1 = gets.strip
s2 = gets.strip
puts solution(s1, s2)


s1 = gets.chomp
s2 = gets.chomp

n = (s1.count('+') - s1.count('-') - (s2.count('+') - s2.count('-'))).abs
m = s2.count('?')

if (n - m).odd? or n > m
	puts 0
else
	m = (m - n) / 2
	ans = Math.gamma(m + 1) / Math.gamma(n + 1) / Math.gamma(m - n + 1) / (2 ** m)
	puts '%.12f' % ans
end


a = gets.split.map(&:to_i)
o = gets.split
min = 100 ** 10000
a.permutation(a.size) do |x|
  o.permutation(o.size) do |y|
    u = x[0].send(y[0], x[1]).send(y[1], x[2]).send(y[2], x[3])
    if u < min
      min = u
    end
  end
end

puts min


s=gets.chomp
t=s.reverse
n=s.size.count{|i|s[i]!=t[i]}
puts n==2||(n==0&&s.size%2==1) ? :YES : :NO


#http://codeforces.com/problemset/problem/798/A

str = gets.chomp!

str_rev = str.reverse

difference = 0

ind = 0

str.size.times do
  if str[ind]!= str_rev[ind]
    difference +=1
  end
  ind +=1
end

puts difference <=2 ? "YES" : "NO"


s=gets.chomp.chars
r=s.reverse

c=0
while !s.empty?
	if s[0]!=r[0] then
		c+=1
	end
	s.shift
	r.shift
end
if c==2 then
	puts "YES"
else
	puts "NO"
end


# coding: utf-8

s = gets.chomp
l = s.length
r = s.reverse
diff = 0
for i in 0...l
    if s[i] != r[i]
        diff += 1
    end
end

if diff <= 2
    puts "YES"
else
    puts "NO"
end


n=gets.to_i.abs
i=1
k=0
while k<n
        k+=i
        i+=1
end
if (k-n)%2==0 then
        puts i-1
elsif k%2==0 then
        puts i+1
else
        puts i

end


x = STDIN.gets.chomp.to_i
n = 0
i = 0
while true
  n += i;
  if (n == x)
    puts i
    break;
  elsif(n > x && i % 2 != 0)
    puts i
    break;
  end
  i += 1
end


x=gets.to_i;i=j=0;while j<x||(j-x)&1>0:j+=i+=1 end;puts i


x=gets.to_i;i=j=0;while j<x||(j-x)&1>0 do j+=i+=1 end;puts i


N, X, Y = $stdin.gets.split.map(&:to_i)

$sep = Y / X

# n = N
# result = 0

def getResult(n)
  # temp = n>>1 * X < Y
  return X if n == 1
  if (n>>1) <= $sep
    return n * X
  end
  result = 0
  if n & 1 == 1
    minus = getResult(n-1)
    plus = getResult(n+1)
    result = X + minus < plus ? minus : plus
  else
    result = Y + getResult(n>>1)
  end
  result
end

puts getResult(N)


word = raw_input()
print (word[0].swapcase() + word[1:].lower()) if (word[1:] == "" or word[1:].isupper()) else word


def Flood(s)

 f = reverse.chop.reverse

 if (s[0] ==s[0].downcase) && f == f.upcase
   s =  s.downcase.capitalize
 elsif s == s.upcase
   s.downcase!
 end

  puts s

 end

Flood(gets)


puts gets[/^.[A-Z]*$] ? $_.swapcase : $_


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

a, b = gis

ans = a.gcd(b)

a.succ.upto(b) do |x|
    break if x == 1
    ans = ans.gcd(x)
end

puts ans


_a = gets.split.map { |e| e.to_i }

def Gcd(a, b)
  while(b != 0)
    r = b
    b = a % b
    a = r
  end
  return a
end

answer = _a[0]

(_a[0]+1..._a[1]).each do |i|
  temp = Gcd(i, answer)
  if temp == 1
    answer = 1
  end
end

puts answer


a = 0
b = 0

a = STDIN.gets.chomp.to_i
b = STDIN.gets.chomp.to_i

def findGCD(first, second)
	if second == 0
		return first
	else
		findGCD(second, first % second)
	end

	#return ? first : findGCD(second, first % second)
end

puts findGCD(a, b)


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

buf=gets.split(' ')
a,b=buf[0].to_i, buf[1].to_i
puts gcd(a,b)


# coding: utf-8

x, y, z = gets.split.map(&:to_i)
# x -> andrew: only green
# y -> dmitry: green and purple
# z -> michal: any type is good
a, b, c = gets.split.map(&:to_i)
# a -> green
# b -> purple
# c -> black
# happy YES unhappy NO

a -= x
bc = b + c - y

if a >= 0 && (a + bc) >= z
    puts "YES"
else
    puts "NO"
end


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

puts a >= x && a + b - x >= y && a + b + c - x - y >= z ? :YES : :NO


def solve(x,y,z,a,b,c)
  return 1 if (x >= a)
  return 1 if (x+y >= a+b)
  return 1 if (x+y+z >= a+b+c)
  return 0
end

x,y,z = gets.split.map(&:to_i)
a,b,c = gets.split.map(&:to_i)

puts ['YES', 'NO'][solve(x,y,z,a,b,c)]


# 1114 A
def solve(x, y, z, a, b, c)
	if (x > a || x + y > a + b)
		return 1
	else
		return 0
	end
end

x, y, z = gets.split.map(&:to_i)
a, b, c = gets.split.map(&:to_i)

ans = solve(x, y, z, a, b, c)
puts ['YES', 'NO'][ans]


x, t, a, b, da, db = gets.split.map(&:to_i)
(0..t).each do |i|
  (0..t).each do |j|
    z1 = a - i*da
    z2 = b - j*db
    if z1+z2 == x or z2 == x or z1 == x
      puts :YES
      exit
    end
  end
end
puts :NO


a=gets.chomp.split(" ")
t=0
0.upto(6) {|i| a[i]=a[i].to_i }
0.upto(a[1]-1) do |i|
0.upto(a[1]-1) do |j|
num=a[2]+a[3]-a[4]*i-a[5]*j
if num==a[0]
t=1
break
end
end
break if t==1
end
if t==1
puts "YES"
else
puts "NO"
end


x,t,a,b,da,db=gets.split.map(&:to_i);
tag=0;
for i in 0..t
	for j in 0..t
		if a-i*da+b-j*db==x or a-i*da==x or b-j*db==x or x==0
			tag=1;
			break;
		end
	end
end

if tag==1
	puts "YES"
else
	puts "NO"
end


x,t,a,b,da,db=gets.split.map(&:to_i)
yes=false
t.times do |i|
    t.times do |j|
    if a-da*i+b-db*j==x or a-da*i==x or b-db*j==x
        yes=true
    end
    end
end
if yes then puts "YES"
else puts "NO"
end


s = gets
if s == s[0] * s.length
    puts "0"
else
    puts "#{s.length - (s == s.reverse ? 1 : 0)}"
end


# -*- encoding: utf-8 -*-
def is_ok?(arr, t)
  arr.select{ |x| x > t }.length == 0 ? true : false
end

n, t, c = gets.chomp.split(" ").map(&:to_i)
prisoners = gets.chomp.split(" ").map(&:to_i)
ans = 0
i = 0

while i <= (n - c)
  arr = prisoners[i, c]
  if is_ok?(arr, t)
    ans += 1
    i += 1
  else
    i += c
  end
end

puts ans


keyboard = "qwertyuiop
            asdfghjkl;
            zxcvbnm,./"
dir = gets.chomp
gets.chomp.split("").each { |x| print dir == "R" ?
                      keyboard[keyboard.index(x) - 1] : keyboard[keyboard(x) + 1] }


a=gets
str="qwertyuiopasdfghjkl;zxcvbnm,./"
strk=gets
if a=="R\n"

 for i in 0...strk.size-1
  strk[i]=str[str.index(strk[i])-1]
 end
else
 for i in 0...str.size
  strk[i]=str[str.index(strk[i])+1]
 end
end
puts strk


s = "qwertyuiopasdfghjkl;zxcvbnm,./"
k = gets.chomp == "L" ? 1 : -1
puts gets.split('').map{|x|s[s.index(x)+k]}.join


n=gets.chop

i=1
flag=false
while i*(i+1)/2<=n.to_i and !flag do
  sum=i*(i+1)/2
  j=i+1
  while sum+j*(j+1)/2<=n.to_i and !flag do
    if sum+j*(j+1)/2==n.to_i then
      flag=true
      break
    end
    j+=1
  end
  i+=1
end

if flag then puts "YES"
else puts "NO"
end


n = gets.to_i

def check(x,co)
  k = 0
  z = 0
  while k<co do
    z+=1
    k=(2*x+z+1)*z/2
  end
  if k==co then 1 else 0 end
end

for i in (1..Math.sqrt(n).to_i) do
  n-=2*i
  if check(i,n)!=0
    puts 'YES'
    exit
  end
end

puts 'NO'


a=2*gets.chomp.to_i
upb=(Math.sqrt(a-1)).floor
up=(Math.sqrt(a/2)).ceil
lwb=1
trig=0
upb.downto(lwb) do |i|
lwb.upto(upb) do |j|
#puts "#{j} #{i} #{a} #{(i*(i+1))+j*(j+1)}"
if (i*(i+1))+j*(j+1)==a
puts "YES"
trig=1
break
end
break if j>up
break if ((i*(i+1))+j*(j+1))>a
end
break if trig==1
break if i<up-1
end

if trig ==0
puts "NO"
end


n = gets.to_i
x = 1
ans = false
y = 1
for i in 2..n-1 do
  for j in 2..n-x do
    if (y>n-x) then break end
    if (x+y==n) then ans = true end
    y += j
  end
  y = 1
  x += i
end
if (ans) then puts "YES"
  else puts "NO" end


n = gets.to_i
def gcd(a,b)
  a,b = b,a if b==0
  a%b==0 ? b : gcd(b, a%b)
end

if n==4
  puts 12
  exit
end
low = n-100>0 ? n-100:1
max = 0
n.downto(low) do |i|
  n.downto(low) do |j|
    n.downto(low) do |k|
      if gcd(i,j)==1  && gcd(j,k)==1 && gcd(i,k)==1
        max = i*j*k if i*j*k > max
      end
    end
  end
end
puts max


#!/usr/bin/ruby
class Integer
	def isqrt
		return 0 if self<=0
		return 1 if self<4 # 1
		x,y=0,self
		while x!=y&&x+1!=y
			x,y=y,(self/y+y)/2
		end
		x
	end
end

a,b=gets.split.map &:to_i
a=a.isqrt
puts [:Vladik,:Vladik,:Valera][a*-~a<=>b]


a,b=gets.split.map{|e| e.to_i}
d=2
ans="Valera"
while a>0 && b>0
	a-=(d-1)
	if a<=0 then

		break
	end
	b-=d
	if b<=0 then
		ans="Vladik"
		break
	end

	d+=2
end
puts ans


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

f,s = scan

v = 1
loop{
    f-=v
    p [f,s]
    if f<=0
        puts "Valera";break
    end
    v+=1
    s-=v
    if s<=0
        puts "Vladik";break
    end
    v+=1
}


def f(x,y,x1,y1)
	return (x-x1).abs+(y-y1).abs
end
x,y=gets.split.map{|e| e.to_i}
x1,y1=gets.split.map{|e| e.to_i}
x,y=(x-x1).abs,(y-y1).abs
x,y=[x,y].min,[x,y].max
ans=0
if x==0
	ans=6+y*2+f(x-1,y,-1,1)+f(x+1,y,1,1)
else
	ans=4+f(x,y,-1,1)+f(x,y,1,-1)
end
puts ans


a=[]
4.times do
	a<<gets.chomp.split('')
end
2.times do
	a[0].shift;a[1].shift;a[2].shift;a[3].shift
end
A,B,C,D=a[0].length,a[1].length,a[2].length,a[3].length
if 2*A<=B && 2*A<=C && 2*A<=D
	puts "A"
elsif 2*B<=A && 2*B<=C && 2*B<=D
	puts "B"
elsif 2*C<=B && 2*C<=B && 2*C<=D
	puts "C"
elsif 2*D<=B && 2*D<=C && 2*D<=A
	puts "D"
elsif A>=2*B && A>=2*C && A>=2*D
	puts "A"
elsif B>=2*A && B>=2*C && B>=2*D
	puts "B"
elsif C>=2*A && C>=2*B && C>=2*D
	puts "C"
elsif D>=2*A && D>=2*C && D>=2*A
	puts "D"
else
	puts "C"
end


descriptions = []
4.times do
  descriptions << gets.chomp
end

descriptions.sort_by!{ |x| x.length }
# puts descriptions.join "\n"
flag = false
if((descriptions[0].length-2)*2 <= (descriptions[1].length-2)) then
  flag = true
end
if (descriptions[2].length-2)*2 <= (descriptions[3].length-2) then
  if(flag)
    flag = false
    puts "C"
  else
    puts descriptions[3][0]
  end
else
  puts "C"
end
puts descriptions[0][0] if(flag)

# flag0 = true
# descriptions.each_with_index do |x,i|
#   flag1 = true
#   descriptions.each_with_index do |y,j|
#     next if i==j
#   end
# end


c=$<.map{|x| x.size-3}
c.each_with_index do |v,i|
	cc=c[i+1..-1]
	cc+=c[0..i-1] if i > 0
	if c[i] >= 2 * cc.max || 2 * c[i] <= cc.min
		puts (i + 'A'.ord).chr
		exit
	end
end
puts 'C'


# be name khoda ...
def read_ints; STDIN.gets.split.map(&:to_i); end
def isPrime(x)
  t = 2
  while (t*t <= x)
    if(x % t == 0)
      return false
    end
    t+=1;
  end
  return true
end
n = gets.to_i
res = '85'
if(n % 2 == 0)
  if(n == 2)
    res = '1'
  else
    res = '2'
  end
else
  if(isPrime(n))
    res = '1'
  elsif(isPrime(n-1))
    res = '2'
  else
    res = '3'
  end
end
puts res


def isPrime?(x)
  d = 2
  until (d*d >= x)
    if(x % d == 0)
       return false
    end
    d = d + 1
  end
  return true
end

n = gets.to_i

if (n==2)
  puts 1
elsif (n == 3)
  puts 1
elsif (n == 4)
  puts 2
elsif (n == 5)
  puts 1
elsif (n == 6)
  puts 2
elsif (n == 7)
  puts 1
elsif (n % 2 == 0)
  puts 2
elsif (isPrime?(n))
  puts 1
elsif (isPrime?(n-2))
  puts 2
else
  puts 3
end


#!/usr/bin/ruby
n=gets.chomp
puts [n,*(1...n.size).map{|i|
	(n[0,i].to_i-1).to_s+'9'*(n.size-i)
}].max_by{|e|[e.chars.map(&:to_i).reduce(:+),e.to_i]}


arow1 = gets.chomp
arow2 = gets.chomp
brow1 = gets.chomp
brow2 = gets.chomp
arow1.gsub!("X", "")
arow2.gsub!("X", "")
brow1.gsub!("X", "")
brow2.gsub!("X", "")

a = arow1 + arow2.reverse
b = brow1 + brow2.reverse

if (2 * a).index(b)
  puts "YES"
else
  puts "NO"
end


a = ["ABXC", "XBAC", "BXAC", "BCAX", "BCXA", "XCBA", "CXBA", "CABX", "CAXB", "XACB", "AXCB"]
#b = ["BAXC", "XABC", "AXBC", "ACBX", "ACXB", "XCAB", "CXAB", "CBAX", "CBXA", "XBCA", "BXCA"]
sa = readline.strip + readline.strip
sb = readline.strip + readline.strip
puts a.count(sa) == a.count(sb) ? "YES" : "NO"


n=gets.to_i
s=gets.to_s
p=s.chomp.split("")
x=1;
600.times do
  x*=10;
end
dp = Array.new(100,x)
dp[0]=0;
y=p.size.to_i
for i in 0..y-1 do
  tmp=0
  j=i
  while j<y&&tmp*10+p[j].to_i<n do
    tmp=tmp*10+p[j].to_i
    dp[j+1]=[dp[j+1],dp[i]*n+tmp].min
    if p[j].to_i == 0
      break
    end
    j+=1
  end
end
puts dp[y]


n = Integer(gets.chomp)
h = n / 2
str = gets.chomp
puts(str =~ /^(4|7)+$/ && str[0...h].reverse == str[h..-1] ? 'YES' : 'NO')


#!/usr/bin/ruby
# 1foot = 12inch = 36cm
n = STDIN.gets.to_i
f = n.to_i / 36
i = (n.to_i % 36) / 3
if ( n.to_i % 36 ) % 3 == 2 then
  i += 1
end

puts [f,i].join(' ')


N = 42
d = Array.new(N)
for i in 0...d.length
  d[i] = Array.new(N, 0)
end

for i in 0...N
  d[0][i] = 1
end

d[1][0] = 1
for n in 1...N
  for depth in 1..n
    for k in 0...n
        d[n][depth] += d[k][depth - 1] * d[n - k - 1][depth - 1]
    end
  end
end

n, depth = gets.split(/\s+/).map{|x| x.to_i}
p d[n][depth]


gets
gets.chomp.split.sort.map{|x|print x+ ' '}


gets
gets.chomp.split.map(&:to_i).sort.map{|x|print x+ ' '}


n, m = gets.split.map &:to_i
arr = gets.split.map &:to_i
puts m >= arr.max ? arr.size : arr.index(arr.max) + 1


a=gets.chomp.split(" ")
b=gets.chomp.split(" ")
ans=1
max=b[0].to_i
1.upto(a[0].to_i-1) do |i|
if b[i].to_i>=max
max=b[i].to_i
ans=i+1
end
end
puts "#{ans}"


n, m = gets.split.map(&:to_i)
puts gets.split.map(&:to_i).map.with_index{|x, i| [x, i + 1]}.sort.reverse[0][1]


n,m,a,b=gets.split.map(&:to_i)
max=[b/m-a/m+1,3].min
upper_r=(m+1-a%m)%m
lower_r=b%m
puts(
	if upper_r==0&&lower_r==0
		1
	elsif upper_r==0||lower_r==0||(upper_r+lower_r==m)
		[2,max].min
	else
		max
	end
)


st, fin = gets.split(' ').map(&:to_i)
num, diff = gets.split(' ').map(&:to_i)

sum_st = st
sum_fin = fin
(1...num).each do |i|
  l, r = i, num-1-i
  st += diff
  sum_st += st
  fin += diff
  sum_fin += fin
  if l == r || (r-l) == 1
    if (st-fin).abs <= diff
      if l == r
        puts sum_st + sum_fin - [st,fin].max
      else
        puts sum_st + sum_fin
      end
      exit
    end

    if l != r
      fin += diff
      sum_fin += fin
    end

    if st > fin
      l.downto(0) { |i|
        if (st-fin).abs <= diff
          puts sum_st + sum_fin - [st,fin].max
          exit
        end
        sum_st -= st
        st -= diff
        fin += diff
        sum_fin += fin
      }
    else
      (l...num).each do |i|
        if (st-fin).abs <= diff
          puts sum_st + sum_fin - [st,fin].max
          exit
        end
        sum_fin -= fin
        fin -= diff
        st += diff
        sum_st += st
      end
    end
  end
end


v1, v2 = gets.split.collect{|i| i.to_i}.minmax
t, d = gets.split.collect{|i| i.to_i}
if d.zero?
    puts v1*t
    exit
end
x = (1.0*d*t+v2-v1)/(2*d).round
ans = v1*x+d*(x-1)*x/2 + v2*(t-x)+d*(t-x-1)*(t-x)/2
puts ans


x, y, m = gets.split.map(&:to_i)
ans = 0
loop do
  x, y = y, x if x > y
  if y >= m
    break
  elsif y <= 0
    ans = -1
    break
  elsif x + y <= [0, m].min
    z = ([0, m].min - x) / y
    x += y * z
    ans += z
  else
    x += y
    ans += 1
  end
end
puts ans


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

one, two = (1..n).partition{ |x| x%2 != 0 }

if k>one.size
    puts two[k-1-one.size]
else
    puts one[k-1]
end


def zyc(str)
  res = true
  l = 0
  r = str.length - 1
  while l < r
    if str[l] == str[r]
      l += 1
      r -= 1
    else
      res = false
      break
    end
  end
  res
end

nums, devider = gets.chomp.split.map(&:to_i)

array = 0
sum = 0

i = 11
while array != nums
  y = i.to_s
  if y.length%2 != 0
    i *= 10
  elsif zyc(y)
    array += 1
    sum += i
    i += 1
  else
    i += 1
  end
end

puts sum%devider


nums, devider = gets.chomp.split.map(&:to_i)
sum = 0
nums.times do |i|
  a = i.to_s
  sum += ( a + a.reverse).to_i
end
puts sum%devider


def exec
  (k, p) = gets.split.map{|i| i.to_i}

  src  = 1
  dist = 10
  sum = 0
  count = 0;

  while true
    for i in src..dist
      break if count == k
      count += 1
      s = i.to_s
      ss = s + s.reverse
      sum += ss.to_i
    end
    break if count == k

    src *= 10
    dist *= 10
  end

  puts sum % p
end

exec


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

x = 1
until x*k>n
  x += 1
end

puts x*k


a, b = gets, gets
a.length.times{|i| print (a[i, 1].to_i == b[i,1].to_i)?0:1}


sammy = gets.chars.map(&:to_i)

ymmas = gets.chars.map(&:to_i)

pokemon = []
digimon = []

sammy.pop
ymmas.pop

pokemon << a.zip(b).map { |x, y| x <=> y}
pokemon.flatten!

pokemon.each do |i|
if i == 1 || i == -1
digimon << 1

else
digimon << 0
end
end

p digimon.join.to_i


n = gets.to_i

ans = 0

abc = []
gets.chomp.each_char do |c|
	if 'a' <= c && c <= 'z'
		abc[c.ord - 'a'.ord] = 1
	else
		ans = [ans, abc.count(1)].max
		abc = []
	end
end

p ans


n = gets.strip.to_i
s = gets.strip
count = Array.new(26, 0)
n.times do |i|
    curr = s[i]
    if curr >= 'a' && curr <= 'z'
        pos = curr.ord - 'a'.ord
        count[pos] += 1
    end
end
ans = 0
count.each do |a|
    ans += 1 if a > 0
end
puts ans


def is_up_case?( chr )
    chr >= 'A' && chr <= 'Z'
end
n = gets.strip.to_i
s = gets.strip
i = ans = 0
prev = '0'
while i < n
    curr = s[i]
    dist = {}
    while !is_up_case? curr
        curr = s[i]
        break if is_up_case? curr
        dist[curr] = 1 if dist[curr].nil?
        prev = curr
        i += 1
    end
    ans = [ans, dist.length].max
    i += 1
end
puts ans


class String
  def is_lower?
    self == self.downcase
  end
end

n = STDIN.gets.chomp.to_i
s = STDIN.gets.chomp
letters = {}
answer = 0
s.each_char do |ch|
  if ch.is_lower?
    letters[ch] = true
  else
    answer = [answer, letters.size].max
    letters = {}
  end
end
puts answer


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

ax, ay, bx, by, cx, cy = gets.split.map{|x| x.to_i}
puts ((ay - by) * (ax - cx) == (ay - by) * (ax - cx)) || (dist(ax, ay, bx, by) != dist(cx, cy, bx, by)) ? "No" : "Yes"


ax,ay,bx,by,cx,cy=gets.split.map(&:to_i)
if ax == bx
    puts ax == cx ? "No" : "Yes"
else
    puts (cy - ay) * (ax - bx) == (ay - by) * (cx - ax) ? "No" : "Yes"
end


puts "введите длину площади"
a = gets.chomp.to_i
puts "введите ширину площади"
b = gets.chomp.to_i
puts "введите длину одной стороны  гранитной плитки"
c = gets.chomp.to_i

def counter_of_squares(length, r)
    x = length / r
    if length % r != 0
        x += 1
    else
        return x
    end
end
puts  counter_of_squares( a , c) * counter_of_squares( b, c)


  n, m, a = ARGV.map(&:to_i)
  na = n / a
  if n % a > 0
    na += 1
  end

  ma = m / a
  if m % a > 0
    ma += 1
  end

 puts na * ma


n, m, a = ARGV.map(&:to_i)
puts (n % a).ceil * (m % a).ceil


input = gets.chomp
value = input.split

x = 1 if value[0].to_i % value[2].to_i != 0
y = 1 if value[1].to_i % value[2].to_i != 0
x += value[0].to_i / value[2].to_i
y += value[1].to_i / value[2].to_i
puts x * y


result = 'YES'
n = -1
s = -1

line1 = ARGF.readline
line2 = ARGF.readline

n = line1.split(' ')[0].to_i
s = line1.split(' ')[1].to_i

temp = []

temp = line2.split(' ')

#convert array of chars into array of int

mugs = []

temp.each do |t|
	mugs.push(t.to_i)
end

groups = mugs.combination(n-1).to_a

groups.each do |g|

	sum = g.reduce(:+)

	if sum > s
		result = 'NO'
		break
	end
end

puts result


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

if mugs.slice(0,n-1).sum <= s
	puts "YES"
else
	puts "NO"
end


def adj(a,b,c,d)
	((a-c)*(a-c))+((b-d)*(b-d))
end
def tri(a,b,c,d,e,f)
	adj(a,b,c,d)>0 && adj(c,d,e,f)>0 && adj(a,b,c,d)+adj(c,d,e,f) == adj(e,f,a,b)
end
def judge(a,b,c,d,e,f)
	tri(c,d,e,f,a,b) || tri(c,d,e,f,a,b) || tri(e,f,a,b,c,d)
end
dx,dy=[0,0,1,-1],[1,-1,0,0]
a=gets.chomp.split.map(&:to_i)
if judge(a[0],a[1],a[2],a[3],a[4],a[5])
	puts "RIGHT"
	exit
end
for i in 0...3
	for j in 0...4
		a[2*i]+=dx[j]
		a[2*i+1]+=dy[j]
		if judge(a[0],a[1],a[2],a[3],a[4],a[5])
			puts "ALMOST"
			exit
		end
	end
end
puts "NEITHER"


def distance(x1,y1,x2,y2)
  xx=x1-x2
  yy=y1-y2
  return xx*xx+yy*yy
end
def isRight(x,y)
  d=Array.new(3)
  3.times do |i|
    d[i]=distance(x[i],y[i],x[(i+1)%3],y[(i+1)%3])
    if d[i] == 0
      return false
    end
  end
  max=-1
  ti=-1
  3.times do |i|
    if max<d[i]
      max=d[i]
      ti=i
    end
  end
  tmp=0
  3.times do |i|
    if i!=ti
      tmp+=d[i]
    end
  end
  return tmp==max
end
tmp=gets.chomp!.split
x=Array.new(3)
y=Array.new(3)
3.times do |i|
  x[i]=tmp[2*i].to_i
  y[i]=tmp[2*i+1].to_i
end
if isRight(x,y)
  print("RIGHT")
  exit
end
3.times do |i|
  -1.upto(1) do |xx|
    -1.upto(1) do |yy|
      tx=x[0,3]
      ty=y[0,3]
      tx[i]=x[i]+xx
      ty[i]=y[i]+yy
      if isRight(tx,ty)
        print "ALMOST"
        exit
      end
    end
  end
end
print "NEITHER"


def dist(x1,y1,x2,y2)
	Math.sqrt(((x2-x1)**2) + ((y2-y1)**2))
end

def right?(distances)
	return false if distances.any? { |e| e.round <=0  }
	(distances[0] ** 2).round + (distances[1] ** 2).round == (distances[2] ** 2).round
end

def almost?(points)
	(0..points.length-1).each do |ind|
		x1,y1,x2,y2,x3,y3 = points.map.each_with_index { |e,i| i == ind ? e + 1 : e }
		# puts x1,y1,x2,y2,x3,y3
		return true if right?([dist(x1,y1,x2,y2),dist(x1,y1,x3,y3),dist(x3,y3,x2,y2)])

		x1,y1,x2,y2,x3,y3 = points.map.each_with_index { |e,i| i == ind ? e - 1 : e }
		# puts x1,y1,x2,y2,x3,y3
		return true if right?([dist(x1,y1,x2,y2),dist(x1,y1,x3,y3),dist(x3,y3,x2,y2)])
	end
	return false
end

x1,y1,x2,y2,x3,y3 = gets.split(" ").map(&:to_i)
distances = [dist(x1,y1,x2,y2),dist(x1,y1,x3,y3),dist(x3,y3,x2,y2)]
distances.sort!
# puts distances
# puts "#{(distances[0] ** 2).to_i}  #{(distances[1] ** 2).to_i} #{(distances[2] ** 2).to_i}"
# puts right?(distances)
ans = "NEITHER"

if (right?(distances))
	ans = "RIGHT"
elsif almost?([x1,y1,x2,y2,x3,y3])
	ans = "ALMOST"
end

puts ans


#!/usr/bin/ruby
s=gets.chomp.chars.sort.join.squeeze
puts s==[*'a'..'z'].join[0,s.size] ? :YES : :NO


@es = [0]
@os = [0]

def get_evens(i, j)
  @es[j+1] - @es[i]
end

def get_odds(i, j)
  @os[j+1] - @os[i]
end

def valid(ary, i, j)
  get_evens(i, j) == get_odds(i, j)
end

@cache = {}
def solve(i, ary, b)
  unless @cache[i]
    if valid(ary, 0, i)==false
      @cache[i] = -1
    elsif i<1
      @cache[i] = 0
    else
      ans = 0
      ni = i-2
      while ni > 0 do
        cost = (ary[ni] - ary[ni+1]).abs
        if cost <= b && valid(ary, ni+1, i)
          mc = solve(ni, ary, b-cost)
          if mc != -1
            ans = [ans, mc+1].max
          end
        end
        ni -= 2
      end
      # puts [i, b, ans].inspect
      @cache[i] = ans
    end
  end
  @cache[i]
end

n, b = gets.strip.split.map(&:to_i)
ary = gets.strip.split.map(&:to_i)
0.upto(n-1).each do |i|
  if ary[i]%2 == 0
    @es << (@es[i] + 1)
    @os << (@os[i])
  else
    @es << (@es[i])
    @os << (@os[i] + 1)
  end
end

puts [solve(n-1, ary, b),0].max
# puts @cache.inspect


s=gets.chomp
i=s.index('1')
s=s[i+1..-1]
puts s.count('0')>=6 ? :YES : :NO


s = gets.chomp
i = s.index(?1)
cnt = 0
(i + 1).upto(s.size - 1) do |i|
    if s[i] == ?0
        cnt += 1
    end
end
if cnt >= 6
    puts "yes"
else
    puts "no"
end


n= gets
while(n.size > 0)
    n.delete_at(0) if n[0] == '0'
    break if n[0]== '1'
end
if n.count('0')>=6
    print('yes')
else
	print('no')
end


n= gets
while(n.size > 0)
    n=n[1..-1] if n[0] == '0'
    break if n[0]== '1'
end
if n.count('0')>=6
    print('yes')
else
	print('no')
end


# http://codeforces.com/contest/596/problem/A
n = gets.chomp.to_i
points = [].tap do |a|
  n.times { a << gets.chomp.split.map(&:to_i) }
end

if n == 1
  puts -1
elsif n == 2
  puts (points[0][0] - points[1][0]).abs * (points[0][1] - points[1][1]).abs
else
  x = points.map(&:first).uniq
  y = points.map(&:last).uniq
  puts (x[0] - x[1]).abs * (y[0] - y[1]).abs
end


n = gets.to_i
xx = []
yy = []
n.times do
  x, y = gets.split.map(&:to_i)
  xx << x
  yy << y
end
xx.sort
yy.sort
ans = (xx[-1] - xx[0]) * (yy[-1] - yy[0])
puts ans > 0? ans : -1


a = gets.strip
b = gets.strip

a.each do |c|
  bc = b.shift
  if c == bc
    print 0
  else
    print 1
  end
end

puts


a = gets.strip
b = gets.strip

a.each_byte do |c|
  bc = b.slice!(0)
  if c == bc
    print 0
  else
    print 1
  end
end

puts


n,a,x,b,y = gets.split.map(&:to_i)
flag = false
while a != x && b != y
    a += 1
    a = 1 if a > n
    b -= 1
    b = n if b <= 0
    if a == b
        f = true
        break
    end
end
if flag
    puts("YES")
else
    puts("NO")
end


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

def square(x)
    x * x
end

def triangle(x)
    x * (x + 1) >> 1
end

def solve(n, x, y, t)
    count = t * t + (t + 1) * (t + 1)
    4.times do
        count -= square(t - x + 1) if x <= t
        count += triangle(t - x + y + 1) if x + y <= t
        x, y = n + 1 - y, x
    end
    count
end

low, high = 0, n - 1 << 1
while low < high
    middle = low + high >> 1
    if solve(n, x, y, middle) >= c
        high = middle
    else
        low = middle + 1
    end
end
p high


 #$stdin.reopen('/cygdrive/d/input.txt', 'r')

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

 w = n*m-k

 if w==0
 	puts 0
 	exit 0
 end


 #$stdin.reopen('/cygdrive/d/input.txt', 'r')

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

def solve(c,d,n,m,k)

	w=n*m-k
	if w==0
		puts 0
		return
	end
	x = c/n
	if x <d
		ans = w/n
		ans *=c
		if w%n!=0
			w= w%n
			if w*d<c
				ans+=w*d
			else
				ans+=c
			end
		end

	else
		ans = w*d
	end
	puts ans
	return
end

solve(c,d,n,m,k)


c, d = gets.split.map {|x| Integer x}
n, m = gets.split.map {|x| Integer x}
k = Integer gets
a = n * m - k
puts [[a / n * c + (a % n) * d, a * d, (a + n - 1) / n * c, 0].min, 0].max


require 'prime'

n = gets.to_i
a = n.prime_division

count = 0

while true

  maxpow = 0
  odd = false
#  p a
  a.each do |z|
    maxpow = z[1] if z[1] > maxpow
    odd = true if z[1]%2==1
  end

  if maxpow == 1
    break
  end

  if odd
    count += 1
    a.each_with_index do |pm,i|
#      if pm[1]%2 !=0
        if maxpow%2==0
          a[i][1]= maxpow
        else
          a[i][1] = maxpow+1
        end
#      end
    end
  end

#  p a
#  p odd
#  p maxpow

  count += 1

  a.each_with_index do |pm,i|
    a[i][1]/=2
  end
end

#puts count
ans = 1
a.each do |x|
  ans*=x[0]
end
puts "#{ans} #{count}"


ns = Array.new(9, 0)
gets.split.map(&:to_i).each { |e| ns[e] += 1 }
puts (if ns.include?(4) && ns.include?(2) || ns.include?(6); "Elephant"
elsif ns.include?(4) || ns.include?(5); "Bear"
else "Alien"
end)


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

c = input.inject({}){|h,e| h[e] ||= 0; h[e] += 1; h }

if c.values.any?{|v| v >= 4 }

  l = c.find{|a,b| a >= 4  }[0]
  cc = input.dup
  4.times do
    cc.delete_at( cc.index(l) )
  end

  if cc.uniq.count > 1
    puts "Bear"
  else
    puts "Elephant"
  end

else
  puts "Alien"
end


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

n = gi

if n == 0
    puts "0 0"
    exit
end

m = n - 1

d = 0

k = 9 + 4 * 3 * n
d = (- 3 + (k ** 0.5)) / 6
d = d.floor

f = d * (d - 1) * 3

case
when (n - f) <= d
    e = n - f
    x = d * 2 - e
    y = e * 2
when (n - f) <= 2 * d
    e = (n - f) - d
    x = d - e * 2
    y = d * 2
when (n - f) <= 3 * d
    e = (n - f) - d * 2
    x = -d - e
    y = d * 2 - e * 2
when (n - f) <= 4 * d
    e = (n - f) - d * 3
    x = - d * 2 + e
    y = -e * 2
when (n - f) <= 5 * d
    e = (n - f) - d * 4
    x = -d + 2 * e
    y = -d * 2
else
    e = (n - f) - d * 5
    x = d + e
    y = -d * 2 + e * 2
end

puts "%d %d" % [x, y]


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

n = gi

if n == 0
    puts "0 0"
    exit
end

m = n - 1

d = 0

while d * (d + 1) * 3 < n
    d += 1
end

f = d * (d - 1) * 3

case
when (n - f) <= d
    e = n - f
    x = d * 2 - e
    y = e * 2
when (n - f) <= 2 * d
    e = (n - f) - d
    x = d - e * 2
    y = d * 2
when (n - f) <= 3 * d
    e = (n - f) - d * 2
    x = -d - e
    y = d * 2 - e * 2
when (n - f) <= 4 * d
    e = (n - f) - d * 3
    x = - d * 2 + e
    y = -e * 2
when (n - f) <= 5 * d
    e = (n - f) - d * 4
    x = -d + 2 * e
    y = -d * 2
else
    e = (n - f) - d * 5
    x = d + e
    y = -d * 2 + e * 2
end

puts "%d %d" % [x, y]


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

n = input[0].to_i
nums = input[1].split('').map(&:to_i)

num1 = nums[0..n-1].sort!
num2 = nums[n..-1].sort!

if num1[0] > num2[0]
	more = true
elsif num1[0] < num2[0]
	more = false
else
	STDOUT.puts "NO"
	exit
end

num1.length.times do |i|
	if more
		if num1[i] < num2[i]
			STDOUT.puts "NO"
			exit
		end
	else
		if num1[i] > num2[i]
			STDOUT.puts "NO"
			exit
		end
	end
end

STDOUT.puts "YES"


n,pos,l,r = gets.split.map(&:to_i)
if r <= pos
  t = l==1 ? -r+1 : -l+2
  ans = pos + t
elsif l >= pos
  t = r==n ? l+1 : r+2
  ans = t - pos
else
  a = l==1 ? 0 : pos-l+1
  b = r==n ? 0 : r-pos+1
  c = (a==0 || b==0) ? 0 : [pos-l,r-pos].min
  ans = a + b + c
end
puts ans


n = gets.chomp.to_i
k = gets.chomp.to_i

k.times do
  if n % 10 == 0 then
    n = n / 10
  else
    n = n - 1
  end
end
puts n


PRESIDENTS = <<STR
18th Century
1. George Washington	 2. John Adams
19th Century
3. Thomas Jefferson	 15. James Buchanan
4. James Madison	 16. Abraham Lincoln
5. James Monroe	 17. Andrew Johnson
6. John Quincy Adams	 18. Ulysses S. Grant
7. Andrew Jackson	 19. Rutherford B. Hayes
8. Martin Van Buren	 20. James Garfield
9. William Henry Harrison	 21. Chester A. Arthur
10. John Tyler	 22. Grover Cleveland
11. James K. Polk	 23. Benjamin Harrison
12. Zachary Taylor	 24. Grover Cleveland
13. Millard Fillmore	 25. William McKinley
14. Franklin Pierce
20th Century
26. Theodore Roosevelt	 35. John F. Kennedy
27. William Howard Taft	 36. Lyndon B. Johnson
28. Woodrow Wilson	 37. Richard M. Nixon
29. Warren G. Harding	 38. Gerald R. Ford
30. Calvin Coolidge	 39. James Carter
31. Herbert Hoover	 40. Ronald Reagan
32. Franklin D. Roosevelt	 41. George H. W. Bush
33. Harry S. Truman	 42. William J. Clinton
34. Dwight D. Eisenhower
21st Century
43. George W. Bush	 44. Barack Obama
STR

n = gets.strip.to_i
PRESIDENTS[/\D#{n}\. (\D+)/]
puts $1.split[1..-1].select{|x| x !~ /\./}.join(' ')


h=Hash.new 0
gets
gets.chomp.chars.each_cons(2){|a|h[a]+=1}
puts h.max_by{|k,v|v.size}[0]*''


n=gets.to_i
a = gets.split(' ').join('').gsub(/^0+|0+$/, '').split('')
a = a.map(&:to_i)
d = 1
ds = [1]
for i in a
  if i == 0
     d+=1
  else
    ds << d
    d = 1
  end
end
puts ds.reduce(&:*)


n = gets.to_i
a = gets.split.map(&:to_i)
s = a.join
t = a.inject(:+)
ans = 1
(t - 1).times do
  ss = s.scan(/10*1/)[0]
  ans *= (ss.size - 1)
  s.gsub!(ss, '1')
end
p ans


n = gets.to_i
a = [0] + gets.split.map(&:to_i) + [0]
aa = a.join.split('1')[1..-2]
p aa.map{|x| x.size + 1}.reduce(1, :*)


n=gets.to_i
a=gets.chomp.split.map(&:to_i)
t=1
l=0
while(a[l]!=1) do l+=1 end
(l+1...n).each do |i|
	if (a[i]==1)
		t*=(i-l)
		l=i
	end
end
p t


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

base = []
while m > 0 do
    base << m % w
    m /= w
end

base.fill(0, base.size, 100)

result = "YES"
for i in 0...100
    if base[i] > 1 and base[i] == w - 1 and
        base[i + 1] += 1
    elsif base[i] > 1
        result = "NO"
        break
    end
end

puts result


# Definition for singly-linked list.
class ListNode
    attr_accessor :val, :next
    def initialize(val)
        @val = val
        @next = nil
    end
end

# @param {ListNode} head
# @param {Integer} val
# @return {ListNode}
def remove_elements(head, val)
  p = head
  h = nil
  t = nil
  until p.nil?
    if p.val != val
      s = ListNode.new(p.val)
      if h.nil?
        h = t = s
      else
        t.next = s
        t = s
      end
    end
    p = p.next
  end
  h

end

s = ListNode.new(1)
s.next = ListNode.new(1)
p remove_elements(s,2)


require 'pp'
def stdin_int; STDIN.gets.chomp.to_i; end
def stdin_ints; STDIN.gets.chomp.split(/ /).map(&:to_i); end

num = stdin_int

taiou = [2, 7, 2, 3, 2,
         3, 2, 5, 1, 2]

a =num/10
b =num%10

puts taiou[a] * taiou[b]


puts (gets.chomp.split "").map{|x| "2723343512"[x.to_i].to_i}.reduce :*


l,r = gets.split

k=0

l1 = l.clone
r1 = r.clone

l1[-1] = l1[0]
if l1.to_i<l.to_i then
  l1=(l1.to_i+10).to_s
  l1[-1]=l1[0]
end

if r1=='10' then r1='9' end
r1[-1] = r1[0]
if r1.to_i>r.to_i then
  r1=(r1.to_i-10).to_s
  r1[-1]=r1[0]
end

def count(b,e,x)
  (e-b+1)*(10**([x-2 ,0].max))
end

def ans(s)
  z=0
  (1..s.length-1).each do |por|
    z+=count(1,9,por)
  end
  z+=count(1,s[0].to_i-1,s.length)+s[1..-2].to_i+1
end
puts r1
k+=ans(r1)
k-=ans(l1)
k+=1
puts k


l,r = gets.split

k=0

l1 = l.clone
r1 = r.clone

l1[-1] = l1[0]
if l1.to_i<l.to_i then
  l1=(l1.to_i+10).to_s
  l1[-1]=l1[0]
end

if r1=='10' then r1='9'
r1[-1] = r1[0]
if r1.to_i>r.to_i then
  r1=(r1.to_i-10).to_s
  r1[-1]=r1[0]
end

def count(b,e,x)
  (e-b+1)*(10**([x-2 ,0].max))
end

def ans(s)
  z=0
  (1..s.length-1).each do |por|
    z+=count(1,9,por)
  end
  z+=count(1,s[0].to_i-1,s.length)+s[1..-2].to_i+1
end
puts r1
k+=ans(r1)
k-=ans(l1)
k+=1
puts k


a, b = gets.split.chomp.map &:to_i
s = gets.chomp.chars.map{|x|x.ord-'a'.ord+1}.sort
res = []
i = 0

while i < a  && res.size != b
  if res.size < 1
    res << s[i]
  elsif (res[-1] - s[i]).abs > 1
    res << s[i]
  end
  i += 1
end

p res.size < b ? -1 : res.reduce(:+)


a, b = gets.split.map &:to_i
s = gets.chars.map{|x|x.ord-'a'.ord+1}.sort
res = []
i = 0
while i < a - 1 && res.size != b
  if res.size < 1
    res << s[i]
  elsif res[-1] - s[i] > 1
    res << s[i]
  end
  a += 1
end
if res.size == b
  puts res.sum
else
  puts -1
 end


a, b = gets.split.map &:to_i
s = gets.chars.map{|x|x.ord-'a'.ord+1}.sort
res = []
i = 0

while i < a  && res.size != b
  if res.size < 1
    res << s[i]
  elsif (res[-1] - s[i]).abs > 1
    res << s[i]
  end
  i += 1
end

p res.size < b ? -1 : res.reduce(:+)


userInput = gets.chomp
result = 0

numberArray = userInput.to_s.split("").map {|item| item.to_i }

loopNum = 0

until loopNum == numberArray.length do
	result += (numberArray.length - loopNum) if numberArray[loopNum] == 4
	result += (2 * (numberArray.length - loopNum)) if numberArray[loopNum] == 7

	loopNum += 1
end

puts result


number = gets.chomp()

number.reverse!

sum = 0

for i in 0..number.length
	if number[i] == '4'
		sum += (2 ** i)
	else
		sum += (2 ** i) * 2
	end
end

puts sum


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

require 'set'

n = gi
x = gis

h1 = Set.new
h1.add [0, 0, 0, -1]

x.each do |z|
    h2 = Set.new
    h1.each do |c, s, r, e|
        case z
        when 0
            h2.add [c, s, r + 1, 2]
        when 1
            if e != 0
                h2.add [c + 1, s, r, 0]
            end
            h2.add [c + 1, s, r + 1, 2]
        when 2
            if e != 1
                h2.add [c, s + 1, r, 1]
            end
            h2.add [c, s, r + 1, 2]
        when 3
            if e != 0
                h2.add [c + 1, s, r, 0]
            end
            if e != 1
                h2.add [c, s + 1, r, 1]
            end
            h2.add [c, s, r + 1, 2]
        end
    end
    h1 = h2
end

ans = nil
h1.each do |c, s, r, e|
    next if c != s
    if ans.nil?
        ans = r
    else
        ans = [ans, r].min
    end
end

if ans.nil?
    puts 0
else
    puts ans
end


n = gets.to_i
numbers = gets.split(' ').map { |x| x.to_i }
#0, 1, 2 ,3

n_arr = []
s_arr = []
c_arr = []

i = 0

while(i < n) do
    if (i == 0)
        n_arr.push 0
        s_arr.push (numbers[i]==1 || numbers[i] == 3) ? 1 : 0
        c_arr.push (numbers[i]==2 || numbers[i] == 3) ? 1 : 0
    else
        n_arr.push [n_arr[i-1], c_arr[i-1], s_arr[i-1]].max
        s_arr.push (numbers[i]==1 || numbers[i] == 3) ? [c_arr[i-1]+1, n_arr[i], n_arr[i-1] + 1].max : n_arr[i]
        c_arr.push (numbers[i]==2 || numbers[i] == 3) ? [s_arr[i-1]+1, n_arr[i], n_arr[i-1] + 1].max : n_arr[i]
    end
    #puts n_arr[i].to_s + ':' + s_arr[i].to_s + ':'+c_arr[i].to_s
    i = i + 1
end


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

#puts "n: #{n}"
#puts "k: #{k}"
#puts "arr: #{arr}"

raise "input error : n or k" if k > n || 0 > k || n >= 50
raise "input error : arr" if arr != arr.sort {|a, b| b <=> a }

k_point = arr[k-1]
if k_point > 0
  puts arr.select{|a| a >= k_point}.count
else
  puts arr.select{|a| a > k_point}.count
end

=begin
k_point > 0 k以上の点数獲得者数
k_point = 0
　k_point以上が存在する(k+1) k+1以上の点数獲得者数
　k_point以上が存在しない(!k+1) 0

以上
=end


n = gets(' ').to_i
k = gets.to_i
input = gets.split.collect{|element| element.to_i}.sort.reverse
cutoff = [1, input[k]].max
count = 0
input.each do |element|
    if element >= cutoff
        count = count+1
    end
end
puts count


n,k  = gets.chomp.split(' ').map!{ |o| o.to_i }
nums = gets.chomp.split(' ').map!{ |o| o.to_i }

puts k + nums[k..n].count n[k]


a= $stdin.read.split.map &:to_i
p a[2..a.size].count{|_|_>=[1,a[m+1]].max}


_, k = gets.split.map(&:to_i);
k-=1
puts gets.split.map(&:to_i).count { |x| x>= a[k] and x > 0 }


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

r = a[k]
puts a.keep_if {|x| x>=r && x > 0}.size


N, t = gets.split.map(&:to_i)
ms = gets.split.map(&:to_i)
w = if ms[t] < 1
      1
    else
      ms[t]
    end
puts ms.select{|m| m >= w}.size


base = gets.chomp().split(' ').map {|value| value.to_i }
places = gets.chomp().split(' ').map {|value| value.to_i }

lowest = places[base[-1]]
puts places.select { |item| item >= lowest and item > 0 }.size


a = gets.chomp.split(' ').to_a
n = a[0].to_i
k = a[1].to_i
a = gets.chomp.split(' ').to_a
b = a.map { |x| x.to_i }
res = 0
b.each do |x|
    res = res + 1 if x >= b[k] && x > 0
end
puts res


n, k = gets.chomp.split(' ').map{|a| a.to_i}

scores = gets.chomp.split(' ').map{|a| a.to_i}

count = 0

scores.each do |s|
  count += 1 if s >= scores[k] && s > 0
end

puts count.to_s


n, k = gets.chomp.split
arr = gets.chomp.split
arr.map!(&:to_i)
c = 0
(0...arr.length).each do |i|
    c += 1 if arr[i] > k
end
p c


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

p a.count {|x| if x > a[k-1]}


inp=gets.split
n=inp[0].to_i
k=inp[1].to_i
a=gets.split
a.map! {|x| x.to_i}

b=a[k-1]

a.map! {|x| (x>=b) & (x!=0)}

a.each {|x| (x==true) ? c+=1:''}
puts c


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

q = gets.chomp.split.map { |x| x.to_i }

p q.select { |a| a >= q[k] and a > 0 }.size


n, k = STDIN.readline.split ' '
n, k = n.to_i, k.to_i

results = STDIN.readline.split ' '
results.map! {|x| x.to_i}

count, lim = 0, results[k]

results.each {|x| if x > 0 && x >= lim then count += 1 end}

puts count


n_and_d = gets.split(" ")
prices = gets.split(" ")
number_of_clients = gets.to_i
each_price = []
total_received = 0
prices.each do |price|
	each_price << price.to_i
end

n = n_and_d[0].to_i
d = n_and_d[1].to_i

to_be_payed = (number_of_clients - n) * d

if number_of_clients <= n
	for client in 0..number_of_clients-1
		total_received += each_price[client]
	end
else
	each_price.each do |sum|
		total_received += sum
	end
end
puts total_received - to_be_payed


n_and_d = gets.split(" ")
prices = gets.split(" ")
number_of_clients = gets.to_i
each_price = []
total_received = 0
prices.each do |price|
	each_price << price.to_i
end
for client in 0..number_of_clients-1
	total_received += each_price[client]
end

n = n_and_d[0].to_i
d = n_and_d[1].to_i

to_be_payed = (number_of_clients - n) * d

puts total_received - to_be_payed


#!/usr/bin/env ruby
n = gets.to_i
k = 0
loop do
  if n % (3**k) != 0
    puts((n / (3.0**k)).ceil)
    break
  end
  k += 1
end


#!/usr/bin/ruby
puts gets.chomp.chop[-1..-1].scan(/[AEIOUYaeiouy]/).size>0 ? 'YES' : 'NO'


s = gets.chomp.upcase!

last = ''
s.chars.each do |ss|
  if ("A".."Z").include?(ss)
    last = ss
  end
end

if ['A', 'E', 'I', 'O', 'U', 'Y'].include?(last)
  puts 'YES'
else
  puts 'NO'
end


class String
  def glasn?
    %w(a e i o u y).include? s[0].downcase
  end
end

s=gets
f=false
s[0..-2].strip[-1].glasn? ? "Yes" : "No"


puts gets.chomp =~ /(a|e|i|o|u|y)(\s?)\?$/i ? 'YES' : 'NO'


googlerese = {
  ?a.ord => "y",
  ?b.ord => "h",
  ?c.ord => "e",
  ?d.ord => "s",
  ?e.ord => "o",
  ?f.ord => "c",
  ?g.ord => "v",
  ?h.ord => "x",
  ?i.ord => "d",
  ?j.ord => "u",
  ?k.ord => "i",
  ?l.ord => "g",
  ?m.ord => "l",
  ?n.ord => "b",
  ?o.ord => "k",
  ?p.ord => "r",
  ?q.ord => "z",
  ?r.ord => "t",
  ?s.ord => "n",
  ?t.ord => "w",
  ?u.ord => "j",
  ?v.ord => "p",
  ?w.ord => "f", # !> statement not reached
  ?x.ord => "m",
  ?y.ord => "a",
  ?z.ord => "q"
}

input = ARGV[0]
# input = 'a_test.txt'
open(input) do |f|
  t = f.gets.to_i
  t.times do |i|
    line = f.gets
    print "Case \##{i + 1}: "
    line.each_byte do |p|
      if ch = googlerese[p]
        print ch
      elsif p == ?\n.ord
        next
      else
        print ' '
      end
    end
    puts
  end
end
# >> Case #1: our language is impossible to understand
# >> Case #2: there are twenty six factorial possibilities
# >> Case #3: so it is okay if you want to just give up


def invalid?(s)
  (s[0] == "0" and s.size > 1) or s.size > 10**6
end

ARGF.each_line do |line|
  max = -1
  digit = line.chomp!.size
  1.upto(digit) do |i|
    1.upto(digit - i - 1) do |j|
      k = digit - i - j

      a = line[0...i]
      b = line[i...(i + j)]
      c = line[(i + j)...digit]
      if invalid?(a) or invalid?(b) or invalid?(c)
        next
      end
      point = a.to_i + b.to_i + c.to_i
      max = point if max < point
    end
  end
  puts max
end


s = gets.strip
P=1000000
n = s.size
#puts 'ok' if "09" != "0" and "09"[0] == "0"
ans = -1
(0...n).each do |i|
  (i+1...n).each do |j|

      iv = s[0...i]
      jv = s[i...j]
      kv = s[j...n]
      #"%s %s %s %d\n" % [iv,jv,kv,ans]
      next if iv.empty? or jv.empty? or kv.empty?
      next if iv != '0' and iv[0] == '0'
      next if jv != '0' and jv[0] == '0'
      next if kv != '0' and kv[0] == '0'
      ans = [ans,iv.to_i+jv.to_i+kv.to_i].max

      #p [iv,jv,kv,ans]

  end
end

puts ans


s = gets.chomp
ans = -1
s.size.times do |i|
  (i + 1).upto(s.size - 1) do |j|
    (j + 1).upto(s.size - 1) do |k|
      a, b, c = s[0..i], s[(i + 1)..j], s[(j + 1)..k]
      next if a.size > 1 and a[0] == ?0
      next if b.size > 1 and b[0] == ?0
      next if c.size > 1 and c[0] == ?0
      next if a.to_i > 1e6.to_i
      next if b.to_i > 1e6.to_i
      next if c.to_i > 1e6.to_i
      ans = [ans, a.to_i + b.to_i + c.to_i].max
    end
  end
end
puts ans


a, b, c = gets.chomp.split(' ')

a = a.to_i
b = b.to_i
c = c.to_i

found = 0

for position in 1..1000000
  a = a * 10 if a < b
  if ( a / b ).to_i == c
    print position.to_s + "\n"
    found = 1
    break
  end
  a = a % b
end

puts "-1" if not found