ns,ms = gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i
ar = Array.new(m)
for i in 0..m-1
	s = gets.chomp
	ar[i] = s.split(/ /).map! {|x| x.to_i }
end

ar.sort! { |a,b| b[1] <=> a[1] }

ret = 0
wk = n
for i in 0..m-1
	break if wk <= 0
	t = [wk, ar[i][0]].min
	ret += t * ar[i][1]
	wk -= t
end

puts ret


a, c = gets.split.map{ |x|
 (x.to_i.to_s(3).reverse+"0"*50).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)


n = gets.to_i
n *= -1 if n.odd?
puts n/2


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

result, i = 0, 0

while true
    temp = m
    while i < tab.size && temp - tab[i] >= 0
        temp -= tab[i]
        i += 1
    end
    result += 1
    if i == tab.size
        break
    end
end

puts result


x,y,a,b=gets.split.map(&:to_i)
q=x.lcm y
p b/q-(a+q-1)/q+1


n, s, c = gets.split.map(&:to_i)
d = gets =~ /tail/ ? 1 : -1
gets.scan(/[01]/).each_with_index do |w, i|
	s = c if w == ?1
	d = 1 if c == 1
	d = -1 if c == n
	c += d
	s += d if s == c
	unless (1 .. n).include? s
		puts "Controller #{i + 1}"
		exit
	end
end
puts :Stowaway


def solution(n, k, d)
  @memo = Array.new(2) { Array.new(n+1, false) }
  def solve(n, k, d, flag)
    return @memo[(flag ? 1 : 0)][n] if @memo[(flag ? 1 : 0)][n]
    return 1 if n == 0 && flag
    return 0 if n == 0 && !flag
    cnt = 0
    for i in 1..k do
      cnt += solve(n-i, k, d, flag || (i >= d)) if n-i >= 0
    end
    @memo[(flag ? 1 : 0)][n] = cnt
  end
  solve(n, k, d, false)
  @memo[0][n] % 1000000007
end

puts solution(*gets.split.map(&:to_i))


a = gets.chomp
b = gets.chomp
for i in (0..a.length-1)
  a[i] = ((a[i].to_i + b[i].to_i)%2).to_s
end
puts a


a = gets.chomp.split.collect{|var| var.to_i}
x = a[0] - ((Math.sqrt(9 + 8*(a[0]+a[1])) - 3)/2)
puts (x).to_i


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

for i in 0 ... n
  for j in i ... n
    # increasing in [0, i]?
    ok = true
    for k in 0 ... i
      if a[k] >= a[k + 1]
        ok = false
        break
      end
    end
    if not ok
      next
    end
    for k in j ... n - 1
      if a[k] <= a[k + 1]
        ok = false
        break
      end
    end
    if not ok
      next
    end
    if a[i .. j].uniq.size != 1
      next
    end
    puts 'YES'
    exit
  end
end
puts 'NO'


a,b=gets.split.map &:to_i
p [a,b,(a+b)/3].min


num=gets.chomp!.scan(/./)
num.length.times do |i|
  num[i]=num[i].to_i
end
num.sort!
j=0
while num[j]==0
  j+=1
end
if j!=num.length
  num[0],num[j]=num[j],num[0]
end
if num.join == gets.chomp!
  puts "OK"
else
  puts "WRONG_ANSWER"
end


year = gets.chomp.to_i

loop do
	year += 1
	if year.to_s.split('').uniq.length == 4
		puts year
		break
	end
end


x = gets.chomp.downcase
y = gets.chomp.downcase

if x==y
	puts 0
elsif x > y
	puts 1
else
	puts -1
end


n = gets.chomp
coins = n.split

count = 0
for i in (0..4)
	count += coins[i].to_i
end

if count%5 == 0 && count != 0
	puts count/5
else
	puts -1
end


v1,v2 = gets.split.map(&:to_i)
t,d  = gets.split.map(&:to_i)

sum = v1 + v2
3.upto(t).each do |_|
  if v1 < v2
    v1 += d
    sum += v1
  else
    v2 += d
    sum += v2
  end
end

puts sum


puts gets.chomp.gsub(/[aoyeui]/i, '').downcase.scan(/./).map{ |s| '.' + s }.join


n, a, b, c = gets.chomp.split().map{ |x| x.to_i }
answer = [ 0, [a, b+c, 3*c].min, [2*a, b, 2*c].min, [ 3*a, a+b, c].min ]
puts answer[ -n % 4 ]


DBG = !true
n,m = gets.split.map{|z| z.to_i}
a = gets.split.map{|z| z.to_i}

def f(n)
  return (n-1)*n/2
end
def g(n,t)
  if DBG
    puts "n #{n} t #{t} fnt1 #{f(n/t+1)} fnt #{f(n/t)}"
  end
  return (n%t)*f(n/t + 1) + (t - n%t)*f(n/t)
end

a.sort_by!{|z| -z}  # decreasing order
asum = [a[0]]
for i in 1...n
  asum << asum[i-1] + a[i]
end
if DBG
  puts "n #{n} m #{m} asum #{asum}"
end
for t in 1..n
  for i in 1..n
    if asum[i-1] - g(i,t) >= m
      puts "#{t}"
      exit 0
    end
  end
end
puts "#{-1}"


  x = gets.chomp
  hash = Hash.new
  x.each_char do |c|
    hash[c] = 0 if hash[c] == nil
    hash[c] += 1
  end
  puts "CHAT WITH HER!" if hash.count % 2 == 0
  puts "IGNORE HIM!" if hash.count % 2 != 0


conds = gets
points = gets

conds = conds.split(" ")
n = conds[0].to_i
k = conds[1].to_i

points = points.split(" ").map{|x| x.to_i}

threshold = [1, points.sort.reverse[k-1]].max
winners = points.select{|x| x >= threshold}.length
puts winners


# http://codeforces.com/problemset/problem/334/B
list = []
list_x = []
list_y = []
8.times do |i|
  list << gets.chomp.split.map(&:to_i)
  list_x << list[i][0]
  list_y << list[i][1]
end

def eight_point_set(list, list_x, list_y)
  hash = Hash.new { |hash, key| hash[key] = 0 }
  for i in 0..7
    hash[list[i]] += 1

    if hash[list[i]] > 1
      return "ugly"
    end
  end

  sorted_list_x = list_x.sort
  sorted_list_y = list_y.sort
  avg_x, avg_y = 0, 0

  for i in 0..7
    avg_x += 1 if sorted_list_x[i] == sorted_list_x[3]
    avg_y += 1 if sorted_list_y[i] == sorted_list_y[3]
  end

  return avg_x == 2 && avg_y == 2 ? "respectable" : "ugly"
end

puts eight_point_set(list, list_x, list_y)


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

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

print stones - colors.size


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

n -= 1

f_base = [x, y, y - x]

base_location = n % 3
sign = (n / 3).even? ? 1 : -1

puts (sign * f_base[base_location]) % 1000000007


def finish(message)
	puts message
	exit
end
s=gets.chomp
t=gets.chomp
hs={}
ht={}
('a' .. 'z').each{|c| hs[c]=0;ht[c]=0}
s.each_char {|e| hs[e]+=1}
t.each_char {|e| ht[e]+=1}
finish('array') if hs==ht
('a' .. 'z').each{|c| finish('need tree') if ht[c]>hs[c]}
pos=-1
t.each_char {|e| pos=s.index(e,pos+1);break unless pos}
finish('automaton') if pos
finish('both')


w = STDIN.read.to_i
if w > 2 && w % 2 == 0
    puts "YES"
else
    puts "NO"
end


k,b,n,t=gets.split.map{|i| i.to_i};x=1;
x,n=x*k+b,n-1 until x>t
puts [0,n+1].max


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

k = n / a
l = m / a

if a * k < n
  k = k + 1
end

if a * l < m
  l = l + 1
end

puts k * l


inp = gets.split(' ')
a = inp[0].to_i
b = inp[1].to_i

c = 0
while a <= b do
  c+=1
  a*=3
  b*=2
end

puts c


primes = Array.new
for i in 2..4096
	mark = true
	for j in 2..i
		if j * j > i; break; end
		if i % j == 0; mark = false; break; end
	end
	if mark; primes.push(i); end
end

almost = Array.new(4096)
almost.each do |x|
	x = 0
end

for i in 1..3000
	j = i
	cnt = 0
	primes.each do |p|
		if p * p > j; break; end
		if j % p == 0
			cnt += 1
			while j % p == 0
				j /= p
			end
		end
	end
	if j != 1
		cnt += 1
	end
	if cnt == 2
		almost[i] = 1
	else
		almost[i] = 0
	end
end

n = gets.chomp
n = n.to_i
ans = 0
for i in 1..n
	ans += almost[i]
end
puts ans


n, m = gets.split.map &:to_i
if m%n != 0
  puts -1
else
  t = m/n
  ans = 0
  while t%2 == 0
    t /= 2
    ans += 1
  end
  while t%3 == 0
    t /= 3
    ans += 1
  end
  if t == 1
    puts ans
  else
    puts -1
  end
end


require 'pp'

# r,g,b,mixed
# 3 flowers 0 <= r,g,b <= 10^9
# output: max bouquets

# sample
# 3 3 6 => 4
# 4 4 4 => 4 !!

# tests
# 3 2 2 => 2
# 2 3 4 => 2
# 1 3 3 => 2 !!
# 2 4 4 => 3 !!

=begin
Approach 1
================
# 0 2 3

if one color (:=red) is zero, the answer is g/3 + b/3 .

if r is 1, the answer is ?
  g%3 == 0
  g%3 == 1
  g%3 == 2

## 1 4 6
## 0 3 5

Approach 2
================
compare the following 3 patterns:
1. make mixed bouquet as many as possible
2. make mixed bouquet as many as possible -1
3. make mixed bouquet as many as possible -2

=end

def _main2(r,g,b,rest)
  a,b,c = [r,g,b].sort!
  return -1 if a < rest

  sum = a - rest
  b -= sum; c -= sum
  sum += b/3; sum += c/3

  sum
end

def main2(r,g,b)
  puts [
    _main2(r,g,b,0),
    _main2(r,g,b,1),
    _main2(r,g,b,2),
    0
  ].max
end

r,g,b = gets.chomp.split(' ').map{|e| e.to_i}
main2(r,g,b)


x = gets.chomp.to_i

total = x

x.downto(1).each_with_index do |i, ind|
  total += i*ind
end

puts total


s = gets.chomp
if s.upcase==s
    s.downcase!
elsif s[0].downcase==s[0] and s[1..-1].upcase==s[1..-1]
    s.capitalize!
end
puts s


a,d,m = gets.chomp.split(' ').map(&:to_i)
g,p,b = gets.chomp.split(' ').map(&:to_i)

total_req = a+d+m
total_grp = g+p+b

if(total_grp < total_req) then puts("NO")
else

	if(a>g) then puts("NO")
	else
		g -= a
		total_grp -= a

		if(g+p < d) then puts("NO")
		else
			total_grp -= d
			if(total_grp < m) then puts("NO")
			else puts("YES") end
		end
	end

end


# coding: utf-8
# if __FILE__ == $0
# end

r, x1, y1, x2, y2 = gets.split.map(&:to_i)
unit = 2*r
direct_distance = Math.sqrt((x1-x2)**2 + (y1-y2)**2)

steps = (direct_distance/unit).floor
direct_distance -= steps * unit
if direct_distance > 0
    # subtract 1 step and make into 2 step to fulfill 2r < R < 4r
    steps += 1
end
puts steps


y, w = gets.split.map(&:to_i)
d_min = [y, w].max
numerator = 7 - d_min >= 0 ? 7 - d_min : 0
divisor = numerator.gcd(6)
numerator /= divisor
denominator = 6 / divisor

puts "#{numerator}/#{denominator}"


matrix = []
3.times { matrix << gets.chomp.scan(/./) }

#p matrix

def rotateMatrix(matrix)
  oldMap = matrix

  # Get the number of lines in the old map (they're the new columns)
  lineCount = oldMap.size
  # Get the number of columns in the old map (We have that many rows now)
  columnCount = oldMap[0].size
  @map = []
  columnCount.times { @map.push [] }

  onLine = 0
  oldMap.each do |oldLine|
    onColumn = 0
    oldLine.each do
      @map[(columnCount - 1) - onColumn][(lineCount - 1) - onLine] = oldLine[(columnCount - 1) - onColumn]
      onColumn += 1
    end
    onLine += 1
  end
  @map
end

matrix2 = rotateMatrix(matrix)
matrix2 = rotateMatrix(matrix2)

puts matrix2 == matrix ? "YES" : "NO"


$a,$b,$n=gets.split.map(&:to_i)
$m=10**9+7
f=[1.0]
1.upto($n){|i|f[i]=f[i-1]*i%$m}
f.map!(&:to_i)
def g(x)while x>0;x,d=x.divmod 10;return false if d!=$a&&d!=$b end; true end
def pow(x,p)p<1?1:pow(x,p/2)**2*(p%2==1?x:1)%$m end
r=0
($n+1).times do|i|
  if g($a*i+($n-i)*$b)
    r+=f[$n]*pow(f[$n-i]*f[i],$m-2)
    r%=$m
  end
end
p r


a, b = gets.split.map(&:to_i)
x = [a, b].min
y = (a + b - [a, b].min * 2)/2
puts [x,y].join(" ")


#!/usr/bin/ruby2.0

def reduce(price, discount)
	s = price.to_s
	i = s.length-1
	i -= 1 while i > 0 && s[i] == '9'
	return price if i <= 0
	x = s[i..-1].to_i + 1
	return price if x > discount
	return reduce(price-x, discount-x)
end

p, d = STDIN.readline.split.collect {|s| s.to_i }
puts reduce(p, d)


require 'prime'

def phi(n)
  d = Prime.prime_division(n)
  d.each { |r, e| n = n * (r - 1) / r }
  n
end

def has_primitive_root?(n)
  return true if n == 1 || n == 2 || n == 4
  return false if n.to_s(2)[1..-1].chars.uniq == [?0] # power of 2 not allowed
  n /= 2 if n.even?
  (3..n).select { |r| Prime.prime?(r) && r**Math.log(r, n).to_i == n } .size > 0
end

def number_of_primitive_roots(n)
  has_primitive_root?(n) ? phi(phi(n)) : 0
end

p number_of_primitive_roots(gets.to_i)


LUCKY_NUMBERS = [4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777]

def lucky_number?(n)
    LUCKY_NUMBERS.include?(n)
end

def divisible_by_lucky?(n)
    LUCKY_NUMBERS.any? { |x| n % x == 0}
end

n = STDIN.read.to_i
puts lucky_number?(n) || divisible_by_lucky?(n) ? "YES" : "NO"


class Euler
  a=gets.to_s;
  sh=0;
  (0..a.length).each do |i|
    if (a[i]=="4" || a[i]=="7")
      sh+=1;
    end
  end
  a=sh.to_s;
  (0..a.length-1).each do |i|
    if (a[i]!="4" && a[i]!="7")
      puts("NO");
      exit;
    end
  end
  puts("YES");
end


class Solver
	def main
		have = gets.split.map { |x| x.to_i }
		need = gets.split.map { |x| x.to_i }
		0.upto(2) do |i|
			tmp = [have[i], need[i]].min
			have[i] -= tmp
			need[i] -= tmp
			have[i] /= 2
		end
		str = ["Yes", "Yes", "No"]
		puts str[have.reduce(:+) <=> need.reduce(:+)]
	end
end

Solver.new.main


res = gets.chomp.chars.map(&:to_i).
         map{|x| [x, 9 -x].min}.map(&:to_s).join('')

res[0] = '9' if res[0] == '0'
puts res


n, k = gets.strip.split(' ').map(&:to_i)
s = gets.strip
j = -1
0.upto(s.length - 1) do |i|
  if s[i] == 'G'
    j = i
    break
  end
end

i1 = j
i2 = j
found = false
while(1)
  i1 -= k
  i2 += k
  if i1 >= 0
    if s[i1] == 'T'
      found = true
      break
    elsif s[i1] == '#'
      i1 = -1
    end
  end
  if i2 < s.length
    if s[i2] == 'T'
      found = true
      break
    end
    if s[i2] == '#'
      i2 = s.length
    end
  end
  if i1 < 0 && i2 >= s.length
    break
  end
end

printf("%s\n", found ? "YES" : "NO")


def remove_zeros(a)
  a.gsub(/0/, '')
end

a = gets.chomp
b = gets.chomp

puts((remove_zeros (a.to_i + b.to_i).to_s).to_i == remove_zeros(a).to_i + remove_zeros(b).to_i ? 'YES' : 'NO')


s = gets
puts s.length - s.gsub('VK', ' ').sub(/KK|VV/, ' ').length


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

n.times do
	arr << gets.split("")
end
eaten = Array.new(n)
eaten.map! { |e| Array.new(m,false) }
count = 0
(0..(n-1)).each do |i|
	(0..(m-1)).each do |j|
		if arr[i][j] == 'W'
			if arr[i][j+1] && arr[i][j+1] == 'P' && !eaten[i][j+1]
				eaten[i][j+1] = true
				count += 1
			elsif j > 0 && arr[i][j-1] == 'P' && !eaten[i][j-1]
				eaten[i][j-1] = true
				count += 1
			elsif arr[i+1] && arr[i+1][j] == 'P' && !eaten[i+1][j]
				eaten[i+1][j] = true
				count += 1
			elsif i > 0 && arr[i-1][j] == 'P' && !eaten[i-1][j]
				eaten[i-1][j] = true
				count += 1
			end
		end
		# puts "#{i} #{j} #{arr[i][j]}"
		# puts "here #{count}"
	end
end
puts count


require 'prime'

def maxprime(x, y)
    y.downto(x){|n| return n if n.prime? }
    -1
end

p, y=gets.split.map &:to_i
q=maxprime(p+1, y)

if q==-1
    puts -1
    exit
end

p=maxprime(2, p)
res=q;
q.upto(y){|x| res=x if x.prime_division[0][0]>p}
puts res


t, w, b = gets.strip.split.map(&:to_i)

l = w.lcm(b)
a = t/l * [w, b].min
a += [t % l + 1, [w, b].min].min
a -= 1

if w == b then
  puts "1/1"
else
  g = a.gcd(t)
  puts "#{a/g}/#{t/g}"
end


a = gets.to_i.to_s(3)
a.chop! while a[-1, 1] == '0'
puts a.chop!.to_i(3) + 1


a=gets.split.map{|x| x.to_i}
b=false
c=0
for i in 0..5
 if a.count(a[i])>=4
  b=true
  c=a[i]
  break
 end
end

s=0
for i in 0..5
 if a[i]==c
  a[i]=nil
  s+=1
 end
 break if s==4
end

if b
 a=a.compact
 puts "Elephant" if a[0]==a[1]
 puts "Bear" if a[0]!=a[1]
else
 puts "Alien"
end


#!/usr/bin/env ruby

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

t = read_ints
k = t.pop
t.map! { |x| x - 1 }.sort!
r = [1] * 3
3.times do |i|
  d = [t[i], k / (3 - i)].min
  r[i..2] = r[i..2].map { |x| x + d }
  k -= d * (3 - i)
  t.map! { |x| x - d}
end
puts r.reduce(:*)


s1=gets.strip
s2=gets.strip
s3=gets.strip
s4=gets.strip
s5=gets.strip
s6=gets.strip
s7=gets.strip
s8=gets.strip

if (s1 == 'WBWBWBWB' || s1 == 'BWBWBWBW') && (s2 == 'WBWBWBWB' || s2 == 'BWBWBWBW') && (s3 == 'WBWBWBWB' || s3 == 'BWBWBWBW') && (s4 == 'WBWBWBWB' || s4 == 'BWBWBWBW') && (s5 == 'WBWBWBWB' || s5 == 'BWBWBWBW') && (s6 == 'WBWBWBWB' || s6 == 'BWBWBWBW') && (s7 == 'WBWBWBWB' || s7 == 'BWBWBWBW') && (s8 == 'WBWBWBWB' || s8 == 'BWBWBWBW')
	puts 'YES'
else
	puts 'NO'
end


n = gets.to_i
p gets.split.map(&:to_i).inject(:gcd) * n


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


s = 'What are you doing at the end of the world? Are you busy? Will you save us?'
t1 = 'What are you doing while sending "'
t2 = '"? Are you busy? Will you send "'
t3 = '"?'
t = t1 + t2 + t3
ls = [ s.length ]
while ls.last < 1e18
  ls << ls.last * 2 + t.length
end

define_method(:get) do |n, k|
  if n == 0
    return k < s.length ? s[k] : '.'
  end
  return t1[k] if k < t1.length
  k -= t1.length
  return get(n-1, k) if k < ls[n-1]
  k -= ls[n-1]
  return t2[k] if k < t2.length
  k -= t2.length
  return get(n-1, k) if k < ls[n-1]
  k -= ls[n-1]
  return t3[k] if k < t3.length
  return '.'
end

q = gets.to_i

q.times do
  n, k = gets.split.map(&:to_i)
  k -= 1
  if n >= ls.length
    d = (n - ls.length + 1) * t1.length
    if k < d
      print t1[k % t1.length]
      next
    else
      k -= d
      n = ls.length - 1
    end
  end
  print get(n, k)
end
puts


a,b=gets.split.map &:to_i
b.times{a=a%10==0?a/10:a-1}
p a


a,b,c,d=gets.split.map(&:to_i)
p=a*d
q=b*c
puts [Rational(p-q,p),Rational(q-p,q)].max


x = gets.chomp.split.map{|i| i.to_i}.inject(:+)
y = gets.chomp.split.map{|i| i.to_i}.inject(:+)

if (x+4)/5 + (y+9)/10 <= gets.to_i
  puts "YES"
else
  puts "NO"
end


a,b,c,d,e=gets.split.map &:to_i
x=[a, c].max
y=[b, d].min
y+=1 unless x<=e and e<=y
p [y-x, 0].max


n, k = gets.strip.split.map(&:to_i)
remainder = n / k
ans = k * remainder + k
puts ans


nt = gets.chomp.split ' '
input = gets.chomp.split ''
size = nt[0].to_i
time = nt[1].to_i

for i in 1..time
  temp = input.clone
  input.each_with_index { |char, index|
    if index == size-1
      break
    end
    if (char+input[index+1]).eql? "BG"
      temp[index] = (temp[index].ord ^ temp[index+1].ord).chr
      temp[index+1] = (temp[index].ord ^ temp[index+1].ord).chr
      temp[index] = (temp[index].ord ^ temp[index+1].ord).chr
    end
  }
  input = temp
end
puts input.join


s=""
1000.times{|i|s+=i.to_s}
puts s[gets.to_i]


#!/usr/bin/ruby

h, m = gets.split(':').map(&:to_i)
3600.times do |i|
    m += 1
    if m == 60
        m = 0
        h += 1
        if h == 24
            h = 0
        end
    end

    hstr = sprintf("%02d", h)
    mstr = sprintf("%02d", m)

    if mstr.reverse == hstr
        puts "#{hstr}:#{mstr}"
        exit
    end
end


def calc(n, k, arr)
	total = 0
	count = 0
	bran = 0
	arr.each do |x|
		total += x
		bran += [8,total].min
		total -= [8,total].min
		count += 1
		if bran >= k
			return count
		end
	end
	return -1
end
n, k = gets.chomp.split(" ").map(&:to_i)
arr = gets.chomp.split(" ").map(&:to_i)
puts calc(n, k, arr)


a, b = gets.split
a = a.to_i + 1
a += 1 until a.to_s.gsub(/[^47]/, '') == b
p a


def f(s,c,dp)
	dp2=[0]*s.size
	count0=0
	count1=0
	i=0
	s.chars.each{|e|
		count1=[count1,dp[i-1]].max if i>0
		if e==c
			count0+=count1
			dp2[i]=count0
		end
		i=i+1
	}
	return dp2
end

s=" "+gets.chomp
ans=0
dp=[1]*s.size
"QAQ".chars.each{|e|
	dp=f(s,e,dp)
}
puts dp.max


def f(k,n,ans)
	if n==0 then
		return 1
	else
		h=(n/2).to_i
		if k==h+1 then
			return ans
		elsif k>h
			return f(k-h-1,h,ans-1)
		else
			return f(k,h,ans-1)
		end
	end
end
n,k=gets.split.map{|e| e.to_i}
puts f(k,(2**n)-1,n)


a,b=gets.split.map &:to_i;puts a+b>0&&(a-b).abs<2?:YES: :NO


line = gets
n, m = line.split.map(&:to_i)
line = gets
c = line.split.map(&:to_i)
c = c.each_with_index.map do |x, i|
	[x, i+1]
end

last_c = nil
i = 0
while c.any?
	break if c.size == 1
	cur = c.first
	c = c[1..-1]
	if cur[0]-m > 0
		c << [cur[0]-m, cur[1]]
	end
end

puts c.first[1]


a,b=gets.split.map{|e| e.to_i}
puts (a*b)/2


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

if ary[-1] == 0
  puts "UP"
elsif ary[-1] == 15
  puts "DOWN"
elsif n==1
  puts "-1"
else
  if ary[-2] < ary[-1]
    puts "UP"
  else
    puts "DOWN"
  end
end


b = gets.to_i

#example 1
# b = 1
#assert 1
#example 2
# b  = 2
#assert 2

if b == 1
	puts 1
else
	require 'prime'

	fast_divisors = b.prime_division.map{ |e| e.last + 1  }.inject(:*)

	puts fast_divisors
end


s = gets
t = gets
start = 0
t.each_char do |i|
    if s[start] == i
        start +=1
    end
end

puts start + 1


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

puts ((c * a - c * b) / b.to_f).ceil


#!/usr/bin/ruby1.9

5.times do |r|
	row = STDIN.readline.split
	5.times do |c|
		if row[c] == '1'
			puts ((r-2).abs+(c-2).abs)
			exit
		end
	end
end


k = gets.to_i
count = Hash.new(0)
arr = []
4.times do
	arr << gets.split("").map(&:to_i)
end
arr.flatten!
arr.each do |a|
	count[a] += 1
end
count.delete(0)
ans = count.values.all?{|c| c <= 2 * k}
puts ans ? "YES" : "NO"


def diff a,b,c,d
  a != b and a != c and a != d and b != c and b != d and c != d and a>=1 and a<=9 and b>=1 and b<=9 and c>=1 and c<=9 and d>=1 and d<=9
end

r1,r2 = gets.split.map(&:to_i)
c1,c2 = gets.split.map(&:to_i)
d1,d2 = gets.split.map(&:to_i)

(1..9).each do |s1|
  s2 = r1 - s1
  s3 = c1 - s1
  s4 = c2 - s2
  if s3+s4 == r2 and s1+s4 == d1 and s2+s3 == d2 and diff(s1,s2,s3,s4)
    puts "#{s1} #{s2}\n#{s3} #{s4}"
    exit
  end
end
puts -1


gets
puts gets.split.map(&:to_i).sort*' '


f = gets.strip.split ''
s = gets.split.join.split ''
puts(s.any?{|x| f.any?{|y| x == y}} ? "YES" : "NO")


 #$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
		exit 0

	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)


# http://codeforces.com/contest/550/problem/B
n, l, r, x = gets.chomp.split.map(&:to_i)
difficulties = gets.chomp.split.map(&:to_i)

selected = []
(2..n).each do |size|
  difficulties.combination(size).each do |arr|
    total = arr.inject(0, :+)

    selected << arr if total >= l && total <= r && (arr.max - arr.min) >= x
  end
end

puts selected.length


n = gets.to_i
res = 0
ns = [1]
i = 10
while ns.last<(10**9 + 1)
  break  if i>n
  temp = [i]
  flag = false
  ns.each do |x|
    if (x+i)<=n
      temp << x+i
    else
      flag = true
    end
  end
  ns += temp.sort
  break if flag
  i *= 10
end
puts ns.size


n,d = gets.split.map(&:to_i)
t = gets.split.map(&:to_i)
d -= t.inject(&:+) + 10 * (n - 1)
if d < 0
	puts '-1'
else
	puts (n - 1) * 2 + d / 5
end


t = gets.to_i
t.times do
  x = gets.to_i
  d = gets.chomp!.split.map &:to_i
  if (a = d[x - 1]) != 0 && b = d[a - 1] != 0
    puts 'YES'
  else
    puts 'NO'
  end
end


a, b = gets.split
p a.to_i + b.reverse.to_i


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

string = gets.chomp

arr = string.each_char.to_a.uniq.sort

if k > 26
  puts -1
  exit
end

weight = 0
i = 0
prevch = 'A'

def addto(char,no)
  return (char.ord+no).chr
end

len = arr.length
while k>0
  if i>=len
    puts -1
    exit
  end

  ch = arr[i]
  if ch<=addto(prevch,1)
    i+=1
    next
  end

  #p ch
  weight += ch.ord-'a'.ord+1
  k-=1
  i+=1
  prevch = ch
end

puts weight


recipe = gets
n = gets.split.map{|e| e.to_i}
p = gets.split.map{|e| e.to_i}
budget = gets.to_i

k = ["B", "S", "C"].map{|e| recipe.count e}

def non_neg(x)
    return x < 0 ? 0 : x
end

l = 0; r = budget + n.max; x = 0
while l <= r
    m = (l + r) / 2
    sum_cost = k.map.with_index{|e, i| e == 0 ? 0 : p[i] * non_neg(e * m - n[i])}.inject(:+)
    if sum_cost < budget
        l = m + 1
        x = m
    elsif sum_cost > budget
        r = m - 1
    else
        x = m
        break
    end
end
puts x


n = gets.to_i
s = gets.chomp
t = [s[0, n], s[n, n]].map{|_| _.chars.to_a.sort}.transpose
puts t.all?{|i, j| i < j} || t.all?{|i, j| i > j} ? :YES : :NO


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

# * 2 and - 1
# go from first to last

count = 0

# go backwards, from last to first
while input.last != input.first do
  if input.last.odd? or input.last < input.first
    input[-1] += 1
  else
    input[-1] /= 2
  end
  count += 1
end

puts count


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


cw_pos = %w{v < ^ >}
ccw_pos = %w{v > ^ <}
start, fin = gets.strip.split
start_pos_cw = cw_pos.index start
start_pos_ccw = ccw_pos.index start
n = gets.strip.to_i
n %= 4
cw = cw_pos[start_pos_cw + (n - 4)] == fin
ccw = ccw_pos[start_pos_ccw + (n - 4)] == fin
if cw && !ccw
    puts "cw"
elsif ccw && !cw
    puts "ccw"
else
    puts "undefined"
end


s = gets
x = s.to_i
h = {}
f = ->(y){ h[y] = 1 if x % y == 0 && s.tr(y.to_s, '') != s }
1.upto(Math.sqrt(x)) do |y|
    f[y]
    f[x / y]
end
p h.size


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

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][n] - d[n][depth - 1]


y=gets.to_i+4;puts %w{Sheldon Leonard Penny Rajesh Howard}[(y>>((y/5).to_s(2).size-1))-5]


#!/usr/bin/ruby
a,b=gets.split.map(&:to_i)
r=0
x=0
while a>0
	r+=a
	x+=a
	a=x/b
	x%=b
end
p r


n = gets.to_i
a = gets.split.map(&:to_i)
i = -1
n -= a[(i += 1) % 7] while n > 0
p i % 7 + 1


a,b,n = gets.split.map(&:to_i)
i=0
loop do
  n -= (i == 0 ? a : b).gcd(n)
  if n < 0
    puts i^1
    exit
  end
  i ^= 1
end


n = Integer(gets)
a = gets.split(" ")
for i in 0..n-1
  a[i] = Integer(a[i])
end
d = []
d[0] = 999999
min = 999999
md = 0
for j in 1..n-1
  value = a[j]-a[j-1]
  if value > md
    md = value
  end
end

for j in 1..n-1
  d[j] = a[j]-a[j-1]
  value = d[j] + d[j-1]
  if value < min
    min = value
  end
  if min < md
    min = md
    break
  end
end
puts min


gets
a,b=$<.map{|e|e.split}
puts (a&b).empty? ? [a.min,b.min].sort.reduce(:+) : (a&b).min


#!/usr/bin/ruby

n = gets.to_i
sum = 0
res = 0
for i in 1..n
  val = gets.to_i
  sum += val
  res = [res, sum.to_f / i].max
end
puts res


END { load __FILE__ unless $stdin.eof? }

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

cnt = Array.new(n + 1, 0)
sum = Array.new(n + 2, 0)
cnt[n] = 1
sum[n] = 1

n.pred.downto(1) do |i|
  cnt[i] = sum[i+1]

  (i * 2).step(n, i) do |k|
    cnt[i] += sum[k]
    cnt[i] %= m
  end
  (i.succ * 2).step(n, i.succ) do |k|
    cnt[i] -= sum[k]
    cnt[i] %= m
  end

  sum[i] = (sum[i+1] + cnt[i]) % m
end

p cnt[1]


#!/usr/bin/env ruby
MOD = 1000000007
m = gets.chomp
cnt = Array.new(15, 0)
def cal(remain, lucky, amount, cnt)
  if remain == 0
    cnt[lucky] += amount
    return
  end
  cal(remain - 1, lucky, amount * 8, cnt)
  cal(remain - 1, lucky + 1, amount * 2, cnt)
end
t = 0
for i in 0...m.size
  for j in 0...m[i].to_i
    is_lucky = j == 4 || j == 7 ? 1 : 0
    cal(m.size - i - 1, t + is_lucky, 1, cnt)
  end
  t += 1 if m[i] == '4' || m[i] == '7'
end
cnt[t] += 1
cnt[0] -= 1
def dfs(party_amount, lucky_amount, method_amount, cnt)
  return if lucky_amount > 9
  if party_amount == 6
    (lucky_amount + 1).upto(9) do |i|
      cnt[10] = (cnt[10] + method_amount * cnt[i]) % MOD
    end
  else
    for i in 0..9
      next unless cnt[i]
      cnt[i] -= 1
      dfs(party_amount + 1, lucky_amount + i, (method_amount * (cnt[i] + 1)) % MOD, cnt)
      cnt[i] += 1
    end
  end
end
dfs(0, 0, 1, cnt)
puts cnt[10] % MOD


a = gets.chomp

vowels = 'AEIOUY'.split('')

cs = a.split('').map { |c| vowels.include?(c)}

ans = 1
cnt = 1
for i in 0...a.size
  if !cs[i]
    cnt += 1
  else
    ans = [ans, cnt].max
    cnt = 1
  end
end
ans = [ans, cnt].max

puts ans


l,d,v,g,r = gets.split(" ").map(&:to_i)

till_a = d/v.to_f
# puts "#{till_a}"
total_interval = g + r
# puts "total_interval#{till_a % total_interval}"
extra_time = (till_a % total_interval) >= g ? (total_interval - (till_a % total_interval)) : 0
# puts "#{extra_time}"
a_to_b = (l-d) / v.to_f
# puts "#{a_to_b}"
#

puts till_a + extra_time + a_to_b


a=gets.chomp.to_i
i=0
loop do
a+=1
i+=1
if "#{a}".count("8")>0
break
end
end
puts "#{i}"


arr = [100,20,10,5,1]
n = gets.to_i
idx = 0
ans = 0
while n!=0 do
  while n<arr[idx] do
    idx+=1
  end
  ans+=n/arr[idx]
  n = n.modulo(arr[idx])
end
puts ans


n,m = gets().split(' ')
n,m = n.to_i, m.to_i
k1 = n / m + (n % m > 0 ? 1 : 0)
k2 = n - (m - 1)
couples = lambda {|n| n*(n-1)/2}
same = n%m == 0 ? m : n%m
other = n%m != 0 ? couples.call(n/m) * (m - n%m) : 0
print couples.call(k1)*same+other, ' ', couples.call(k2)


s = gets.chomp!.gsub(/\/+/, "/")
if s.length > 1
  s.gsub!(/\/$/, "")
end
puts s


n,m,r = gets.split.map(&:to_i)
s = gets.split.map(&:to_i).min
b = gets.split.map(&:to_i).max
if s > b
    puts r
else
    puts r%s+(r/s)*b
end


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

n -= a

if n > b + 1
  n = b + 1
end
puts n


a = gets.strip
was = {[0,0] => 1}
x, y = [0,0]
bad = false
i = 1
a.split('').each{ |c|
  i += 1
  x -= 1 if c == 'L'
  x += 1 if c == 'R'
  y -= 1 if c == 'D'
  y += 1 if c == 'U'
  bad = true if !was[[x, y]].nil? && was[[x, y]] != 0
  (-1..1).each{|a|
    (-1..1).each{|b|
      next if a*a + b* b != 1
      if !was[[x+a, y+b]].nil? and was[[x+a, y+b]] > 0 then
        bad = true if was[[x+a, y+b]] != i - 1
      end
    }
  }
  was[[x, y]] = i
}
if bad then
  puts 'BUG'
else
  puts 'OK'
end


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

n = gets.to_i

ans = 0
while (n > 0) do
  ans += 1 if n % 2 == 1
  n /= 2
end

p ans


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

if m > n
  puts -1
else
  steps = n / 2 + (n % 2)
  cand = steps
  while (cand % m) != 0 do
    cand += 1
  end
  puts cand
end


def f(x)
	y = x.to_s
	s = 0
	for i in 0...y.length()
		s += y[i].to_i
	end
	return s
end
n = 0
tab = []
a, b, c = gets.split().map(&:to_i)
for s in 1..100
	x = b*s**a+c
	if x > 0 and x < 1000000000 and f(x) == s
		n += 1
		tab += [x]
	end
end
puts n
if n != 0
	tab.sort!
	for i in 0...tab.length()
		print tab[i]
		print " "
	end
	puts
end


require 'prime'
n = gets.to_i
if n%2 == 0
  if n == 2
    print 1
  else
    print 2
  end
else
  a = Prime.prime?(n)
  b = Prime.prime?(n-2)
  if(a)
    print 1
  else
    if(b)
      print 2
      exit
    end
    print 3
  end
end


#!/usr/bin/ruby
n=gets.chomp
p [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]}.to_i


a='qwertyuiopasdfghjkl;zxcvbnm,./'
a=a.split("")
b=gets.chomp
if b=="R"
b=-1
else
b=1
end
c=gets.chomp.split("")
c.each do|i|
temp=a.index(i)+b
printf "#{a[temp]}"
end


a, b = gets.split.map(&:to_i)
c, d = gets.split.map(&:to_i)
memo = Array.new(100000)
r, m = b, d
while r <= 100000
  memo[r] = true
  r += a
end
ans = -1
while m <= 100000
  if memo[m]
    ans = m
    break
  end
  m += c
end
p ans


string = gets.chomp
if string.length >= 5 && !(string =~ /[A-Z]/).nil? && !(string =~ /[a-z]/).nil? && !(string =~ /\d/).nil?
  puts 'Correct'
else
  puts 'Too weak'
end


n,m,i,j,a,b=gets.chomp.split.map(&:to_i)
c=[n,m].max
p,q=[1,1,n,n],[1,m,1,m]
4.times do |x|
	dy,dx=(q[x]-j).abs,(p[x]-i).abs
	if dy%b==0 && dx%a==0 && dy/b %2 == dx/a %2
		break if (a > [i-1,n-i].max && dy > 0) || (b > [j-1,m-j].max and dx > 0)
		c=[c,[dy/b,dx/a].max].min
	end
end
puts (c<[n,m].max) ? "#{c}" : "Poor Inna and pony!"


n,k=gets.split.map(&:to_i);
p=[];input=[];
n.times{
    s=gets.split[0]
    tmp=[]
    k.times{|i|
        tmp.push(s[i].to_i)
    }
    input.push(tmp)
}
ans=[]
k.times{|i|
    p.push(i);
}
p.permutation{|a|
    t=[]
    input.each{|i|
        tmp=0
        k.times{|j|
            tmp*=10
            tmp+=i[a[j]]
        }
        t.push(tmp)
    }
    ans.push(t.max-t.min)
}
p ans.min


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

ary.sort!
nf = n * 1.0
sum = ary.inject(:+)

min_req_sum = (4.5 * nf).ceil
# puts "#{min_req_sum} : #{sum}"
if min_req_sum <= sum
  puts 0
else
  diff = min_req_sum - sum
  i = 0
  ans = 0
  while diff > 0 do
    inc = 5 - ary[i]
    diff -= inc
    ans += 1
    i += 1
  end
  puts ans
end


s, v1, v2, t1, t2 = gets.strip.split.map(&:to_i)
first = (s * v1) + (t1 * 2)
second = (s * v2) + (t2 * 2)
if first < second
    puts "First"
elsif second < first
    puts "Second"
else
    puts "Friendship"
end


ar = gets.chomp
hs = ar.split(//).group_by{|x| x=~/[[:upper:]]/}
hs.default = []
if hs[0].length > hs[nil].length
  puts ar.upcase
else
  puts ar.downcase
end


a, b, c, d = gets.split.map &:to_i
p = [a, b, c].sort
puts [d - p[1] + p[0], 0].max + [d - p[2] + p[1], 0].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 < a + b + c
  puts a + a + b + b
else
  if b * 2 + c * 2 < a + b + c
    puts b + b + c + c
  else
    if c * 2 + a * 2 < a + b + c
      puts a + a + c + c
    else
      puts a + b + c
    end
  end
end


hint = gets
temp = hint
arr = Array.new()
num = 10
res = 1
if hint[0] == '?'
  res *= 9
elsif hint[0] > '9'
  res *= 9
  hint = hint[0] + temp.gsub(hint[0],'')
  num = num - 1
end
hint[0] = ''
hint.each_char {|c|
  if c == '?'
    res *= 10
  elsif c > '9' && !(arr.include?(c))
    res *= num
    arr << c
    num = num - 1
  end
}

puts res


r,c = gets.split.map{|i| i.to_i }

mas = []
sr = []
sc = []
r.times do |i|
    str = gets.chomp
    mas[i] = str.split("")
    if str.include? "S" then
        sr << i
        k = -1
        while (k = str.index("S", k+1)) do
            sc << k
        end
    end
end

sum = r*c
r.times do |i|
    c.times do |j|
        if sr.include?(i) && sc.include?(j) then
            sum -= 1
        end
    end
end

puts sum


n, pos, l, r = gets.split.map(&:to_i)

if r == n && l == 1
  puts 0
elsif r == n || l == 1
  ans = 1
  if r == n
    ans += (pos - l).abs
  else
    ans += (pos - r).abs
  end
  puts ans
else
  ans = 2
  ans += [(pos - l).abs, (pos - r).abs].min
  ans += r - l
  puts ans
end


x=gets(p).split
[1,2,3].permutation(3){|a,b,c|f=->(_){eval _.downcase}
if x.all?(&f)
$><<[*?A..?C].sort_by(&f)*""
exit
end}
$><<:Impossible


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

opens = n
throws = n+1
moves = n-1 + [k-1,n-k].min

puts opens + throws + moves


# coding: utf-8

n = gets.to_i
sched = gets.strip.gsub(' ', '')

sched.gsub!(/^0+/, '')
sched.gsub!(/0+$/, '')
sched.gsub!(/0{2,}/, '')

puts sched.length.to_s


N, B = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)

sum = 0
cand = []
(N-1).times do |i|
    sum += as[i]%2==0 ? 1 : -1
    cand.push((as[i] - as[i+1]).abs) if sum == 0
end
cand.sort!
ans = 0
sum = 0
cand.each do |c|
    sum += c
    if sum <= B
        ans += 1
    else
        break
    end
end
puts ans


def F(x,s);x-(-x%s*(x/s));end;n,m,s=gets.split.map &:to_i;p F(n,s)*F(m,s)


EPS=1e-9
def s(x)
	ret=0
	while x>0
		ret+=x%10
		x/=10
	end
	ret
end

def is_int?(_x)
	x=_x.round
	(x-EPS<=_x)&&(_x<=x+EPS)
end

n=gets.to_i
ans=100.times.map{|s|
	_x=(-s+(s*s+4*n)**0.5)/2.0
	if is_int?(_x)
		x=_x.round
		if x*x+s(x)*x-n==0
			x
		else
			nil
		end
	else
		nil
	end
}

p ans.any? ? ans.compact.min : -1


n, m = gets.chomp.split(' ').map(&:to_i)
x = 1
ans = 0
until x > n
	ans += ((m + x) / 5) - (x / 5)
	x += 1
end

puts ans


x,y,m=gets.split.map(&:to_i)
x,y = y,x if x > y
res = 0
if x < 0 and y > 0 and m > y
    t = (-x+y-1)/y
    x += t*y
    res += t
end
cnt=0
while y < m
    x,y = y,x+y
    cnt+=1;
    break if cnt > 100
end
puts cnt > 100 ? -1 : res+cnt


def answer(num)
  ret = []
  counts = Hash.new(0)
  num.each_char.with_index do |c, index|
    i = c.to_i
    case i
    when 4,7
      if counts[c] == num.size / 2 && c == '4'
        c = '7'
      end
      if counts[c] < num.size / 2
        ret << c
        counts[c] += 1
        next
      end
    when 0..3
      if counts['4'] == num.size / 2
        ret << '7'
        counts['7'] += 1
      end
      ret << '4' * (num.size / 2 - counts['4'])
      ret << '7' * (num.size / 2 - counts['7'])
      return ret.join
    when 5,6
      if counts['7'] < num.size / 2
        ret << '7'
        counts['7'] += 1
        ret << '4' * (num.size / 2 - counts['4'])
        ret << '7' * (num.size / 2 - counts['7'])
        return ret.join
      end
    when 8,9
      if index == 0
        size = (num.size + 2) / 2
        return ('4' * size) + ('7' * size)
      end
    end
    while n = ret.pop
      counts[n] -= 1
      if n == '4' && counts['7'] < num.size / 2
        ret << '7'
        counts['7'] += 1
        ret << '4' * (num.size / 2 - counts['4'])
        ret << '7' * (num.size / 2 - counts['7'])
        return ret.join
      end
    end
    size = (num.size + 2) / 2
    return ('4' * size) + ('7' * size)
  end
  ret.join
end

num = gets.strip
if num.size % 2 == 1
  size = (num.size + 1) / 2
  puts '4' * size + '7' * size
else
  puts answer num
end


line = gets.chomp

x, y, z, t1, t2, t3 = line.split.map(&:to_i)

elevator_time = ( (z-x).abs + (x-y).abs ) * t2 + 3 * t3
stair_time = (y-x).abs * t1

# puts elevator_time
# puts stair_time

if stair_time >= elevator_time
    puts "YES"
else
    puts "NO"
end


s = gets.chomp
t = gets.chomp
puts s.reverse == t ? 'YES' : 'NO'


# your code goes here

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

n = inp[0]
w = inp[1]

flag = false

no_parenthesis = ""
with_parenthesis = ""

counter = 0
w.split("").each do |c|

	if c == "("
		flag = true
		next
	end

	if c == ")"
		flag = false

		with_parenthesis.split("_").each do |cur|
			counter = counter + 1 if cur.length > 0
		end

		with_parenthesis = ""

		no_parenthesis += "_"
		next
	end

	with_parenthesis += c if flag
	next if flag
	no_parenthesis += c
end

max_l = 0
no_parenthesis.split("_").each do |w|
  max_l = w.length if w.length > max_l
end

puts "#{max_l} #{counter}"


vals = gets.split.map{|s| s.to_i}

maxv = 0
maxi = -1
sum = 0
res = []
vals.each_index {|i|
	sum += vals[i]
	if maxv < vals[i]
		maxv = vals[i]
		maxi = i
	end
}
if sum % 2 == 1
	print "Impossible"
elsif sum - maxv < maxv
	print "Impossible"
else
	dif = sum/2 - maxv
	res[maxi] = vals[(maxi+1)%3]-dif
	res[(maxi+1)%3] = dif
	res[(maxi+2)%3] = vals[(maxi+2)%3]-dif
	print res.join(" ")
end


a, b = gets.split.map(&:to_i)
s = gets.chop

if a < 0
  a = -a
  s.tr! 'LR', 'RL'
end

if b < 0
  b = -b
  s.tr! 'UD', 'DU'
end

maxx = maxy = x = y = 0

def step(x, y, c)
  if c == 'U' then
    y += 1
  elsif c == 'D' then
    y -= 1
  elsif c == 'R' then
    x += 1
  elsif c == 'L' then
    x -= 1
  end
  [x, y]
end

s = "0#{s}"
s.each_char do |c|
  x, y = step x, y, c
  maxx = x if x > maxx
  maxy = y if y > maxy
end

dx = x
dy = y

startx = a - maxx - 1
starty = b - maxy - 1

inf = 10**9 + 10
iter = inf
iter = startx / dx if dx > 0
iter = [iter, starty / dy].min if dy > 0

iter = 0 if iter == inf or iter < 0

x = iter * dx
y = iter * dy

s.size.times do
  s.each_char do |c|
    x, y = step x, y, c
    if x == a and y == b
      puts 'Yes'
      exit
    end
  end
end

puts 'No'


hello = "hello"
input = gets.chomp
i = 0
pass = false
input.each_char do |char|
    if char == hello[i]
        i += 1
    end
    if i == hello.length
        pass = true
        break
    end
end

if pass
    puts "YES"
else
    puts "NO"
end


n,d=gets.split.map(&:to_i)
a=gets.split.map(&:to_i).sort
m=gets.to_i
res=0
[m,n].min.times {|i| res+=a[i] }
puts res-d*[0,m-n].max


arr = gets.split.map &:to_i
$n, x0, y0, dx, dy, t = arr
dx %= $n
dy %= $n

def zmul(a, b)
  ans = 0
  6.times do |i|
    ans += (a[i] * b[i] - 1) % $n + 1
    ans -= $n if ans > $n
  end
  ans
end

class Matrix
  def initialize(n)
    @mat = Array.new(n) do |i|
      Array.new(n) { |j| i == j ? 1 : 0 }
    end
  end

  def [](index)
    @mat[index]
  end

  def []=(index, val)
    @mat[index] = val
  end

  def mul(rhs)
    ans = Matrix.new(6)
    6.times do |i|
      6.times do |j|
        ans[i][j] = 0
        6.times do |k|
          ans[i][j] += (@mat[i][k] * rhs[k][j] - 1) % $n + 1
          ans[i][j] -= $n if ans[i][j] > $n
        end
      end
    end
    ans
  end

  def pow(e)
    ans = Matrix.new(6)
    tmp = Matrix.new(6)
    6.times { |i| tmp[i] = Array.new(6) { |j| @mat[i][j] } }
    while e > 0
      ans = ans.mul(tmp) if (e & 1) == 1
      tmp = tmp.mul(tmp) if (e >>= 1) > 0
    end
    ans
  end
end

mat = Matrix.new(6)
mat[0] = [2, 1, 1, 0, 1, 0]
mat[1] = [1, 2, 0, 1, 1, 0]
mat[2] = [1, 1, 1, 0, 1, 0]
mat[3] = [1, 1, 0, 1, 1, 0]
mat[4] = [0, 0, 0, 0, 1, 1]
mat[5] = [0, 0, 0, 0, 0, 1]

mat = mat.pow(t)

arr.shift
arr[-1..-1] = [0, 1]
puts "#{zmul(mat[0], arr)} #{zmul(mat[1], arr)}"


s=eval gets.sub(' ','^');99.times{s|=s/2};p s


l,r,x,y,k=gets.split.map &:to_i
l=(l+k-1)/k
r=r/k
puts l<=r && x<=r && l<=y ? :YES : :NO


m, s = gets.split.map &:to_i
small, large = '-1', '-1'
if s == 0 and m == 1 then
  small, large = '0', '0'
elsif s > 0 and m * 9 >= s
  small = '0' * m
  small[0] = '1'
  r = s - 1
  (m-1).downto(1).each do |i|
    if r <= 9 then
      small[i] = (r + '0'.ord).chr
      r = 0
    else
      small[i] = '9'
      r -= 9
    end
  end
  if r > 0 then
    small[0] = (r + '1'.ord).chr
  end
  large = '0' * m
  (0...m).each do |i|
    if s <= 9 then
      large[i] = (s + '0'.ord).chr
      break
    else
      large[i] = '9'
      s -= 9
    end
  end
end
puts small + ' ' + large


n=gets.chomp
if n.to_i >= 0 then
    puts n
else
    x=n.chop.to_i
    y=n.chop.chop.to_i*10-n[-1].to_i
    puts [x,y].max
end


d=gets.to_i.times.map { gets.split.map(&:to_i) }.transpose; p d[0].product(d[1]).select{|x,y| x==y}.length;


s1, s2, s3 = gets.chomp.split(/ /).map(&:to_i)
a = Math.sqrt( s1 * s3 / s2 )
b = Math.sqrt( s1 * s2 / s3 )
c = Math.sqrt( s2 * s3 / s1 )
sum = 4 * ( a + b + c )
puts sum.to_i


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] >= w -1
       base[i+1] += 1
    elsif base[i] > 1
        result = "NO"
        break
    end
end

puts result


a,b = gets.split(" ").map {|x| x.to_i }

puts a + b


cityNum, livingCity = gets.chomp.split(' ').map(&:to_i)
criminals = gets.chomp.split(' ').map(&:to_i)

maxDist = [(cityNum - livingCity), livingCity - 1].max
checkedDist = 0

sure = 0

until checkedDist > maxDist
	leftCity = livingCity - checkedDist
	rightCity = livingCity + checkedDist

	lackNumber = 0
	lackNumber += 1 if leftCity <= 0
	lackNumber += 1 if rightCity > cityNum
	if leftCity == rightCity
		sure += 1 if criminals[livingCity - 1] > 0
	else
		case lackNumber
		when 0 then sure += 2 if criminals[leftCity - 1] > 0 && criminals[rightCity - 1] > 0
		when 1 then	sure += 1 if (leftCity > 0 && criminals[leftCity - 1] > 0) || (rightCity <= cityNum && criminals[rightCity - 1] > 0)
		end
	end

	checkedDist += 1
end

puts sure


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

if n1 > n2
  puts 'First'
else
  puts 'Second'
end


puts ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][({"January"=>0, "February"=>1, "March"=>2, "April"=>3, "May"=>4, "June"=>5, "July"=>6, "August"=>7, "September"=>8, "October"=>9, "November"=>10, "December"=>11}[gets.chomp]+gets.to_i)%12]


a,b=gets.split.map &:to_i;p a==b ? a : 1


a = [
    "Washington",
    "Adams",
  "Jefferson",
  "Madison",
  "Monroe",
  "Adams",
  "Jackson",
  "Van Buren",
  "Harrison",
  "Tyler",
  "Polk",
  "Taylor",
  "Fillmore",
  "Pierce",
  "Buchanan",
  "Lincoln",
  "Johnson",
  "Grant",
  "Hayes",
  "Garfield",
  "Arthur",
  "Cleveland",
  "Harrison",
  "Cleveland",
  "McKinley",
  "Roosevelt",
  "Taft",
  "Wilson",
  "Harding",
  "Coolidge",
  "Hoover",
  "Roosevelt",
  "Truman",
  "Eisenhower",
  "Kennedy",
  "Johnson",
  "Nixon",
  "Ford",
  "Carter",
  "Reagan",
  "Bush",
]

n = gets.to_i
puts a[n-1]


gets
s = gets.chomp

result = ''
1.upto(10) do |i|
  break if s.empty?

  result += s[0]

  s.replace s[i..-1]
end

puts result


n,l = gets.split.map &:to_i
c = gets.split.map &:to_i

(n-1).times{|i| c[i+1] = c[i]*2 if c[i+1] > c[i]*2}

ans = []
just = 0
(n-1).downto(0){|i|
  d = (i+1).times.map{|j| c[j]*(2**(i-j))}
  x = 2**i
  ne = l/x
  l %= x
  just += d.min*ne
  ans << just + d.min
}

ans << just

p ans.min


n, t, k, d = gets.split.map(&:to_i)
fst = (n + k - 1) / k * t
snd = fst + 1
for i in 0 .. fst
  tmp = i / t * k
  if i >= d
    tmp += (i - d) / t * k
  end
  if tmp >= n
    snd = i
    break
  end
end
puts (fst > snd ? "YES" : "NO")


w,h,k=gets.split.map &:to_i
p k*(2*(w+h)-8*k+4)


n=gets.to_i;a=gets.split.map(&:to_i);(amax,amin)=[a.find_index(a.max),a.find_index(a.min)];puts [amax, n-amax-1, amin, n-amin-1].max


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

# if sum >=3
# 	min,min_ind = arr.each_with_index.min
# 	arr.delete_at(min_ind)
# 	arr = arr.map{|e| e - min}
# 	ans = min
# 	sum = arr.inject(0,:+)
# 	if sum >= 3
# 		max = arr.max
# 		pair =  max / 2
# 		min = arr.min
# 		if pair <= min
# 			ans += pair
# 			if pair % 2 != 0
# 				min -= pair
# 				ans += 1 if min >= 2
# 			end
# 		else
# 			ans += min
# 		end

# 	end
# end
# arr = []
# ac2 = a/2
# bc2 = b/2
# cc2 = c/2
# # 0 2 1
# arr << [bc2,c].min
# # 0 1 2
# arr <<  b + cc2
# # 1 1 1
# arr << a + b + c
# # 1 2 0
# # 1 0 2
# puts ans
#  66,
#  34,23

arr.sort!
if((2 * (arr[0] + arr[1])) <= arr[2])
	puts arr[0] + arr[1]
else
	puts arr.inject(0,:+) / 3
end


a=gets.chomp.split(" ")
s=a[1].to_i
q=a[2].to_i
t=a[0].to_i
ans=1
s=s*q
loop do
if s<t
s=s*q
ans+=1
else
break
end
end
puts "#{ans}"


input = gets.chomp.to_i

result = 0
numOfBases = input - 2

2.upto(input - 1) do |base|
    temp = input
    while (temp > 0)
        mod = temp % base
        temp = temp / base
        result = result + mod
    end
end

nominator = result
denominator = numOfBases

#find the gcd, it's in nominator
while denominator > 0
    temp = denominator
    denominator = nominator % denominator
    nominator = temp
end

denominator = numOfBases / nominator
nominator = result / nominator

puts "#{nominator}/#{denominator}"


arraynum1 = ["zero", "one","two","three","four","five","six","seven","eight","nine"]
arraynum2 = [nil , nil ,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"]
arraynumtens = ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]
numero = gets.chomp
array = numero.split(//)
if numero.to_i >=10 && numero.to_i <20
    print "#{arraynumtens[array[1].to_i]}"

elsif array.length == 2
    if array[1].to_i == 0
        print "#{arraynum2[array[0].to_i]}"
    else
        print "#{arraynum2[array[0].to_i]}-"+"#{arraynum1[array[1].to_i]}"
    end
elsif array.length == 1
    print "#{arraynum1[array[0].to_i]}"
end


n, m = gets.split.map{|x| (x.to_i-1).to_s(7)}
ln, lm = [n, m].map(&:size)
puts (0...7).to_a.permutation(ln + lm).lazy.map(&:join).select{|a| a[0...ln] <= n && a[ln..-1] <= m}.count


# coding: utf-8

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

# n cities, v liters of fuel tank
# beginning: empty tank
# each city has a gas station, i-th city=> $i/liter
# minimize expense.
# 2<=n<=100, 1<=v<=100

tank = 0
city = 1
expense = 0

while city < n
    fueling = [n - city, v - tank].min # (required, tank volume)
    # p "> #{fueling} city #{city} n #{n}"
    if n - city <= tank
        city += 1
    else
        expense += fueling*city
        tank += fueling
        city += 1
    end
    tank -= 1
end

puts expense


l,r=gets.split.map &:to_i;t=1;t*=10 while t<=r;p=[[t/2,l].max,r].min;p p*(t-1-p)


board=[]
num=0
for i in 0...8
  board[i]=STDIN.gets().chomp()
  all=true
  for j in 0...board[i].length
    if board[i][j]=='W'
      all=false
      break
    end
  end
  num+=1 if all
end

if num==8
  print("#{num}\n")
  exit()
end

for i in 0...8
  all=true
  for j in 0...8
    if board[j][i]=='W'
      all=false
      break
    end
  end
  num+=1 if all
end
print("#{num}\n")


def solve(p,k)
  b = [1]
  l = [0]
  r = [k]
  while true
    bb = b.last
    nb = -k*bb
    b.push(nb)
    if nb > 0
      r.push(r.last + (k-1)*nb)
      l.push(l.last)
    else
      l.push(l.last + (k-1)*nb)
      r.push(r.last)
    end
    break if p < r.last
  end

  ans = []
  while b.length > 1
    l.pop
    r.pop
    bb = b.pop
    le = l.last
    ri = r.last
    i = 0
    while !(le <= p && p < ri)
      p -= bb
      i += 1
    end
    ans.push(i)
  end
  ans.push(p)
  ans.reverse!
  while ans.last == 0
    ans.pop
  end
  return ans
end

p, k = gets.chomp.split
p = p.to_i
k = k.to_i
ans = solve(p,k)
puts(ans.length)
puts(ans.join(" "))


def toi(n, x, y)
	if x == 0
		return y
	elsif y == n
		return n + x
	elsif x == n
		return n + n + (n - y)
	else
		return n + n + n + (n - x)
	end
end

n, x1, y1, x2, y2 = gets.split.map(&:to_i)
z1 = toi(n, x1, y1)
z2 = toi(n, x2, y2)
m = 4 * n
d = (z2 - z1 + m) % m
p [d, m - d].min


num = gets.chomp.to_i

n = Math::sqrt(8 * num + 1) - 1 / 2

if n % 1.0 == 0
  puts "YES"
else
  puts "NO"
end


n, m = gets.split(" ").map {|x| x.to_i}
i = 0
while n > 0
  n -= 1
  i += 1
  if i % m == 0
    n += 1
  end
end
puts(i)


target = gets.chomp.to_i
m = gets.chomp
months = m.split

for i in (0..11)
	for x in (i..11)
		if i != x
			if months[i].to_i > months[x].to_i
				temp = months[i].to_i
				months[i] = months[x].to_i
				months[x] = temp
			end
		end
	end
end

growth = 0
i = 11
count = 0
while growth < target && i >= 0
	growth += months[i].to_i
	count += 1
	i -= 1
end

if growth < target
	print -1
else
	print count
end


cubes = gets.chomp.to_i

sum = 0
a = 0
1.upto(1000){ |i|
	a += i
	sum += a
	if cubes < sum
		p i -1
		exit
	end
}


def gao1(a, b)
	c = 0
	4.times{|i| c += 1 if a[i] == b[i]}
	c
end

def gao2(a, b)
	c = 0
	4.times{|i| c += 1 if b.index(a[i])}
	c
end

n = gets.to_i
x = 100.upto(9999).map{|_| '%04d' % _}
x.reject!{|_| _.bytes.sort.uniq.size < 4}
n.times do
	a, b, c = gets.split.map(&:to_i)
	a = '%04d' % a
	c += b
	x.reject!{|_| gao1(_, a) != b || gao2(_, a) != c}
end

if x.empty?
	puts 'Incorrect data'
elsif x.size > 1
	puts 'Need more data'
else
	puts x[0]
end


t = gets.chomp.to_i
t.times { puts (2 ** gets.chomp.to_i) - 1 }


number = gets.chomp.to_i
string = gets.chomp.downcase

letters = string.split('')

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

if number < 24
	print "NO"
else
	for i in (0..number-1)
		if alphabet.include? letters[i]
			alphabet -= [letters[i]]
		end
	end
	if alphabet.size == 0
		print "YES"
	else
		print "NO"
	end
end


#bismillahir rahmanir rahim

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

k = 240-k
c = 0

for i in 1..10
    q = 5*i*(i+1)
    q = q/2
    if k>=q
        c = c+1
    end
end

c=n if c>n

puts c


# coding: utf-8

# if __FILE__ == $0
# end
A = 'A'.ord

def convnum(twogram)
    return (twogram[0].ord - A)*26 + (twogram[1].ord - A)
end

n = gets.to_i
s = gets.chomp
b = s[0]
counter = Array.new(26*26) { 0 }
s[1...n].split(//).each { |c|
    b += c
    counter[convnum(b)] += 1
    b = c
}

highest_idx = -1
highest_freq = 0

counter.each_with_index { |item, index|
    if item > highest_freq
        highest_freq = item
        highest_idx = index
    end
}

puts "#{(highest_idx/26 + A).chr}#{(highest_idx%26 + A).chr}"


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


# coding: utf-8

# if __FILE__ == $0
# end

# 2, 3, 5, 6
ks = gets.split.map(&:to_i)
total = 0
min256 = [ks[0], ks[2], ks[3]].min
total += 256 * min256
min32 = [ks[0] - min256, ks[1]].min
total += 32 * min32

puts total


#!/usr/bin/env ruby

rgb = gets.split.map(&:to_i)
time = color = 0
while rgb[0] > 0 || rgb[1] > 0 || rgb[2] > 0
    time += 1
    rgb[color] = rgb[color] < 2 ? 0 : rgb[color] - 2
    color = (color + 1) % 3
end
puts time+29


n = gets.chomp.to_i
interestings = gets.chomp.split(/ /).map(&:to_i)
watch = 0
prev = 0
(0...n).each do |i|
    curr = interestings[i]
    watch = curr
    if curr - prev > 15
        watch = prev + 15
        break
    end
    prev = curr
end
watch = prev + 15 if 90 - prev > 15
watch = 90 if watch > 90 || 90 - prev <= 15
puts watch


n,k,*a=$<.read.split.map &:to_i
p a.all?{|e|e<=k}?n:a.index{|e|e>k}+(n-1-a.rindex{|e|e>k})


gets
arr = gets.split

cnt1 = arr.count(?1)
ans = 0
cnt0 = 0

arr.each do |x|
	if x == ?1
		ans = [ans, cnt0 + cnt1].max
		cnt1 -= 1
	else
		cnt0 += 1
	end
end

p [ans, cnt0 + cnt1].max


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

loop do
  unless m - (i % n + 1) < 0
    m -= (i % n) + 1
    i += 1
  else
    break
  end
end

puts m


n = gets.to_i
m = gets.to_i

if n>=27
    puts m
else puts m%(1<<n)
end


a = gets.chomp.split(" ").map{|i| i.to_i}
result = 0

GT = []
GT[0] =1

(1..30).each do |i|
    GT[i] = GT[i-1]*i
end

def tohop k,n
    kq = GT[n]/(GT[k]*GT[n-k])
    return kq
end

def min a,b
    kq = a
    if b < a
        kq = b
    end
    return kq
end

c = 1
if a[2]-a[0]> c
    c = a[2] - a[0]
end
d = min a[1],(a[2]-4)

(c..d).each do |i|
    m = tohop i,a[1]
    n = tohop a[2]-i,a[0]
    result += m*n
end

puts result


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+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


a=gets.chomp.split(" ")
g=a[0].to_i
c=a[1].to_i

if c==0
min=g
max=g
puts "#{min} #{max}"
elsif g==0
puts "Impossible"
elsif g>=c
min=g
max=g+c-1
puts "#{min} #{max}"
else
min=c
max=g+c-1
puts "#{min} #{max}"
end


a, b, c = gets.chomp.split(/ /).map(&:to_i)
ans = "NO"
if c == 0
    ans = "YES" if a == b
else
    d = (b - a) % c
    e = (b - a) / c
    ans = "YES" if d == 0 && e >= 0
end
puts ans


if File.exists?("input")
  $stdin = File.open("input")
  $stdout = File.open("output","w")
end

whwh = gets.chomp.split(" ").map(&:to_i)
w1=whwh[0]
h1=whwh[1]
w2=whwh[2]
h2=whwh[3]

ans = h1+h2+w1+w2+4+h1+h2+(w1-w2).abs
puts ans


#!/usr/bin/ruby
x=3
gets
puts $<.all?{|e|
	e=e.to_i
	e!=x && x=6-x-e
} ? :YES : :NO


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
k+=ans(r1)
k-=ans(l1)
k+=1
puts k


n = gets.to_i
l0, *a = gets.split.map &:to_i
a.sort!
a.reverse!
l = l0

if l > a[0]
    puts 0
    exit
end

h = a[0]
i = 0
loop {
    h -= 1
    i += 1 until i >= a.size || a[i] <= h
    if l + i - 1 > h + 1
        puts h+2-l0
        break
    end
    l += i
    if l > h
        puts l - l0
        break
    end
}


r1, c1, r2, c2 = gets.split.collect{|i| i.to_i}
if r1==r2 and c1==c2
    rook = 0
elsif r1==r2 or c1==c2
    rook = 1
else
    rook = 2
end
if (r1+c1)%2 != (r2+c2)%2
    bishop = 0
elsif r1+c1==r2+c2 or r1+c2==r2+c1
    bishop = 1
else
    bishop = 2
end
king = [(r1-r2).abs,(c1-c2).abs].max
puts [rook,bishop,king].join(" ")


n = gets.to_i
min1, max1 = gets.split.map { |e| e.to_i }
min2, max2 = gets.split.map { |e| e.to_i }
min3, max3 = gets.split.map { |e| e.to_i }

d1, d2, d3 = 0, 0, 0

d1 = n-min2-min3 > max1 ? max1 : n-min2-min3
n -= d1
print d1.to_s + ' '

d2 = n-min3 > max2 ? max2 : n-min3
n -= d2
print d2.to_s + ' '

print n


table = Hash.new(false)
(1..5).each { |i| table[i] = Hash.new(false) }

count = gets.chop.to_i
(1..count).each {
  sf1, sf2 = gets.split(/\s+/)
  f1 = sf1.to_i
  f2 = sf2.to_i

  table[f1][f2] = true
  table[f2][f1] = true
}

win = false
(1..5).each { |i|
  know = 0
  unknown = 0
  (1..5).each { |j|
    if (i != j)
      if (table[i][j] == true)
        know += 1
      else
        unknown += 1
      end
    end
  }

  if (know >= 3 or unknown >= 3)
    win = true
    break
  end
}

if (win)
  puts "WIN"
else
  puts "FAIL"
end


y=gets.split.collect {|i| i.to_i}
n=y[0]
x=y[1]
y=y[2]

clone = (y/100.00*n).ceil - x
puts (clone<0 ? 0 : clone)


n,m = gets.split(" ").map(&:to_i)
intersections = [n , m].min
puts intersections % 2 != 0 ? "Akshat" : "Malvika"


n=gets.to_i
n.times{
    x=gets.to_i
    ok=false
    0.step(x, 3) {|a| 0.step(x, 7) {|b| ok=true if a+b==x}}
    puts (ok ? "YES" : "NO")
}


n = gets.to_i

while n.to_s.chars.map(&:to_i).inject(:+) % 4 != 0 do
  n += 1
end

puts n


s = gets.chomp
i = 0
s1 = 'CODEFORCES'
while s[i] == s1[i]
  i += 1
end
j = 0
while s[-j - 1] == s1[-j - 1]
  j += 1
end
if j + i >= s1.length
  print 'YES'
else
  print 'NO'
end


#!/usr/bin/ruby

# Get one number at a time
N = STDIN.gets.to_i

# 0 win = 1 person
# 1 win = 2 ppl
# 2 wins = 3 ppl
# 3 wins = 3 + 2 = 5 ppl

# needed[x] = the minimum number of ppl needed to get x wins.
# needed[x] = needed[x-1] + needed[x-2]
needed = [1, 2, 3]

if N == 2
	puts 1
	exit
end

if N == 3
	puts 2
	exit
end

ans = 3
while true
	needed[ans % 3] = needed[(ans - 1) % 3] + needed[(ans - 2) % 3]
	if(needed[ans % 3] > N)
		break
	end
	ans += 1
end

puts (ans - 1)


n = gets.split(/ /).map { |i| i.to_i }.sort
if (n[2] < n[1] + n[0]) or (n[3] < n[2] + n[1])
  puts "TRIANGLE"
elsif (n[2] == n[1] + n[0]) or (n[3] == n[2] + n[1])
  puts "SEGMENT"
else
  puts "IMPOSSIBLE"
end


max_coin = gets.to_i
while max_coin != 1
  puts max_coin
  num = 2
  while max_coin % num != 0
    num = num + 1
    num = max_coin if num > max_coin / 2
  end
  max_coin = max_coin / num
end
puts 1


n, x, y = gets.split.map &:to_i
n /= 2
puts [x, y].all?{|_| _ == n || _ == n + 1} ? :NO : :YES


# coding: utf-8

n = gets.to_i

# 5 days work, 2 days off
# min: begin with first work day (monday)
# max: begin with first off day (saturday)
min = (n / 7) * 2
min += 1 if n%7 == 6 # saturday
max = 2 + ( (n-2)/7 )*2
max += 1 if (n-2)%7 == 6 # saturday
puts "#{min} #{max}"


s = gets.to_i
a = 1
ans = 0
while a < s
    ans += 1
    a *= 100
end
puts ans


String.class_eval do
  ROD = %w[
    O-|-OOOO O-|O-OOO O-|OO-OO O-|OOO-O O-|OOOO-
    -O|-OOOO -O|O-OOO -O|OO-OO -O|OOO-O -O|OOOO-
  ]
  def soroban
    self.each_char{|s|puts ROD[s.to_i]}
  end
end

gets.chop.reverse.soroban


# -*- mode: ruby -*-
# vi: set ft=ruby :

arr = gets.split.map{|x| x.to_i}.sort
puts arr[-1]-arr[0]


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

include Math
require 'prime'
require 'bigdecimal'
require 'bigdecimal/math'
require 'matrix'
require 'pp'
require 'benchmark'

Inf = 1.0 / 0.0
def pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end
class String;def to_a;split("");end;
             def to_ia(d = " ");split(d).map(&:to_i);end;end
def getia(*arg);gets.to_ia(*arg);end
def geti;gets.to_i;end
class Object;def dcopy;Marshal.load(Marshal.dump(self));end;end

def f
  n, bx = *getia
  ts = getia
  res = 0
  for x in ts
    res *= bx
    res += x
  end
  return res
end

a = f
b = f
if a == b
  puts "="
elsif a < b
  puts "<"
else
  puts ">"
end


n=gets.chomp.to_i
s=gets.chomp

if s=='0'
  puts 0
else
  puts '1'+'0'*s.count('0')
end


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

n, m = gets.split.map &:to_i
def f(n, i, m)
  if i == n
    return i.to_s
  elsif m > 2 ** (n - i - 1)
    return f(n, i + 1, m - 2 ** (n - i - 1)) + i.to_s
  else
    return i.to_s + f(n, i + 1, m)
  end
end
puts f(n, 1, m).each_char.to_a.join(" ")


k, n, s, p = gets.split.map(&:to_f)
res = ((n/s).ceil * k / p).ceil
puts res.to_i


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


ts = gets.chomp.split.map(&:to_i)
hs = Hash.new(0)

ts.each do |t|
  hs[t] += 1
end

s = ts.inject(&:+)
ans = s
hs.each do |k, v|
  ans = [ans, s - k * [v, 3].min ].min if 2 <= v
end

puts ans


l, r = gets.split(' ').map(&:to_i)
cnt = 0
for x in 0..(Math.log(r, 2)+1)
	for y in 0..(Math.log(r,3)+1)
		a = (2**x)*(3**y)
		if a >= l and a <= r
			cnt += 1
		end
	end
end
print String(cnt)


def fact (n)
  mul = 1
  for i in 1..n
    mul *= i
  end
  return mul
end

a,b = gets.chomp.split

a = a.to_i
b = b.to_i

if a<b
  puts fact (a)
else
  puts fact (b)
end


n,m=gets.split.map &:to_i
puts n==1 ? 1 : 2*m<=n ? m+1 : m-1


k = gets.to_i - 1
(1..100).each do |d|
    all = d * 9 * 10**(d-1)
    if k >= all
        k -= all
    else
        num = 10**(d-1) + k/d
        puts num.to_s[k%d]
        exit
    end
end


n,m,a,b=gets.split.map &:to_i
p [n*a,n/m*b+n%m*a,n/m*b+b].min


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

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

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

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


n, p, k = gets.split.map &:to_i
ans = []
ans << '<<' if p - k > 1
(p - k).upto(p - 1) do |i|
    ans << i if i > 0
end
ans << "(#{p})"
(p + 1).upto(p + k) do |i|
    ans << i if i <= n
end
ans << '>>' if p + k < n
puts ans.join ' '


n, m = gets.split.map{ |i| i.to_i }
$a = []
def doit(n)
	return if n.to_s.size > 10
	($a << n) if n > 0
	doit(n * 10 + 4)
	doit(n * 10 + 7)
end
doit(0)
c, r = n - 1, 0
$a = $a.sort
$a.each { |i|
	if i >= n
		if i < m
			r += (i - c) * i
		else
			r += (m - c) * i
			break
		end
		c = i
	end
}
puts r


def run
  n = $stdin.readline.to_i
  a = $stdin.readline.split(" ").map(&:to_i)

  sum = a.reduce(&:+)
  max = 0
  for i in 0..n
    for j in i..n
      gain = a[i..j].count(0) - a[i..j].count(1)
      max = [gain, max].max
    end
  end

  puts max == 0 ? sum - 1 : sum + max
end

run if $0 == __FILE__


#ebony = ARGV[0].to_i
#ivory = ARGV[1].to_i
#shield = ARGV[2].to_i

#ebony = gets.to_i
#ivory = gets.to_i
#shield = gets.to_i

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

for i in 0..nums[2] / nums[0]
	for j in 0..nums[2] / nums[1]
		if i * nums[0] + j * nums[1] == nums[2]
			puts "Yes"
			exit
		end
	end
end

puts "No"


number, exp = gets.chomp.split('e')
lead, trail = number.split('.')
exp = exp.to_i
zeroString = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" * 100
ans = lead[0...(lead.length)]

if lead == "0" && trail == "0"
	puts '0'
	exit
end

dotPoint = exp + 1
pointInserted = false

if dotPoint < (1 + trail.length)
	ans << trail[0...(dotPoint - 1)]

	if trail[(dotPoint - 1)...(trail.length)] != "0"
		ans << "."
		ans << trail[(dotPoint - 1)...(trail.length)]
		pointInserted = true
	end
else
	ans << trail
	restZeros = dotPoint - (trail.length + 1)
	ans << zeroString[0...restZeros]
end

cutPoint = 0
if lead == "0" && pointInserted
	notDone = true
	for i in 0...(ans.length)
		if ans[i] == "." && ans[i - 1] == "0"
			cutPoint = i - 1
			notDone = false
			break
		end
	end

	if cutPoint == 0 && notDone
		for i in 0...(ans.length)
			if ans[i] != "0"
				cutPoint = i
				break
			end
		end
	end
elsif lead == "0" && !pointInserted
	for i in 0...(ans.length)
		if ans[i] != "0"
			cutPoint = i
			break
		end
	end
end

ans = ans[cutPoint...(ans.length)] if lead == "0"

puts ans


x, y, *rest = STDIN.read.split(" ")
puts (1..16).inject {
  |acc, b|
  [acc, (x.to_i(b) + y.to_i(b)).to_s(b).length].max
}


require "date"

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

da = Date.new(a[0], a[1], a[2])
db = Date.new(b[0], b[1], b[2])

puts (db - da).abs.to_i


n = gets.chomp
digits = gets.chomp.chars.map(&:to_i)
table = {
  1 => 4, 2 => 5, 3 => 6,
  4 => 7, 5 => 8, 6 => 9,
  8 => 0
}
table2 = {
  1 => 2, 4 => 5, 7 => 8,
  2 => 3, 5 => 6, 8 => 9
}

up = digits.map { |d| table[d] }.any?(&:nil?)
down = digits.map { |d| table.invert[d] }.any?(&:nil?)
right = digits.map { |d| table2[d] }.any?(&:nil?)
left = digits.map { |d| table2.invert[d] }.any?(&:nil?)

puts (up && down && right && left) ? "YES" : "NO"


n = gets.to_i
a = gets.split.map(&:to_i)
a.sort!
ans = 0
(n/2).times do |i|
  ans += a[2*i+1] - a[2*i]
end
p ans


bookCode = gets.chomp
targetLength = bookCode.length + 1
dangerous = []

(0...targetLength).each do |iterNum|
	('a'..'z').each do |ch|
		start = if iterNum == 0
			""
		else
			bookCode[0..iterNum - 1]
		end

		middle = ch.to_s
		final = bookCode[iterNum..(bookCode.length - 1)]

		dangerous << (start << middle << final)
	end
end

dangerous.uniq!
puts dangerous.length


require 'prime'
def find_minimum(row, col, factor, n)
  res = (0..n).collect {|i|
    [row * (factor**i), col * (factor**(n-i))]
  }.select{ |pair|
    pair[0] <= pair[1]
  }.min_by { |pair|
    pair[1] - pair[0]
  }
  res.nil? ? [1, row*col*(factor**n)] : res
end

def minimum_pair(row, col, primes)
  if primes.size == 1
    return find_minimum(row, col, primes[0][0], primes[0][1])
  else
    prime = primes[0][0]
    n = primes[0][1]
    rest = primes[1..-1]
    minimum = 10000000
    minimum_result = nil
    (0..n).collect { |i|
      minimum_pair(row*(prime**i), col*(prime**(n-i)), rest)
    }.select{ |pair|
      pair[0] <= pair[1]
    }.min_by { |pair|
      pair[1] - pair[0]
    }
  end
end

def solve(n)
  return [1,1] if n == 1
  return [0,0] if n == 0
  primes = n.prime_division
  minimum_pair(1, 1, primes)
end

puts solve(STDIN.read.to_i).join(" ")


n,k = gets.split.map(&:to_i)
a =  n - (k - n*2)
if a < 0
  print 0
else
  print a
end


a = gets.split
l = a[0].to_i; r = a[1].to_i;
count = l==0?1:0
now = 3
for i in 2..65
	for j in 0..i-2
		t = now^(1<<j)
		count += (t>=l && t<=r) ? 1:0
	end
	now = now<<1|1

end
print count


n=gets.to_i.abs
if n==0 then puts "0";exit end

i=1
k=0
loop do
        k+=i
        if k>=n && (k-n)%2==0 then break end
        i+=1
end

puts i


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

if (k == n) || (k == 0)
	print "0 0"
else
	print 1, " ", (n + 2) / 3 <= k ? n - k : 2 * k
end


n = gets.to_i
puts n * (n - 3) - (n - 4)


need = STDIN.readline.to_i - 10
if need == 10
  puts 15
elsif need >= 1 and need <= 11
  puts 4
else
  puts 0
end


require 'matrix'

a1, b1, c1 = gets.split.map { |v| Rational(v.to_i) }
a2, b2, c2 = gets.split.map { |v| Rational(v.to_i) }
a3, b3, c3 = [0, 0, 0].map { |v| Rational(v) }

rank_a = Matrix[ [a1, b1], [a2, b2] ].rank
rank_b = Matrix[ [a1, b1, c1], [a2, b2, c2], [a3, b3, c3] ].rank

result = 0

if rank_a == rank_b
  result = rank_a == 2 ? 1 : -1;
end

puts result


require "set"

def f(n)
	s = (n+1).to_s
	s[...(s =~ /0+$/)].to_i
end

n = gets.to_i
s = Set.new
while !s.include?(n)
	s.add(n)
	n = f(n)
end
p s.size


# Codeforces My Practice
# author: Leonardone @ NEETSDKASU

s = gets.strip

ans = s.each_char.reduce(0) do |a, c|
    case c
    when 'a','i','e','o','u','1','3','5','7','9'
        a + 1
    else
        a
    end
end

p ans


a = gets.to_i

a=a+1

if a==1
    puts 0
elsif a%2!=0
    puts a
else
    puts a/2
end


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


row1 = []
row2 = []
row3 = []
input1 = gets.chomp.split
input2 = gets.chomp.split
input3 = gets.chomp.split
for i in 0...3
    row1[i] = Integer(input1[i])
    row2[i] = Integer(input2[i])
    row3[i] = Integer(input3[i])
end

x = 0
y = 0
z = 0
y = (row1[1] + row1[2] + row3[0] + row3[1] - row2[0] - row2[2]) / 2
z = y - (row3[0] + row3[1] - row2[0] - row2[2])
x = y + row2[0] + row2[2] - row1[1] - row1[2]

row1[0] = x
row2[1] = y
row3[2] = z
print row1.join(" ")
puts
print row2.join(" ")
puts
print row3.join(" ")
puts


n = gets.chomp.to_i
high = Math.log10(n).to_i;
res = 2**(high + 1)
until n <= 0 do
	dig = n/10**high
	if dig == 7 then
		res += 2**high
	end
	n -= dig*10**high
	high -= 1
end
puts res - 1


a=gets.chomp.to_i
trig=0
1.upto(31623) do |i|
temp=a-(i*(i+1))/2
break if temp<=0
if (Math.sqrt(1+8*temp)/2.0-0.5)%1==0
trig=1
break
end
end

if trig == 0
puts "NO"
else
puts "YES"
end


n,a,b = gets.split.map(&:to_i)
k = 2
ans = 1
while (a-1)/k != (b-1)/k
  k *= 2
  ans += 1
end
puts k == n ? "Final!" : ans


def calc(s)
	s[0],s[1]=s[1],s[0] if s[0]=='X'
	s[1],s[3]=s[3],s[1] if s[1]=='X'
	s[2],s[3]=s[3],s[2] if s[2]=='X'
	a=s
end
a=["ABCX", "CABX", "BCAX"]
x=gets.chop+gets.chop
y=gets.chop+gets.chop
ans=0
ans+=1 if a.include?calc(x)
ans+=1 if a.include?calc(y)
puts ans%2==0 ? "YES" : "NO"


n = gets
s = gets

1000.times{s = s.sub(/([aeiouy])[aeiouy]/,'\1')}

puts s


n, k = gets.split.map(&:to_f)
puts (2*n/k).ceil + (5*n/k).ceil + (8*n/k).ceil


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

$a2 = []
$b2 = []

$n.times do |i|
  $a2[i] = i == $n - 1 ? (a[0] + l - a[i]) : a[i + 1] - a[i]
  $b2[i] = i == $n - 1 ? (b[0] + l - b[i]) : b[i + 1] - b[i]
end

def magic
  $n.times do
    return true if $a2 == $b2
    tmp = $a2.shift()
    $a2 << tmp
  end
  return false
end

puts magic ? 'YES' : 'NO'


n,m=gets.split.map &:to_i;x=gets.to_i-1
p [n,m].min>2*x ?[n+m-4*x-2,1].max: 0


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'
      next if iv.to_i > P
      next if jv.to_i > P
      next if kv.to_i > P
      ans = [ans,iv.to_i+jv.to_i+kv.to_i].max

      #p [iv,jv,kv,ans]

  end
end

puts ans


k,a,b=gets.split.map(&:to_i)
if (a%k!=0&&b<k) || (b%k!=0&&a<k)
	p -1
else
	p a/k+b/k
end


# Fitting Arts

input = Array.new
for i in 0..2
	input.push (gets.chomp.split(' ').map(&:to_i)) ###
end

out = "NO"
for i in 0..1
	for j in 0..1
		for k in 0..1
			if input[1][i] + input[2][j] <= input[0][k]
				if [input[1][(i+1)%2] , input[2][(j+1)%2]].max <= input[0][(k+1)%2]
					out = "YES"
				end
			end
		end
	end
end

puts out


n, a1, a2, min, c = [], [], [], 99999, ''
(0..7).each do |_|
  n << gets.chomp.split('')
end
(0..7).each do |i|
  arr, a1, a2 = [], [], []
  (0..7).each do |j|
    arr << n[j][i]
  end
  x = Array.new arr
  y = Array.new arr
  x.uniq!.delete('.')
  y.reverse!.uniq!.delete('.')
  a1 = x
  a2 = y
  if a1.size == 0
  	next
  elsif a1.size == 1
  	d = a1.first == 'W' ? arr.index('W') : arr.reverse.index('B')
  	if (d < min) || (d == min && a1.first == 'W')
  		min = d
  		c = a1.first
  	end
  else
		if a1.first == 'W' && arr.index('W') <= min
			min = arr.index('W')
			c = 'W'
		end
		if a2.first == 'B' && arr.reverse.index('B') < min
			min = arr.reverse.index('B')
			c = 'B'
		end
  end
end
puts c == 'W' ? 'A' : 'B'


r, c, n, k = gets.split.map(&:to_i)
pos = n.times.map{gets.split.map(&:to_i)}

photos = 0
for top in 1..r do
  for left in 1..c do
    for down in top..r do
      for right in left..c do
        alts = pos.count do |x, y|
          x >= top && x <= down && y >= left && y <= right
        end
        photos += 1 if alts >= k
      end
    end
  end
end
puts photos


exhibit = gets.rstrip

arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

count = 0
current_index = 0
exhibit.split('').each do |char|
  index = arr.find_index do |l|
    l == char
  end

  distance_index = (index - current_index).abs

  if distance_index > 13
    if index < current_index
      count += index + 26 - current_index
    else
      count += current_index + 26 - index
    end
  else
    count += distance_index
  end
  current_index = index
end

puts count


a = gets.to_i
b = gets.to_i
diff = (a-b).abs
m = diff/2
ans = 0
m.times do |i|
  ans += (i+1)*2
end
ans += diff/2 + 1 if diff % 2 == 1
puts ans


n, k = gets.split.map(&:to_i)
case (n - k)%(2*k)
when 0..k-1
  puts "YES"
when k..2*k-1
  puts "NO"
end


a,b,r = gets.split.map(&:to_i)
puts (r*2<=b and r*2<=a) ? "First" : "Second"


a = gets.chomp.split(//)
c = a[0]
m = 1
r = 1
i = 1
while i < a.size
	if a[i] == c
		m += 1
	else
		m = 1
		r += 1
	end
	if m > 5
		m = 1
		r += 1
	end
	c = a[i]
	i += 1
end
puts r


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


n, s = gets.chomp.split.map &:to_i

def differ(x)
    string = x.to_s
    for i in 0...string.length
        x -= string[i].to_i
    end
    return x
end

l = 1
r = n
m = -1
while l <= r do
    m = (l + r) / 2
    if differ(m) < s
        l = m + 1
    else
        r = m - 1
    end
end
if differ(m) >= s
    amount = n - m + 1
else
    amount = n - m
end
puts amount


n = gets.to_i

aa = (1..n/2).to_a

aa.delete_if {|a| a.gcd(n-a) != 1}
r = aa.max

puts "#{r} #{n-r}"


#!ruby -parprime
p=Prime
eval'$_=p.each(%d).reject{|i|!p.each(i).each_cons(2).any?{|q,r|i==q+r+1}}.size>=%d?"YES": :NO'%$F


arr = readline.split(' ').collect(&:to_i)
n = arr[0]
arr = arr[1..-1]
a = [arr[0] + arr[1], arr[0] + arr[2], arr[1] + arr[3], arr[2] + arr[3]]
d = a.max - a.min
c = [0, n - d].max
puts c * n


n,m = gets.split.map(&:to_i)
c =0
for i in(1..n)
  if m%i==0 && m/i<=n
     c += 1
  end
end
puts c


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>9500
  n-=9500
  st = 924284
elsif (n>8953)
  n -= 8953
  st = 900000
elsif (n>8000)
  n -= 8000
  st = 758672
elsif n>6900
  n -= 6900
  st = 708404
elsif n>6700
  n-=6700
  st = 700000
elsif (n>5000)
  n -= 5000
  st = 328814
elsif (n>4200)
  n -= 4200
  st = 197262
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, b = gets.chomp.split(" ").map(&:to_i)

val = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]

num = Hash.new(0)

(a..b).each do |n|
  while n > 0
    num[n%10] += 1
    n /= 10
  end
end

res = 0

key = (0..9).to_a

key.zip(val).each do |i, j|
  res += num[i] * j
end

puts res


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

def calc( a, b, c )
	aux = a * ( b / gcd( a,b) )
	return aux * ( c / gcd( aux, c ) )
end

def max( a, b )
	return a > b ? a : b
end

n = gets.chomp.to_i
ans = [1,2,6]

if n < 4
	puts ans[ n-1 ]
else
	ans = calc( n, n-1, n-2 )
	for i in ( max( n-50,1 )..n )
		for j in ( max( n-50,1 )..n )
			for k in ( max( n-50,1 )..n )
				ans = max( ans, calc( i, j, k ) )
			end
		end
	end

	puts ans
end


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+f(x,y,-1,1)+f(x,y,1,1)
else

	ans=4+f(x,y,-1,1)+f(x,y,1,-1)
end
puts ans


n, m = readline.chomp.split(' ').map(&:to_i)
hs = readline.chomp.split('')
vs = readline.chomp.split('')

ok = n.times.all? { |sy|
  m.times.all? { |sx|
    # puts "=== #{sx} #{sy}"
    moves = [[sx, sy]]
    reach = {}
    while !moves.empty?
      nx, ny = moves.pop
      reach[[nx, ny]] = true
      nexts = [[0, -1], [1, 0], [0, 1], [-1, 0]].select { |dx, dy|
        (hs[ny] == '>' && dx == 1) ||
        (hs[ny] == '<' && dx == -1) ||
        (vs[nx] == 'v' && dy == 1) ||
        (vs[nx] == '^' && dy == -1)
      }.map { |dx, dy|
        [nx + dx, ny + dy]
      }.select { |x, y|
        x >= 0 && x < m && y >= 0 && y < n && !reach[[x, y]]
      }
      # puts "Nexts for #{nx},#{ny}: #{nexts}"
      moves += nexts
    end

    reach.size == n * m
  }
}

puts ok ? "YES" : "NO"


n = gets.to_i
ans = 0
n.times do |i|
  gets.split.map(&:to_i).each_with_index do |e, j|
    if i == n / 2 || j == n /2 || i == j || i + j == n - 1
      ans += e
    end
  end
end
puts ans


def is_lucky(s)
        while s>0
                return false if s%10!=7 && s%10!=4
                s/=10
        end
        return true
end
def fact(k)
        return 1 if k<=0
        return fact(k-1)*k
end
def lucky(now)
        res=[]
        return [] if now>1000000000
        return [now]+lucky(now*10+4)+lucky(now*10+7)
end
n,k=gets.split.map(&:to_i)
k-=1
remain=[]

def solve(n,remain,k)
        return [] if n==0
        n.times{|i|
                if k<fact(n-1)*(i+1) then
                        ans=[remain[i]]
                        remain.delete_at(i)
                        ans+=solve(n-1,remain,k-fact(n-1)*i)
                        return ans
                end
        }
end
if n<=30 then
        n.times{|i|
                remain.push(i+1)
        }
        if fact(n)<=k then
                puts -1
                exit
        end
        res=solve(n,remain,k)
        ans=0
        n.times{|i|
                ans+=1 if is_lucky(i+1) && is_lucky(res[i])
        }
        p ans
else
        (n-29).upto(n){|i|
                remain.push(i)
        }
        res=solve(30,remain,k)
        ans=0
        (n-29).upto(n){|i|
                ans+=1 if is_lucky(res[i-n+29]) && is_lucky(i)
        }
        lucky_number=lucky(0)
        lucky_number.shift
        lucky_number.each{|i|
                ans+=1 if i<n-29
        }
        p ans
end


s = gets.strip
s.insert(s.index("p")+1, "://")
i = s.index("ru")
if s[i-1] == '/' then
  i += 2
  while s[i, 2] != "ru" do
    i += 1
  end
end
s.insert(i, ".")
i += 3
if i < s.length
  s.insert(i, "/")
end
print s


#!/usr/bin/ruby
include Math

a, b = gets.split().map(&:to_i)
x, y, z = gets.split().map(&:to_i)
aa = 2*x + y
bb = 3*z + y
puts [aa-a, 0].max + [bb-b, 0].max


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


a1, a2 = gets.chomp.split(" ").map(&:to_i)
n = 0
while true
  if a1 < a2 then
    a1 += 1
    a2 -= 2
  else
    a1 -= 2
    a2 += 1
  end
  if a1 > 0 and a2 > 0 then
    n += 1
  elsif a1 == 0 or a2 == 0 then
    n += 1
    puts n
    exit
  else
    puts n
    exit
  end
end


n=gets.to_i
p=n
m=0
while n>=10
	m+=1
	n/=10
end
#puts "m="+m.to_s
#puts "n="+n.to_s
puts (n+1)*(10**m)-p


t,s,x=gets.split.map{|e| e.to_i}
if (x-t)%s==0 && (x-t)/s>=0 then
	puts "YES"
elsif (x-t-1)%s==0 && (x-t-1)/s>0 then
	puts "YES"
else
	puts "NO"
end


class Algorithm
  NUM_PLAYERS, PLAYER_ID = 3, %w(F M S ?)

  def solve(combinations)
    PLAYER_ID[find_winner(combinations.map { |v| v.strip.to_sym })]
  end

  private

  def find_winner(combinations)
    combinations.each_with_index do |combo, index|
      ok = true
      combinations.each_with_index do |c, i|
        next if i == index
        ok = (ok and beat?(combo, c))
      end
      return index if ok
    end
    return -1
  end

  def beat?(first, second)
    return true if first == :paper and second == :rock
    return true if first == :rock and second == :scissors
    return true if first == :scissors and second == :paper
    return false
  end
end

combos = []
Algorithm::NUM_PLAYERS.times { combos.push gets }

puts Algorithm.new.solve(combos)


n,k = gets.chomp.split(" ").map(&:to_i)
ans = 0
if 2*n-1 >= k
  m = k-1 <= n ? 1 : k-n
  ans += k/2-m+1
  ans -= 1 if k.even?
end
puts ans


n = gets.chomp.to_i
xs = []
ys = []
n.times do
  x, y = gets.chomp.split.map(&:to_i)
  xs << x
  ys << y
end

xs.sort!.uniq!
ys.sort!.uniq!

s = (xs[-1] - xs[0]) * (ys[-1] - ys[0])

puts (0 < s ? s : -1)


#不是闰年，就是说：2月份是28天
number=gets.chomp.split
m=number[0].to_i
d=number[1].to_i
month=[0,31,28,31,30,31,30,31,31,30,31,30,31]
#puts month[m]
column=(month[m]-(8-d))/7
if (month[m]-(8-d))%7 > 0
	column+=1
end
puts column+1


n = gets.to_i
k = 0;
for i in 1..n-1
  if((n%i) == 0)
    k+=1;
  end
end
p k


n=gets.to_i
s=gets.to_s
p=s.chomp.split("")
x=1;
10000.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[i].to_i == 0
      break
    end
    j+=1
  end
end
puts dp[y]


n, m = gets.split.map(&:to_i)
if m < n
  lo, hi, n = 0, 10**18, (n - m) * 2
  while lo < hi do
    i = (lo + hi) / 2
    if i * (i + 1) < n
      lo = i + 1
    else
      hi = i
    end
  end
  p lo + m
else
  p n
end


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


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

mod = (a + b) % n
puts mod.zero? ? n : mod


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

p(if m == 0; 1 else [m, n - m].min end)


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

ar = Array.new(m)
ar[r]=0
i = 0
loop do
  i += 1
  r = (a*r+b)%m
  if ar[r]
    puts i - ar[r]
    break
  end
  ar[r] = i
end


n = gets.to_i
a = gets.split.map{|x| x = x.to_i}
a.uniq!
a.sort!
if(a.length <= 3)
	if(a.length == 3)
		if((a[1]-a[0]).abs == (a[1]-a[2]).abs)

			print((a[1]-a[0]).abs)
		else
			print(-1)
		end
	elsif (a.length == 2)
		#for fucking minimum
		if((a[1]-a[0]).abs % 2 == 0)
			print((a[1]-a[0]).abs / 2)
		else
			print((a[1]-a[0]).abs)
		end
	else
		print(0)
	end
else
	print(-1)
end


n, c = gets.split.map(&:to_i)
ps = gets.split.map(&:to_i)
ts = gets.split.map(&:to_i)

l = 0
lt = 0
r = 0
rt = 0
(0..n-1).each do |i|
  lt += ts[i]
  rt += ts[n-1-i]
  l += [0, ps[i] - lt*c].max
  r += [0, ps[n-1-i] - rt*c].max
end
if l == r
  puts 'Tie'
elsif l > r
  puts 'Limak'
else
  puts 'Radewoosh'
end


# Utility
def factorial(n)
  (1..n).inject(:*) || 1
end

# Input
n, m, k = *(gets.split.map { |i| i.to_i })

# Processing
f = factorial(n - 1) / (factorial(n - 1 - k) * factorial(k))
g = m * (m - 1)**k

result = f * g % 998244353

# Output
puts result


n = gets.to_i
s = gets.chomp

div = []
for i in 1..n
    if n%i==0 && i!=1 && i!=n
        div << i
    end
end

x = div.size

for i in 0..x-1
    tempA = s[0,div[i]].reverse
    tempB = s[div[i],n-1]
    s = tempA+tempB
end
puts s.reverse


k, a, b, v = gets.split.map{|e| e.to_i}

(1..1000).each do |x|
    if (([x + b, x * k].min) * v >= a)
        puts x
        exit
    end
end


def calc(stones)
	threes = (stones / 3).to_i # 1

	remaining = stones - (threes * 3) # 2

	count = threes * 2

	if remaining == 2
		count += 1 #, 1
		count -= 1 # 1, 2 -> 3
		count += 1 #3, 1
	end

	if remaining == 1
		count += 1
	end

	count = 1 if count == 0

	count
end

stones = gets.chomp.to_i

puts calc(stones)


n = gets.to_i
ans = Array.new(n, 0)
ans[0] = 1
(1..n-1).each do |i|
  ans[i] = ans[i-1] + 4 * i
end
puts ans[n-1]


n, k = gets.chomp.split(" ").map{|e| e.to_i}

left = 1
right = n
ans = nil
while (left <= right) do
  mid = (left + right) / 2
  tmp = mid
  sum = 0

  while (tmp > 0) do
    sum += tmp
    tmp /= k
  end

  if sum < n
    left = mid + 1
  else
    right = mid - 1
    ans = mid
  end
end

puts ans


n = gets.to_i
t = Math.sqrt(n).floor
if (n - (t ** 2) == 0)
  puts t * 2
else
  if (n - (t ** 2) <= t)
    puts t * 2 + 1
  else
    puts (t + 1) * 2
  end
end


#!/Users/mike/.rvm/rubies/ruby-1.9.3-p327/bin/ruby

redNum, blueNum = STDIN.readline.split.collect {|s| s.to_i}

def simulate(red, redCount, blueCount, petyaMove)
	petya, vasya = 0, 0
	while redCount+blueCount > 0
		wasRed = red
		if petyaMove and red and redCount > 0
			red = true
		elsif petyaMove and !red and blueCount > 0
			red = false
		elsif !petyaMove and red and blueCount > 0
			red = false
		elsif !petyaMove and !red and redCount > 0
			red = true
		elsif redCount > 0
			red = true
		else
			red = false
		end
		if red
			redCount -= 1
			petya += 1 if wasRed
			vasya += 1 if !wasRed
		else
			blueCount -= 1
			vasya += 1 if wasRed
			petya += 1 if !wasRed
		end
		petyaMove = !petyaMove
	end
	return [petya, vasya]
end

petya0, vasya0 = simulate(true, redNum-1, blueNum, false)
petya1, vasya1 = simulate(false, redNum, blueNum-1, false)

if (petya0 > petya1)
	puts '%s %s' % [petya0, vasya0]
else (petya1 > petya0)
	puts '%s %s' % [petya1, vasya1]
end


class Matrix2x2
  attr_accessor :a, :b, :c, :d
  def initialize(a=0,b=0,c=0,d=0)
    @a = a
    @b = b
    @c = c
    @d = d
  end
end

def mult(x, y)
  z = Matrix2x2.new
  z.a = (x.a*y.a + x.b*y.c)%1000000007
  z.b = (x.a*y.b + x.b*y.d)%1000000007
  z.c = (x.c*y.a + x.d*y.c)%1000000007
  z.d = (x.c*y.b + x.d*y.d)%1000000007
  z
end

def expt(a, p)
  if p == 1
    a.clone
  elsif p % 2 == 1
    t = expt(a, p-1)
    mult(a, t)
  else
    t = expt(a, p/2)
    mult(t, t)
  end
end

n = gets.to_i
if n == 0
  puts 0
else
  a = Matrix2x2.new(0, 3, 1, 2)
  an = expt(a, n)
  puts an.a
end


a=[]
4.times do
a << gets.chomp.split("")
end
ans=0
0.upto(2) do |i|
0.upto(2) do |j|
w=0
b=0
if a[i][j]=="#"
b+=1
else
w+=1
end
if a[i+1][j]=="#"
b+=1
else
w+=1
end
if a[i][j+1]=="#"
b+=1
else
w+=1
end
if a[i+1][j+1]=="#"
b+=1
else
w+=1
end
if w>2 || b>2
ans=1
end
break if ans==1
end
break if ans==1
end
if ans==1
puts "YES"
else
puts "NO"
end


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
answer = [answer, letters.size].max
puts answer


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

puts n - [n_arr[n-1], c_arr[n-1], s_arr[n-1]].max


for i in 0..3*10**7
end
print gets.split.map(&:to_i).slice(1..-1).sort.join(' ')


s = gets
res = 0
for i in 0..s.length
    case
      when i + 4 < s.length && s[i] + s[i + 1] + s[i + 2] + s[i + 3] + s[i + 4] == "Danil" then res += 1
      when i + 4 < s.length && s[i] + s[i + 1] + s[i + 2] + s[i + 3] + s[i + 4] == "Slava" then res += 1
      when i + 3 < s.length && s[i] + s[i + 1] + s[i + 2] + s[i + 3] == "Olya" then res += 1
      when i + 2 < s.length && s[i] + s[i + 1] + s[i + 2] == "Ann" then res += 1
      when i + 5 < s.length && s[i] + s[i + 1] + s[i + 2] + s[i + 3] + s[i + 4] + s[i + 5] == "Nikita" then res += 1
    end
end
if res == 1
  puts "YES"
else
  puts "NO"
end


n, m = gets.split.map(&:to_i)
xs = gets.split.map(&:to_i)
ys = gets.split.map(&:to_i)
ans = []
xs.each do |x|
  ys.each do |y|
    if x == y
      ans << x
      break
    end
  end
end
puts ans.join(" ")


#! /usr/bin/env ruby

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

n -= 1

ans = 0

if n > 0
    ans += [a, b].min
    n -= 1
end

ans += [a, b, c].min * n
n -= n

puts ans


n, h, m = gets.split.map &:to_i
a = [h]*(n + 1)
m.times do
  l, r, x = gets.split.map &:to_i
  (l..r).each { |i| a[i] = [a[i], x].min }
end
puts a[1..-1].inject(0) { |s, x| s + x*x }


n,m,k=gets.split.map(&:to_i)
ans=0
MOD=1000000007
if k==1 || k>n then
        ans=1
        n.times{ ans=(ans*m)%MOD}
        p ans
elsif k==n
        t=(k+1)/2
        ans=1
        t.times{ans=ans*m%MOD}
        p ans
elsif k % 2==0 then
        p m
elsif k % 2==1
        p m*m
end


#!/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, c, d], [b, c, d], [c, a, b], [d, a, b] ].each do |x, a, b|
	next if intersects(x, a) or intersects(x, b)
	if includes(a, x)
		next if includes(x, b)
		next if not includes(b, x)
	end
	count += 1
end
puts count


k,r=gets.split.map &:to_i
(1..10).each{|i|t=k*i%10;(p i;exit)if t==0||t==r}


s, k = gets.split
d = {false => 0, true => -k.to_i}
s.reverse.chars { |c| d[c == '0'] += d[true].zero? ? 0 : 1 }
p d[true] == 0 ? d[false] : s.size - 1


def isinside(ax, ay)
  half = $a/2.0
  return false if ax - half >= $x || ax + half <= $x
  return false if ay - half >= $y || ay + half <= $y
  return true
end
$a,$x,$y = gets.chomp.split(/ /).map!{|x| x.to_i}
ret = -1
if $y < $a
  ret = 1 if isinside(0, $a*0.5)
else
  tr = ($y - $a).divmod($a*2)[0]
  st = 3*tr + 2
  sty = (2*tr+1.5)*$a
  ret = st if isinside(0, sty)
  ret = st+1 if isinside(-$a*0.5, sty+$a)
  ret = st+2 if isinside( $a*0.5, sty+$a)
end
puts ret.to_s


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

ans = 99999999999999999999999
a.each_with_index do |x, i|
  i += 1
  if x <= k && x != 0
    ans = [ans, (i-m).abs].min
  end
end

p ans*10


END { load __FILE__ unless $stdin.eof? }

@m = 10**9 + 7

def f(n, k)
	return 1 if k == 0
	$table[k] ||= (
	if n.even?
		(2.pow(n * k.pred, @m) + 2.pow(n - 1, @m).pred * f(n, k - 1)) % @m
	else
		(2.pow(n - 1, @m).succ * f(n, k - 1)) % @m
	end
	)
end

t = gets.to_i
t.times do
	n, k = gets.split.map!(&:to_i)
	$table = Array.new(k + 1)
	k.times { |kk| f(n, kk) }
	p f(n, k)
end


def f(n, a, b)
    if a > b
        f(n, b, a)
    else
        (b / n).floor + 1 - (a / n).ceil
    end
end

x, y, a, b, c, d = gets.split.map(&:to_r)
p [f(2 * x, a + b, c + d), f(2 * y, a - b, c - d)].max


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

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

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

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

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

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

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

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

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

def sum(a) a.inject(:+) end

require 'prime'
n = inp[0]

d = Prime.prime_division(n)
if(d.size == 1 )
  p d[0][0]
else
  p 1
end


n = gets.to_i
puts n - gets.chomp.scan(/RU|UR/).size


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

ans    = 0
offset = 0
flag   = false
m      = 2 ** h

while m != 1
  m /= 2
  if flag ^ (offset + m >= n)
    ans += 1
    flag = !flag
    offset += m unless flag
  else
    ans += 2 * m
    offset += m unless flag
  end
end

puts ans


BEGIN{
	def main()
		n, m, v = gets.split.map(&:to_i)
		a = gets.split.map(&:to_i)
		a.sort!
		l = a.length - 1
		cc = 0
		while v < m do
			if a[l] <= 1 then
				puts -1
				return
			end
			v -= 1
			v += a[l]
			cc += 1
			l -= 1
			break if l == -1
		end
		if v < m then
			puts -1
		else
			puts cc
		end
	end
		main()
}


a=gets.chomp.to_i-1
b=gets.chomp
res=0
if a!=0
array=[]
i=0
if (a+1)%2==0
k=0
loop do
break if a-k<=0
l=0
	loop do
		break if l>k
		array[i]=b[l..(a-k+l)]
		i+=1
		l+=1
	end
		k+=2
end
else
k=1
loop do
break if a-k<=0
l=0
	loop do
		break if l>k
		array[i]=b[l..(a-k+l)]
		i+=1
		l+=1
	end
		k+=2
end
end
i=0
loop do
	temp=array[i].split('')
	if
		temp.count('U')==temp.count('D') && temp.count('L')==temp.count('R')
		res+=1
		end
	i+=1
	break if i==array.length
end
end
puts res


inp = gets.chomp.split.map(&:to_i);
n = inp.shift;
dn = Array.new(n + 1, -1e9);
dn[0] = 0;

(0..n).each do |x|
  for y in inp do
      dn[x] = [dn[x], 1 + dn[x - y]].max if (x - y >= 0);
  end
end

puts dn[n];


n = gets.chomp.to_i
row = gets.chomp.split('')

ans = []
bSize = 0

for i in (0...row.length)
	bSize += 1 if row[i] == 'B'

	if row[i] == 'W'
		if bSize > 0
			ans << bSize
		end

		bSize = 0
	end
end

if bSize > 0
	ans << bSize
end

if ans.length > 0
	puts ans.length
ans.each do |a|
	print "#{a} "
end
puts ''

else
	puts 0
end


def solve()
    line = gets
    x = line.split(' ')[0].to_i
    y = line.split(' ')[1].to_i
    z = line.split(' ')[2].to_i

    if (x > y + z)
        return "+"
    end
    if (y > x + z)
        return  "-"
    end
    if (z == 0)
        if ( x > y)
            return "+"
        end
        if ( y > x)
            return "-"
        end
        return "0"
    end

    return "?"
end

puts solve()


n = gets.strip.to_i

def kek(n)
    min = n
    i = n - 1
    while i > 0
        if n%i!=0
            k = kek(n-i)
            min = n-i if n-i < min
            return min if min == 1
        end
        i-=1
    end
    min
end

min = n
i = n - 1
while i > 0
    if n%i!=0
        k = kek(n-i)
        min = n-i if n-i < min
        break if min == 1
    end
    i-=1
end

p min


a=gets.chomp.to_i
b=gets.chomp.split(" ")
lb=b.delete_at(0).to_i
0.upto(lb-1) {|i|b[i]=b[i].to_i}
c=gets.chomp.split(" ")
lc=c.delete_at(0).to_i
0.upto(lc-1) {|i|c[i]=c[i].to_i}
turn=0
loop do
if b[0]>c[0]
b<<c.delete_at(0)
b<<b.delete_at(0)
turn+=1
lb+=1
lc-=1
else
c<<b.delete_at(0)
c<<c.delete_at(0)
turn+=1
lc+=1
lb-=1
end
if lb==0
puts "#{turn} 2"
break
elsif lc==0
puts "#{turn} 1"
break
elsif turn>106
puts "-1"
break
end
end


a = gets.strip.to_i
b = gets.strip.to_i
c = gets.strip.to_i
ans = 0
while a >= 1 && b >= 2 && c >= 4
    ans += 7
    a -= 1
    b -= 2
    c -= 4
end
puts ans


a, b, c, n = gets.split.map(&:to_i)
x = a + b - c
puts (a >= c && b >= c && n - x > 0) ? (n - x) : -1


class Solver
	def main
		n = gets.to_i
		a = gets.to_i
		b = gets.to_i
		c = gets.to_i
		if b - c >= a
			puts n / a
		else
			ans = 0
			if n >= c
				ans += (n - c) / (b - c)
				n -= (n - c) / (b - c) * (b - c)
			end
			ans += n / [a, b].min
			puts ans
		end
	end
end
Solver.new.main


n =gets.chomp.to_i
sum = 0
arr = gets.chomp.split(" ").collect! {|x| x.to_i}
arr.each { |k|
	sum += k
}

#puts sum
sum = sum%(n+1)
if sum == 0
#sum = 1
end
p=5
q = 1
while q<=5
	if (sum + q)%(n+1)==1
		p -= 1
	end
	q += 1
end

puts p


n,k,t=gets.split.map(&:to_i)
if t >= k && t <= n
    puts k
elsif t < k
    puts t
else
    puts n + k - t
end


n,m,k=gets.split.map(&:to_i)
if k>n+m-2
  p -1
else
  r=[n/(k+1)*m,m/(k+1)*n]
  r << n/(k-m+2) if k-m+2 >0
  r << m/(k-n+2) if k-n+2 >0
  p r.max
end


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

ans = 0
while b != 0
	ans += a / b
	a -= a / b * b
	a, b = b, a
end

puts ans


n, m = gets.chomp.split(' ').map { |x| x.to_i }

buttons = gets.chomp.split(' ').map { |x| x.to_i }

n.times do |i|

	buttons.each do |button|

		if button <= i+1
			print button," "
			break
		end
	end
end

puts


def step(a,op)
	a.permutation.map{|v| ( ( [ (op=="+") ? v[0]+v[1] : v[0]*v[1] ] ) + ( (v.drop(2)) ? v.drop(2) : [] ) ).sort }.uniq
end

a=gets.split.map{|v| v.to_i}
oper=gets.split
res=[]
step(a,oper[0]).each do |v|
  step(v,oper[1]).each do |t|
	res+=step(t,oper[2])
  end
end
puts res.min


n, t = readline.split(' ').collect(&:to_i)
arr = Array.new(n) {|i| Array.new(i + 1, 0.0)}
t.times do
  arr[0][0] += 1.0
  0.upto(n - 2) do |i|
    0.upto(i) do |j|
      if arr[i][j] > 1.0
        left = arr[i][j] - 1.0
        arr[i + 1][j] += left / 2.0
        arr[i + 1][j + 1] += left / 2.0
        arr[i][j] = 1.0
      end
    end
  end
end

puts arr.inject(0) {|sum, a|
  sum + a.count {|num| num >= 1.0}
}


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

def solve(a,b,c)
  ar = Array.new(1000001) { 0 }
  ans = 0
  mod = 1073741824
  for i in 1..a do
    for j in 1..b do
      for k in 1..c do
        d = i*j*k
        ans += ret_div(ar, d, mod)
      end
    end
  end
  puts ans
end

def ret_div(ar, el, mod)
  return ar[el] if ar[el]>0
  j = 1
  div = 0
  while j*j<=el do
    if el%j == 0
      div+=2
    end
    j+=1

  end
  div-=1 if (j-1)*(j-1) == el
  ar[el] = div % mod
  ar[el]
end

solve a, b ,c


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

gcd = x.gcd(y)
x /= gcd
y /= gcd
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


n, d, $x = gets.split(' ').map(&:to_i)
a, b = [], []
def gnx
  $x = ($x *37 + 10007) % 1000000007
end
  # initAB
(1..n).each{|i| a << i}
(1..n).each do |i|
  tgt = gnx % i
  a[i-1], a[tgt] = a[tgt], a[i-1]
end
(1..n).each{|i| b << ( i<=d ? 1 : 0 )}
(1..n).each do |i|
  tgt = gnx % i
  b[i-1], b[tgt] = b[tgt], b[i-1]
end
  # end of initAB

aid = (0...n).sort_by{|i| -a[i]}
bid = (0...n).each_with_object([]){|i,a| a << i if b[i]>0}
c = (0...n).map{|_| -1}
nf, maxc = n, n-1
aid.each do |ai|
  break if nf == 0
  bid.each do |bi|
    id = ai + bi
    break if id > maxc
    if c[id] < 0
      c[id] = a[ai]
      nf -= 1
    end
    maxc -= 1 until (maxc < 0) || (c[maxc] < 0)
  end
end
c.each{|i| puts ( i>0 ? i : 0 )}


#!/usr/bin/env ruby

def main
  input = STDIN.readlines
  k = input[0].to_i
  l = input[1].to_i

  x = 1
  im = 0
  while x < l
    x *= k
    im += 1
  end

  if x == l
    puts "YES"
    puts im - 1
  else
    puts "NO"
  end
end

if __FILE__ == $0
  main
end


s = gets.chomp.split(' ')
n = s[0].to_i
b = s[1].to_i
p = s[2].to_i

x = 0
y = n*p
while(n/2>0)
        x = x + (b*2+1)*(n/2)
        n = n/2 + n%2
end
puts x.to_s + " " + y.to_s


n = gets.strip.to_i
s = gets.strip
i = count_s = count_f = 0
ans = "YES"
while i < n - 1
    first = s[i]
    second = s[i + 1]
    count_f += 1 if first == 'F' && second == 'S'
    count_s += 1 if first == 'S' && second == 'F'
    i += 1
end
ans = "NO" if count_s <= count_f
puts ans


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

def palindrome(n, a, b, dancers)
  min_cost = 0
  len = dancers.size

  min_cost = [a, b].min if (len % 2 == 1) and (dancers[len/2] == 2)
  (0..len/2-1).each do |idx|
    if dancers[idx] != dancers[len-idx-1] and dancers[idx] != 2 and dancers[len-idx-1] != 2
      p -1
      return
    elsif (dancers[idx] == 2) and (dancers[len-idx-1] == 2)
      min_cost += [a, b].min * 2
    elsif (dancers[idx] == 2) or (dancers[len-idx-1] == 2)
      min_cost += ([dancers[idx], dancers[len-idx-1]].min == 0) ? a : b
    end
  end
  p min_cost
end

palindrome(n, a, b, dancers)


X,Y,x,y,a,b=$<.read.split.map &:to_i
x-=X
y-=Y
puts x%a==0&&y%b==0&&x/a%2==y/b%2 ? :YES : :NO


def stop
	puts "stop"
	sleep(2)
end
def readarr
	gets.split.map{|i| i.to_i}
end
def writearr(foo)
	puts foo.map{|i| i.to_s}.join(" ")
end
n,k,m = readarr
arr0 = []
k.times{arr0 << 0}
arr = []
(n+1).times{arr << arr0.clone}
arr[0][0]=1
x = 1
1.upto(n) do |i|
	0.upto(9) do |j|
		y = x * j % k
		cnt = 0
		arr[i-1].each do |elem|
			s = (y+cnt)%k
			arr[i][s]  += elem
			arr[i][s]  = arr[i][s]  % m
			cnt+=1
		end
	end
	arr[i][0] = 1
	x *= 10
	x = x % k
end
def norm(array,n)
	return 10**n-array[n].inject(0){|sum,i| sum+i}
end
puts (norm(arr,n)-norm(arr,n-1))%m
#sleep(300)


s=gets.chomp
n=s.size
res=[0]*4

"RBYG".chars.each_with_index{|c, i|
    k=s.index c
    j=k
    res[i]+=1 if s[j]==?! while 0<=j-=4
    j=k
    res[i]+=1 if s[j]==?! while n>j+=4
}

puts res*' '


MOD = 1000000007

def pow(x,n)
  if n == 0
    1
  elsif n % 2 == 0
    pow(x*x%MOD, n/2) % MOD
  else
    x * pow(x, n-1) % MOD
  end
end

n = gets.to_i
sum = 1
1.upto(n) do |i|
  sum *= i
  sum %= MOD
end
minus = pow(2,n-1)
sum -= minus
sum %= MOD
puts sum


n = gets.to_i
arr = gets.split(' ').collect(&:to_i).sort
arr_used = Array.new(n)

arr.each_with_index do |elem_1, idx_1|
	arr.each_with_index do |elem_2, idx_2|
		next unless arr_used[idx_2].nil?
		next unless elem_2 % elem_1 == 0 && elem_1 <= elem_2
		arr_used[idx_2] = idx_1
	end
end

p arr_used.uniq.count


x,y = gets.split ' '
x = x.to_f
y = y.to_f
if x == y
  puts '='
elsif y/x < Math.log10(y)/Math.log10(x)
  puts '<'
elsif y/x > Math.log10(y)/Math.log10(x)
  puts '>'
else
  puts '='
end


gets
a=gets.split.map(&:to_i)
p a[-1]^a.max


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

h=Hash.new(0)
s.each{|c| h[c]+=1}
ok=true
h.each_value do |v|
  ok&=v<=k
end

puts ok ? 'YES' : 'NO'