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

n = input[0]
k = input[1]

contador = 0

pontuacao.each do |x|
    if (x >= pontuacao[k] && x > 0)
        contador+=1
    end
end

puts contador


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

c = number[0..5]

puts c


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

a.delete_if{|e| e < a[m]}.delete(0)
puts a.length


#!/usr/bin/env ruby
# n, k = %w(8 5).map(&:to_i)
# points = %w(10 9 8 7 7 7 5 5).map(&:to_i)
n, k =gets.chomp.split(" ").map(&:to_i)
points = gets.chomp.split(" ").map(&:to_i)

ans = 0

min_points= points[k-1]
if min_points > 0
  ans = k
  (k..n).each do |x|
    break if points[x] < min_points
    ans += 1
  end
else
  points.each do |x|
    break if x == 0
    ans += 1
  end

end

puts ans


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

scores = scores.select { |a| a>0 }
grouped_scores = scores.group_by{|item| item}
grouped_peoples = grouped_scores.values.map(&:length)

total = 0
begin
  total += grouped_peoples.shift
end while total < k

puts total


n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
hoge=n
k.times do |i|
    tmp = a[i] / 4
    if hoge >= tmp
        hoge -= tmp
        a[i] %= 4
    else
        a[i] -= hoge * 4
        hoge = 0
    end
    if hoge == 0
        break
    end
end
lim = 2 * n + hoge
lim2 = hoge
k.times do |i|
    tmp = (a[i] + 1) / 2
    if lim >= tmp
        lim -= tmp
        a[i] %= 2
    else
        a[i] -= 2 * lim
        lim2 -= a[i]
        a[i] = 0
    end
end
k.times do |i|
    lim2 -= a[i]
end
puts lim2 >= 0 ? "YES" : "NO"


def func(n,i)
    s = ""
    while n>=1 do
        s += (n % i).to_s
        n /= i
    end
    s = s.reverse
    res = s.to_i
    return res
end

n = gets.to_i
i = 2
sum = 0
while i<n do
    s1 = func(n,i)
    s = s1.to_s
    s.each_char do |k|
        sum += k.to_i
    end
    i += 1
end
puts "#{sum}/#{(n-2)}"


inp = gets.to_i

i = 2
total = 0
while i < inp do
  x = inp
  while x > 0 do
    total = total + (x % i)
    x = x/i
  end

  i = i+1
end

puts "#{total}/#{inp-2}"


#=================================
require 'prime'
include Math

if ARGV.include?("ONLINE_JUDGE")
	$stdin = File.open("./input.txt", "r")
	ARGV.delete("ONLINE_JUDGE")
end
#=================================

n=gets.to_i
dif=n-n/2
while n>1
	print "#{n} "
	n-=dif
end
puts 1


#=================================
require 'prime'
include Math

if ARGV.include?("ONLINE_JUDGE")
	$stdin = File.open("./input.txt", "r")
	ARGV.delete("ONLINE_JUDGE")
end
#=================================

n=gets.to_i
dp=Array.new(n+1, false)

dp[n]=true
(n-1).downto(1){|i|
	(i+1).upto(n){|j|
		if dp[j]
			if j%i==0
				dp[i]=true
			end

			break
		end
	}
}

n.downto(1){|index|
	if dp[index]
		print "#{index} "
	end
}


mball,vball=0,0
a,b,c,d=gets.to_i,gets.to_i,gets.to_i,gets.to_i
3*a/10.0>a-(a/250.0)*c ? mball=3*a/10.0 : mball=a-(a/250.0)*c

3*b/10.0>b-(b/250.0)*d ? vball=3*b/10.0 : vball=b-(b/250.0)*d

if mball>vball
  puts 'Misha'
elsif vball>mball
  puts 'Vasya'
else
  puts 'Tie'
end


mball,vball=0,0
s=gets.to_s
s1=s.split(' ')
a=s1[0]
b=s1[1]
c=s1[2]
d=s1[3]
3*a/10.0>a-(a/250.0)*c ? mball=3*a/10.0 : mball=a-(a/250.0)*c

3*b/10.0>b-(b/250.0)*d ? vball=3*b/10.0 : vball=b-(b/250.0)*d

if mball>vball
  puts 'Misha'
elsif vball>mball
  puts 'Vasya'
else
  puts 'Tie'
end


a=gets.to_i
b=gets.to_i
c=gets.to_i
d=gets.to_i
misha=[3*a/10,a-(a*c/250)].max
vasya=[3*b/10,b-(b*d/250)].max

if misha>vasya
  puts "Misha"
elsif vasya>misha
  puts "Vasya"
else
  puts "Tie"
end


def search(idx, num, ss)
	wk = num + $s[idx].to_i
	if idx == $s.length-1
		ret = 0
		if wk % 2 == 0
			$same = 1 if ss + (wk/2).to_s == $s
			ret = 1
		else
			$same = 1 if ss + (wk/2).to_s == $s
			$same = 1 if ss + (wk/2+1).to_s == $s
			ret = 2
		end
		return ret
	end
	if wk % 2 == 0
		nn = wk / 2
		if $ar[idx][nn] == nil
			$ar[idx][nn] = search(idx+1, nn, ss + nn.to_s)
		end
		return $ar[idx][nn]
	else
		nn = wk / 2
		if $ar[idx][nn] == nil
			$ar[idx][nn] = search(idx+1, nn, ss + nn.to_s)
		end
		if $ar[idx][nn+1] == nil
			$ar[idx][nn+1] = search(idx+1, nn+1, ss + (nn+1).to_s)
		end
		return $ar[idx][nn]+$ar[idx][nn+1]
	end
end

$same = 0
$s = gets.chomp
$ar = Array.new($s.length).map!{|x| Array.new($s.length)}
ret = 0
for i in 0..9
	ret += search(1,i,i.to_s)
end
puts ret-$same


def search(idx, num, ss)
	wk = num + $s[idx].to_i
	if idx == $s.length-1
		ret = 0
		if wk % 2 == 0
			$same = 1 if ss + (wk/2).to_s == $s
			ret = 1
		else
			$same = 1 if ss + (wk/2).to_s == $s
			$same = 1 if ss + (wk/2+1).to_s == $s
			ret = 2
		end
		return ret
	end
	if wk % 2 == 0
		nn = wk / 2
		if $ar[idx][nn] == nil
			$ar[idx][nn] = search(idx+1, nn, ss + nn.to_s)
		end
		return $ar[idx][nn]
	else
		nn = wk / 2
		if $ar[idx][nn] == nil
			$ar[idx][nn] = search(idx+1, nn, ss + nn.to_s)
		end
		if $ar[idx][nn+1] == nil
			$ar[idx][nn+1] = search(idx+1, nn+1, ss + (nn+1).to_s)
		end
		return $ar[idx][nn]+$ar[idx][nn+1]
	end
end

$same = 0
$s = gets.chomp
$ar = Array.new($s.length).map!{|x| Array.new($s.length)}
if $s.length == 1
	puts 9
else
	ret = 0
	for i in 0..9
		ret += search(1,i,i.to_s)
	end
	puts ret-$same
end


#!/usr/bin/ruby
include Math

INF = 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f

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

best = []
best[0] = INF
res = INF
for i in 1..n
	best[i] = [2*best[i-1], cost[i-1]].min
	if 2**(i-1) > v
		res = [res, best[i]].min
	end
end
for i in n+1..70
	best[i] = 2*best[i-1]
	if 2**(i-1) > v
		res = [res, best[i]].min
	end
end

ans = INF
vv = v
while (vv & -vv) != vv
	temp = 0
	j = 0
	v = vv
	while v > 0
		j += 1
		if v % 2 == 1
			temp += best[j]
		end
		v /= 2
	end
	ans = [ans, temp].min
	vv += vv & -vv
end

puts [res, ans].min


inp = gets.chomp
if inp.lenth.odd?
  p 'CHAT WITH HER!'
else
  p 'IGNORE HIM!'
end


puts ['CHAT WITH HER!','IGNORE HIM!'][gets.strip.to_a.unique.join('').length%2]


s = gets.split(//).uniq!
puts (s.length % 2 == 1) ? "CHAT WITH HER!"  : "IGNORE HIM!"


year = {
    January: 1,
    February: 2,
    March: 3,
    April: 4,
    May: 5,
    June: 6,
    July: 7,
    August: 8,
    September: 9,
    October: 10,
    November: 11,
    December: 12
}

month = gets.chomp
k = gets.to_i
actual = year[month.to_sym]
puts actual
release = (actual+k) % 12
if release == 0
    puts "December"
else
    puts year.key(release)
end


words = gets.chomp.split(" ")

words.map{|w| w.each_char.count{|c| ('A'..'Z').cover?(c)}}.max


def cmmdc a,b
  return a if a==b
  return cmmdc([a,b].min,(a-b).abs)
end

n,m,x,y,a,b = gets.chomp.split(" ").collect {|x| x.to_i}

cmm = cmmdc(a,b)
a /= cmm
b /= cmm

p = n/a
q = m/b

min = [p,q].min

p = min * a
q = min * b

delta_center_x = p/2.0
delta_center_y = q/2.0

c1x =  delta_center_x
c1y =  delta_center_y

c2x = n - delta_center_x
c2y = m - delta_center_y

rx = 0
ry = 0

if x<c1x and y<c1y
  rx = c1x
  ry = c1y
end

if x<c1x and y>c2y
  rx = c1x
  ry = c2y
end

if x>c2x and y<c1y
  rx = c2x
  ry = c1y
end

if x>c2x and y>c2y
  rx = c2x
  ry = c2y
end

if x>=c1x and x<=c2x and y<c1y
  ry = c1y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if x>=c1x and x<=c2x and y>c2y
  ry = c2y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if y>=c1y and y<=c2y and x<c1x
  rx = c1x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>c2x
  rx = c2x
  if (c1y-y).ceil == (c1y-y).floor
    ry = c1y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>=c1x and x<=c2x
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

puts [rx-delta_center_x,ry-delta_center_y,rx+delta_center_x,ry+delta_center_y].map {|x| x.to_i}.join(' ')


def cmmdc a,b
  return a if a==b
  return cmmdc([a,b].min,(a-b).abs)
end

n,m,x,y,a,b = gets.chomp.split(" ").collect {|x| x.to_i}

cmm = cmmdc(a,b)
a /= cmm
b /= cmm

p = n/a
q = m/b

min = [p,q].min

p = min * a
q = min * b

delta_center_x = p/2.0
delta_center_y = q/2.0

c1x =  delta_center_x
c1y =  delta_center_y

c2x = n - delta_center_x
c2y = m - delta_center_y

rx = 0
ry = 0

if x<c1x and y<c1y
  rx = c1x
  ry = c1y
end

if x<c1x and y>c2y
  rx = c1x
  ry = c2y
end

if x>c2x and y<c1y
  rx = c2x
  ry = c1y
end

if x>c2x and y>c2y
  rx = c2x
  ry = c2y
end

if x>=c1x and x<=c2x and y<c1y
  ry = c1y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if x>=c1x and x<=c2x and y>c2y
  ry = c2y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if y>=c1y and y<=c2y and x<c1x
  rx = c1x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>c2x
  rx = c2x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>=c1x and x<=c2x
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

puts [rx-delta_center_x,ry-delta_center_y,rx+delta_center_x,ry+delta_center_y].map {|x| x.to_i}.join(' ')


a, b = gets.split.map(&:to_i)
if a == b
  puts 'infinity'
else
  ans = 0
  for i in 1..a
    ans+= 1 if a%i == b
  end
  puts ans
end


a, b = gets.split.map(&:to_i)
if a == b
  puts 'infinity'
elsif a<b
  puts '0'
else
  ans, x = 0, a-b
  for i in 1..Math.sqrt(x)
    ans+= 1 if (a-b)%i == 0 && i>b
    ans+= 1 if (a-b)%i == 0 && (a-b)/i>b
  end
  ans+=1 if (a-b)==i**2 && i>b
  puts ans
end


# https://codeforces.com/problemset/problem/339/A

sum = gets.chomp.split(" ").to_i
sum.sort!

ans = ""
sum.each do |n|
    ans << "#{n}+"
end
ans = ans.slice(0, ans.length - 1)

puts ans


def sort(s)
  s.split('+').sort!.join('+')
end

# f = File.new('test1.txt', 'r')
while s = f.gets
  puts sort s.chomp
end
# f.close


Teams = {"BERLAND" => 0}
def teamn(name)
  if Teams[name].nil?
    Teams[name]=Teams.size
  end
  Teams[name]
end
game = [1,0,0,0]
point = [3,0,0,0]
diff = [0,0,0,0]
earn = [0,0,0,0]
5.times do |i|
  input = gets.split(" ")
  teama = teamn(input[0])
  teamb = teamn(input[1])
  score = input[2].split(":").map{|x| x.to_i}
  if score[0]>score[1]
    point[teama] += 3
  elsif score[0]==score[1]
    point[teama] += 1
    point[teamb] += 1
  else
    point[teamb] += 3
  end
  game[teama] += 1
  game[teamb] += 1
  diff[teama] += score[0] - score[1]
  diff[teamb] += score[1] - score[0]
  earn[teama] += score[0]
  earn[teamb] += score[1]
end
opponent = game.index(2)
uprank = 0
mindiff = []
1.upto(3) do |i|
  uprank += 1 if point[i] > point[0]
  mindiff << (diff[i]-diff[0]) if point[i] == point[0] and i != opponent
  mindiff << (diff[i]-diff[0])/2 if point[i] == point[0] and i == opponent
end
if uprank >= 2
  puts "IMPOSSIBLE"
else
  mindiff = mindiff.sort.reverse
  mindiff = mindiff[1-uprank]
  diff[0] += mindiff
  diff[opponent] -= mindiff
  minearn = []
  1.upto(3) do |i|
    uprank += 1 if point[i] == point[0] and diff[i] > diff[0]
    minearn << i if point[i] == point[0] and diff[i] == diff[0]
  end
  minearn = minearn.sort.reverse
  minearn = minearn[1-uprank]
  minearn = mindiff if (minearn < mindiff)
  puts minearn.to_s + ":" + (minearn-mindiff).to_s
end


Teams = ["BERLAND"]
def teamn(name)
  Teams << name if Teams.index(name).nil?
  Teams.index(name)
end
game = [1,0,0,0]
point = [3,0,0,0]
diff = [0,0,0,0]
earn = [0,0,0,0]
5.times do |i|
  input = gets.split(" ")
  teama = teamn(input[0])
  teamb = teamn(input[1])
  score = input[2].split(":").map{|x| x.to_i}
  if score[0]>score[1]
    point[teama] += 3
  elsif score[0]==score[1]
    point[teama] += 1
    point[teamb] += 1
  else
    point[teamb] += 3
  end
  game[teama] += 1
  game[teamb] += 1
  diff[teama] += score[0] - score[1]
  diff[teamb] += score[1] - score[0]
  earn[teama] += score[0]
  earn[teamb] += score[1]
end
opponent = game.index(2)
found = false
fi = nil
fj = nil
1.upto(50) do |i|
  diff[0] += i
  diff[opponent] -= i
  i.upto(50) do |j|
    earn[0] += j
    earn[opponent] += j-i
    rank = (0..3).sort{|a,b| (point[a]<=>point[b])*8+(diff[a]<=>diff[b])*4+(earn[a]<=>earn[b])*2+(Teams[a]<=>Teams[b])}.index(0)
    if rank > 1
      found = true
      fj = j
      break
    end
    earn[0] -= j
    earn[opponent] -= j-1
  end
  if found
    fi = i
    break
  end
  diff[0] -= i
  diff[opponent] += i
end
if found
  puts fi.to_s+":"+(fj-fi).to_s
else
  puts "IMPOSSIBLE"
end


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

def line(v)
	ret=v
	m=$k
	30.times{
		ret+=v/m
		m*=$k
	}
	ret
end

def bin_search(l,r)
	return -1 if l>r
	if l==r
		return line(l)==-1 ? -1 : l
	end
	m=(l+r)/2
	lines=line(m)
	if lines<$n
		return bin_search(m+1,r)
	else
		bs=bin_search(l,m-1)
		return bs==-1 ? m : bs
	end
end

p bin_search(1,10**9)


#! ruby
# Try Codeforces Round 326 div2
# author: Leonardone @ NEETSDKASU

n = gets.to_i

root = (n ** 0.5).floor

f = [true] * (root + 1)

lovely = n

2.upto(root) do |i|
    next if !f[i]
    if lovely % i == 0
        ii = i * i
        while lovely % ii == 0
            lovely = lovely.div ii
        end
        if lovely % i != 0
            lovely *= i
        end
    end
    j = 2
    while j * i <= n
        f[j * i] = false
        j += 1
    end
end

puts lovely


n=gets.chomp.to_i

b=1
if n!=1
	i=2
	while i<=n
		if n%i==0
			b*=i
			while n%i==0
				n/=i
			end
		else
			i+=1
		end
	end
end

puts b


require 'prime'

n = gets.to_i
k = n
q=(Math.sqrt n).floor
f = Array.new(q+1, true)
prime = Array.new
result = 1
(2..q).each do |i|
  if i > k
    break
  end
  if Prime.prime?(i)
    if k % i == 0
      result *= i
      while (k % i ==0)
        k /= i
      end
    end
  end
end
if k != 1
  result *= k
end

puts result


n = gets.chomp.to_i

ps = [2]

for q in 3..(n**0.5).floor
  isp = true
  ps.each do |p|
    if q % p == 0
      isp = false
      break
    end
  end
  ps << q if isp
end

ans = 1
while 1 < n
  ps.each do |p|
    ans *= p if n % p == 0
    while n % p == 0
      n /= p
    end
  end
  if ps[-1] < n
    ans *= n
    n = 1
  end
end

puts ans


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

sum = 0
a.each_cons(2) do |x,y|
  if x+y == 5
    print "Infinite\n"
    exit
  else
    sum += x+y
  end
end

print "Finite\n#{sum}"


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

sum = 0
a.each_cons(2) do |x,y|
  if x+y == 5
    print "Infinite\n"
    exit
  else
    sum += x+y
  end
end

a.each_cons(3) { |x,_,z| if x==3 and y==2 then sum -= 1 end }

print "Finite\n#{sum}"


#!/usr/bin/ruby
n,m=gets.split.map(&:to_i)
r=0
x=0
gets.split.map(&:to_i).each{|e|
	if x<e
		r+=1
		x=n
	end
	x-=e
}
p r


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

i=0
ans=0
while xs.empty? ==false
	m1=m
	while xs.empty? == false
		x=xs.shift
		if m1>=x then
			m1-=x
		else
			xs.push(x)
			break
		end
	end
	ans+=1
end
puts ans


mn=gets().chomp
mn=mn.split(" ")
n=mn[0].to_i
m=mn[1].to_i
line=gets().chomp
line=line.split(" ")
sum=0
for i in 0...n
  sum+=line[i].to_i
end
if sum%m==0
  puts (sum/n)
else
  puts (sum/n)+1
end


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


TEST_MODE = ARGV[0] == 't'

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

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

# actual codes here

def solve(input, output)
  v = parse_int_array(input[0])
  if (v[0] - v[1]).abs <= 1
    output << "YES"
  else
    output << "NO"
  end
end

tests do
  # test cases
  t("2 3", "YES")
  t("3 1", "NO")
end

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


a, b = gets.strip.split.map(&:to_i)
if a > 0 && b > 0 && (a == b || (a - b).abs == 1)
    puts "YES"
else
    puts "NO"
end


a,b=gets.split.map{|e| e.to_i}
if (b-a).abs<=1 then
	puts "YES"
else
	puts "NO"
end


code=gets.strip.scan(/.{10}/)

hs={}
10.times{|i|
	hs[gets.strip] = i
}

puts code.inspect

code.each{|digit|
	print hs[digit]
}


c=gets.strip.scan(/.{10}/)
h={}
10.times{|i|h[gets.strip]=i}
c.each{|d|p h[d]}


#         ... LETS HACK THE WORLD ...
#... KEEP CALM AND SAY LE ROI EST MORT IS HERE ...
#       ... EL-BADRY BECOME A LEGEND ...
x,z = gets.chomp.split.map(&:to_i)
puts(x*z)/2


m, n = gets.chomp.split.map{ |x| x.to_s }
puts m*n/2


m = gets.chomp.to_i
n = gets.chomp.to_i

puts (n*m)/2


x = gets.chomp.split()
x.downcase()
ans=""
for i in x
	if i=="a" or i=="e" or i=="i" or i=="o" or i=="u" or i=="y"
		ans+=""
	else
		ans+="."+i.downcase()
	end
end
puts ans


x = gets.split("")
x.downcase()
ans=""
for i in x
    if i=="a" or i=="e" or i=="i" or i=="o" or i=="u" or i=="y"
        ans+=""
    else
        ans+="."+i
    end
end
puts ans


vowel = ['a','e','i','o','u','y']
word = gets.chomp
str = ''
word.split("").each do |c|
unless vowel.include? c.downcase
str = str + '.' + c.downcase
end
end
print str
end


a=gets.strip.downcase
a.gsub!(/aeiou/,"")
a.each do |c|
  print "."+c
end


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


a,b,w,x,c=gets.split.map(&:to_i)
h={}
i=0
while true
  if c<=a
    p i
    exit
  end
  if b>=x
    b-=x
  else
    a-=1
    b=w-(x-b)
  end
  break if h[b]!= nil
  h[b]=[i,a]
  i+=1
  c-=1
end
cl=i-h[b][0]
da=h[b][1]-a
d=(c-a)/cl
a-=da*d
c-=d*cl
i+=d*cl
while c>a
  if b>=x
    b-=x
  else
    a-=1
    b=w-(x-b)
  end
  c-=1
  i+=1
end
i-=2 if d>0
p i


a,b,w,x,c=gets.split.map(&:to_i)
h={}
i=0
while true
  if c<=a
    p i
    exit
  end
  if b>=x
    bb=b-x
  else
    a-=1
    bb=w-(x-b)
  end
  break if h[bb]!= nil
  b=bb
  h[b]=[i,a]
  i+=1
  c-=1
end
cl=i-h[bb][0]
da=h[bb][1]-a
d=(c-a)/(cl-da)-1
a-=da*d
c-=d*cl
i+=d*cl
while c>a
  if b>=x
    b-=x
  else
    a-=1
    b=w-(x-b)
  end
  c-=1
  i+=1
end
p i


require 'byebug'

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


word = gets.chomp.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']
sum = 0
pos = 0
for i in (0..word.size-1)
	if i == 0
		if alphabet.find_index(word[i]) >= 13
			sum += 26 - alphabet.find_index(word[i])
		else
			sum += alphabet.find_index(word[i])
		end
		pos = alphabet.find_index(word[i])
	else
		if (pos - alphabet.find_index(word[i])).abs >= 13
			sum += 26 - (pos - alphabet.find_index(word[i])).abs
		else
			sum += pos - alphabet.find_index(word[i])
		end
		pos = alphabet.find_index(word[i])
	end
end

print sum


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)
p d[0][0]


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

n = inp[0]
(2..((n**0.5).ceil)).each do |d|
  if (n%d ==0)
    p d
    exit
  end
end
p n


#!/usr/bin/ruby
a,b=gets.split.map(&:to_i)
h=1.step(6).group_by{|i|(a-i).abs<=>(b-i).abs}
puts -1.step(1).map{|i|h[i].size}*' '


# your code goes here
cin=$stdin

a,b=cin.read.split(' ')

a=Integer(a)
b=Integer(b)
i=1
res1=0
res2=0
res3=0
while i<7 do
  x=a-i
  y=a-i
  if x<0
    x=x*-1
  end
  if y<0
    y=y*-1
  end
  if x<y
    res1=res1+1
  end
  if x==y
    res2=res2+1
  end
  if y<x
    res3=res3+1
  end
  i=i+1
end

print res1, " ", res2," ",res3,"\n"


n, k = gets.chomp.split(' ').map(&:to_i)
a = gets.chomp.split(' ').map(&:to_i)
puts a.inject(0) {|r, x| x >= [a[k], 1].max ? r += 1 : r}


hq = gets.strip
l = hq.length
s = false

for i in 0...l
  if hq[i] == "H" || hq[i] == "Q" || hq[i] == "9" then s = true end
end

puts if s then "Yes" else "No" end


y = gets.chomp.to_i;
x = gets.chomp;
#a = gets.strip.split(' ').map(&:to_i);

x.reverse!
puts x;


a = gets.to_i
s = gets

divs = []
while 1 < a
  divs << a
  a /= 2
end
divs.reverse!

divs.each {|d|
  s = s[0...d].reverse + s[d..-1]
}

puts s


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}


a = gets.split.map{|i| [i[0].ord - 97, i[1].ord - 49]}
b = Array.new(8){Array.new(8){false}}
c = Array.new(8){Array.new(8){false}}
a[0 .. 2].each{|x,y| b[x][y] = true}
a[0 .. 1].each do |x,y|
	x.pred.downto(0).each{|i| c[i][y] = true; break if b[i][y]}
	x.succ.upto(7).each{|i| c[i][y] = true; break if b[i][y]}
	y.pred.downto(0).each{|j| c[x][j] = true; break if b[x][j]}
	y.succ.upto(7).each{|j| c[x][j] = true; break if b[x][j]}
end
(a[2][0] - 1 .. a[2][0] + 1).each do |i|
	(a[2][1] - 1 .. a[2][1] + 1).each do |j|
		c[i][j] = true if (0 .. 7).include?(i) && (0 .. 7).include?(j) && !b[i][j]
	end
end
(a[3][0] - 1 .. a[3][0] + 1).each do |i|
	(a[3][1] - 1 .. a[3][1] + 1).each do |j|
		if (0 .. 7).include?(i) && (0 .. 7).include?(j) && !c[i][j] then
			puts :OTHER
			exit
		end
	end
end
puts :CHECKMATE


k=gets.chomp.to_i
l=gets.chomp.to_i
m=gets.chomp.to_i
n=gets.chomp.to_i
d=gets.chomp.to_i
ret=0
1.upto(d){|i| if %k==0 || i%l==0 || i%m==0 || i%n==0 || i%d==0 then ret+=1 end}
print(ret,"\n")


input_nums = []
until (input = gets.chomp).empty?
 input_nums.push input.to_i
end

dragons_number = input_nums[-1]
input_nums.delete_at(input_nums.count)
dragons = 1.upto(dragons_number).to_a
input_nums.each do |damage|
  dragons.select!{ |dragon| dragon % damage != 0 }
end
puts "#{dragons_number - dragons.count}"


k = $stdin.readline.to_i
l = $stdin.readline.to_i
m = $stdin.readline.to_i
n = $stdin.readline.to_i
d = $stdin.readline.to_i

a = Array.new(d)
i = 1
while i<=d
    a[i] = 1 if i%k==0 || i%l==0 || i%m==0 || i%n==0
    i+=1
end

s = a.compact.inject(:+)
puts s.nil? : 0 : s


k = gets.chomp.to_i
l = gets.chomp.to_i
m = gets.chomp.to_i
n = gets.chomp.to_i
dragons = gets.chomp.to_i
dragon_arr = []

i = 1
while i <= dragons
	dragon_arr << i
	i += 1
end

count = 0
if k == 1 || l == 1 || m == 1 || n == 1
	puts dragons
else
	x = 1
	i = k
	while i <= dragons
		if dragon_arr.include? i
			dragon_arr.delete(i)
		end
		i = k * x
		x += 1
	end
	x = 1
	i = l
	while i <= dragons
		if dragon_arr.include? i
			dragon_arr.delete(i)
		end
		i = l * x
		x += 1
	end
	i = m
	x = 1
	while i <= dragons
		if dragon_arr.include? i
			dragon_arr.delete(i)
		end
		i = m * x
		x += 1
	end
	i = n
	x = 1
	while i <= dragons
		if dragon_arr.include? i
			dragon_arr.delete(i)
		end
		i = n * x
		x += 1
	end
	count = dragons - dragon_arr.size
	puts count
end


n,*a=gets.split.map &:to_i
a=a.map{|e|e*2}.sort
i=0;s=a.reduce(:+)
while s<n*9
	s+=10-a[i]
	i+=1
end
p i


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

na = a.inject(&:+)
nb = b.inject(&:+)

k = (na/5 + na%5>0?1:0) + (nb/10 + nb%10>0?1:0)

puts (k<=n) ? "YES" : "NO"


coin_count = gets.to_i
coins = gets.split(' ').map(&:to_i)

pockets = 0
coin_counts = Hash.new {|h, k| h[k] = 0}

coins.each do |c|
    coin_count[c] += 1
    pockets = [pockets, coin_count[c]].max
end

puts pockets


n = gets.to_i; ar = gets.split.map &:to_i
puts ar.size - ar.uniq.size


n, a, b, c = gets.chomp.split().map { |x| x.to_i }

n = (-n % 4)

puts case n
    when 0
        0
    when 1
        a
    when 2
        [ 2*a, b ].min
    when 3
        [ 3*a, a+b, c ].min
end


#!/usr/bin/ruby
n,a,b,c=gets.split.map(&:to_i)
if n%4==0
	p 0
elsif n%4==1
	p [a*3,a+b,c].min
elsif n%4==2
	p [a*2,b,c*2].min
elsif n%4==3
	p [a,c*3].min
end


inp = gets.chomp.split(' ')

n=inp[0].to_i

a=inp[1].to_i

b=inp[2].to_i

c=inp[3].to_i

k= 4-n%4 if(n%4!=0)

if (n%4==0)
    puts 0

k=0

end

if(k==1)

    puts a

elsif (k==2)

    puts [2*a,b].min

elsif (k==3)

    puts [3*a,a+b,c].min

end


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

a, b, x, y = read_vector_int
puts [a / x, b / y].min


line = gets.chomp
a,b,width,height = line.split(' ').map!(&:to_f)

a_times = (a/width).floor
b_times = (b/height).floor

puts [a_times, b_times].min


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


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

sol =  n - a - b + c
if sol > 0
	puts sol
else
	puts -1
end


def dsum(given_degree)
    given_degree['1'] + given_degree['2'] + given_degree['3']
end

n = gets.to_i
degree = gets.split(' ')
degree_1 = {:min => degree.first.to_i, :max => degree.last.to_i}
degree = gets.split(' ')
degree_2 = {:min => degree.first.to_i, :max => degree.last.to_i}
degree = gets.split(' ')
degree_3 = {:min => degree.first.to_i, :max => degree.last.to_i}
given_degree = {'1' => degree_1[:min], '2' => degree_2[:min], '3' => degree_3[:min]}
if dsum(given_degree) == n
    puts "#{given_degree['1']} #{given_degree['2']} #{given_degree['3']}"
else
    if n - dsum(given_degree) <= (degree_1[:max] - given_degree['1'])
        given_degree['1'] += n - dsum(given_degree)
    else
        given_degree['1'] = degree_1[:max]
        puts "#{given_degree['2']} #{given_degree['2']} #{given_degree['3']}" if dsum(given_degree) == n
        if n - dsum(given_degree) <= (degree_2[:max] - given_degree['2'])
            given_degree['2'] += n - dsum(given_degree)
        else
            given_degree['2'] = degree_2[:max]
            puts "#{given_degree['1']} #{given_degree['2']} #{given_degree['3']}" if dsum(given_degree) == n
            if n - dsum(given_degree) <= (degree_3[:max] - given_degree['3'])
                given_degree['3'] += n - dsum(given_degree)
            end
        end
    end
end
puts "#{given_degree['1']} #{given_degree['2']} #{given_degree['3']}"


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

c = 0
f = 0
while c < x
  c+=5
  f+=1
end

puts f


=begin

                            Online Ruby Interpreter.
                Code, Compile, Run and Debug Ruby script online.
Write your code in this editor and press "Run" button to execute it.

=end

def test
	input = $stdin.read
	input = input.split("\n")
	chars = ['a','b','c','d','e','f','j','h']
	place1 = [chars.find_index(input[0][0]) ,input[0][1].to_i-1]
	place2 = [chars.find_index(input[1][0]),input[1][1].to_i-1]

	wrong_p = []
	wrong_p.push(place1)
	wrong_p.push(place2)

	for i in 0...8
	   if !wrong_p.include?([place1[0],i])  && i >=0
	       wrong_p.push([place1[0],i])
	   end
	   if !wrong_p.include?([i,place1[0]]) && i >=0
	       wrong_p.push([i,place1[0]])
	   end
	end

	if !wrong_p.include?([place2[0]+2, place2[1]+1]) && place2[0]+2 >=0 && place2[1]+1 >=0
	  wrong_p.push([place2[0]+2, place2[1]+1])
	end
	if !wrong_p.include?([place2[0]+2, place2[1]-1]) && place2[0]+2 >=0 &&  place2[1]-1 >= 0
	wrong_p.push([place2[0]+2, place2[1]-1])
	end
	if !wrong_p.include?([place2[0]-2, place2[1]+1]) && place2[0]-2 >=0 && place2[1]+1 >=0
	wrong_p.push([place2[0]-2, place2[1]+1])
	end
	if !wrong_p.include?([place2[0]-2, place2[1]-1]) && place2[0]-2 >=0 && place2[1]-1 >=0
	wrong_p.push([place2[0]-2, place2[1]-1])
	end
	if !wrong_p.include?([place2[0]+1, place2[1]+2]) && place2[0]+1 >= 0 && place2[1]+2 >= 0
	wrong_p.push([place2[0]+1, place2[1]+2])
	end
	if !wrong_p.include?([place2[0]+1, place2[1]-2]) && place2[0]+1 >=0 &&  place2[1]-2 >= 0
	wrong_p.push([place2[0]+1, place2[1]-2])
	end
	if !wrong_p.include?([place2[0]-1, place2[1]+2]) && place2[0]-1 >=0  && place2[1]+2 >=0
	wrong_p.push([place2[0]-1, place2[1]+2])
	end
	if !wrong_p.include?([place2[0]-1, place2[1]-2]) && place2[0]-1 >= 0 &&  place2[1]-2 >=0
	wrong_p.push([place2[0]-1, place2[1]-2])
	end

    if !wrong_p.include?([place1[0]+1, place1[1]+2]) && place1[0]+1 >= 0 &&  place1[1]+2 >=0
	  wrong_p.push([place1[0]+1, place1[1]+2])
	end
	if !wrong_p.include?([place1[0]+1, place1[1]-2]) && place1[0]+1 >= 0 &&  place1[1]-2 >=0
	  wrong_p.push([place1[0]+1, place1[1]-2])
	end
	if !wrong_p.include?([place1[0]-1, place1[1]+2]) && place1[0]-1 >= 0 &&  place1[1]+2 >=0
	  wrong_p.push([place1[0]-1, place1[1]+2])
	end
    if !wrong_p.include?([place1[0]-1, place1[1]-2]) && place1[0]-1 >= 0 &&  place1[1]-2 >=0
	  wrong_p.push([place1[0]-1, place1[1]-2])
	end
    if !wrong_p.include?([place1[0]+2, place1[1]+1]) && place1[0]+2 >= 0 &&  place1[1]+1 >=0
	  wrong_p.push([place1[0]+2, place1[1]+1])
	end
	if !wrong_p.include?([place1[0]+2, place1[1]-1]) && place1[0]+2 >= 0 &&  place1[1]-1 >=0
	  wrong_p.push([place1[0]+2, place1[1]-1])
	end
	if !wrong_p.include?([place1[0]-2, place1[1]+1]) && place1[0]-2 >= 0 &&  place1[1]+1 >=0
	  wrong_p.push([place1[0]-2, place1[1]+1])
	end
    if !wrong_p.include?([place1[0]-2, place1[1]-1]) && place1[0]-2 >= 0 &&  place1[1]-1 >=0
	  wrong_p.push([place1[0]-2, place1[1]-1])
	end

	count = 0
    for i in 0...8
       for j in 0...8
          if !wrong_p.include?([i,j])
            count = count+1
          end
       end
    end
    puts count
end
test


=begin

                            Online Ruby Interpreter.
                Code, Compile, Run and Debug Ruby script online.
Write your code in this editor and press "Run" button to execute it.

=end

=begin

                            Online Ruby Interpreter.
                Code, Compile, Run and Debug Ruby script online.
Write your code in this editor and press "Run" button to execute it.

=end

def test
	input = $stdin.read
	input = input.split("\n")
	chars = ['a','b','c','d','e','f','j','h']
	place1 = [chars.find_index(input[0][0]) ,input[0][1].to_i-1]
	place2 = [chars.find_index(input[1][0]),input[1][1].to_i-1]

	wrong_p = []
	wrong_p.push(place1)
	wrong_p.push(place2)

	for i in 0...8
	   if !wrong_p.include?([place1[0],i])  && i >=0
	       wrong_p.push([place1[0],i])
	   end
	   if !wrong_p.include?([i,place1[1]]) && i >=0
	       wrong_p.push([i,place1[1]])
	   end
	end

	if !wrong_p.include?([place2[0]+2, place2[1]+1])
	  wrong_p.push([place2[0]+2, place2[1]+1])
	end
	if !wrong_p.include?([place2[0]+2, place2[1]-1])
	wrong_p.push([place2[0]+2, place2[1]-1])
	end
	if !wrong_p.include?([place2[0]-2, place2[1]+1])
	wrong_p.push([place2[0]-2, place2[1]+1])
	end
	if !wrong_p.include?([place2[0]-2, place2[1]-1])
	wrong_p.push([place2[0]-2, place2[1]-1])
	end
	if !wrong_p.include?([place2[0]+1, place2[1]+2])
	wrong_p.push([place2[0]+1, place2[1]+2])
	end
	if !wrong_p.include?([place2[0]+1, place2[1]-2])
	wrong_p.push([place2[0]+1, place2[1]-2])
	end
	if !wrong_p.include?([place2[0]-1, place2[1]+2])
	wrong_p.push([place2[0]-1, place2[1]+2])
	end
	if !wrong_p.include?([place2[0]-1, place2[1]-2])
	wrong_p.push([place2[0]-1, place2[1]-2])
	end

    if !wrong_p.include?([place1[0]+1, place1[1]+2])
	  wrong_p.push([place1[0]+1, place1[1]+2])
	end
	if !wrong_p.include?([place1[0]+1, place1[1]-2])
	  wrong_p.push([place1[0]+1, place1[1]-2])
	end
	if !wrong_p.include?([place1[0]-1, place1[1]+2])
	  wrong_p.push([place1[0]-1, place1[1]+2])
	end
    if !wrong_p.include?([place1[0]-1, place1[1]-2])
	  wrong_p.push([place1[0]-1, place1[1]-2])
	end
    if !wrong_p.include?([place1[0]+2, place1[1]+1])
	  wrong_p.push([place1[0]+2, place1[1]+1])
	end
	if !wrong_p.include?([place1[0]+2, place1[1]-1])
	  wrong_p.push([place1[0]+2, place1[1]-1])
	end
	if !wrong_p.include?([place1[0]-2, place1[1]+1])
	  wrong_p.push([place1[0]-2, place1[1]+1])
	end
    if !wrong_p.include?([place1[0]-2, place1[1]-1])
	  wrong_p.push([place1[0]-2, place1[1]-1])
	end

	count = 0
    for i in 0...8
       for j in 0...8
          if !wrong_p.include?([i,j])
            count = count+1
          end
       end
    end
    puts count
end
test


res=["ABC","ACB","BAC","BCA","CAB","CBA"]
input=[gets.split[0],gets.split[0],gets.split[0]]
res.each{|ans|
        check=1
        input.each{|i|
                if i[1]==">" then
                        a0=0
                        a1=0
                        3.times{|j|
                                if ans[j]==i[0] then
                                        a0=j
                                end
                                if ans[j]==i[2] then
                                        a1=j
                                end
                        }
                        if a0<=a1 then
                                check=0
                        end
                else
                        a0=0
                        a1=0
                        3.times{|j|
                                if ans[j]==i[0] then
                                        a0=j
                                end
                                if ans[j]==i[2] then
                                        a1=j
                                end
                        }
                        if a0>=a1 then
                                check=0
                        end

                end
        }
        if check==1 then
                puts ans
        end

}


#!/usr/bin/ruby
n=gets.to_i
a=gets.split.map(&:to_i)[-2..-1]
if a[-1]==0
	puts :UP
elsif a[-1]==15
	puts :DOWN
elsif n==1
	p -1
else
	puts a[0]<a[1] ? :UP : :DOWN
end


#!/usr/bin/ruby
n=gets.to_i
if n==1
	p -1
else
	a=gets.split.map(&:to_i)[-2..-1]
	puts a==[1,0]||a[0]<a[1] ? :UP : :DOWN
end


old = gets.split(' ')
sp = gets.split(' ')
old.map!{|o| o.to_i}
sp.map!{|s| s.to_i}
pos, neg = 0, 0
old.size.times do |i|
    a = old[i]
    b = sp[i]
    a>b ? neg+=(a-b) : pos+=(b-a)
end
neg = neg/2;
if(neg>=pos)
    puts "Yes"
else
    puts "No"
end


data=gets.chomp.split
col1=[]
data.each do |i|
	col1.push(i.to_i)
end

col2=[]
data1=gets.chomp.split
data1.each do |i|
	col2.push(i.to_i)
end

diff1=0
diff2=0

for i in(0..col1.length-1)
	if col1[i]>=col2[i]
		a=col1[i]-col2[i]
		if a>=2
			diff1+=col1[i]-col2[i]
		end
	else
		diff2+=col2[i]-col1[i]
	end
end

if diff2==0
	puts "Yes"
else
	k=diff1/2
	if k>=diff2
		puts "Yes"
	else
		puts "No"
	end
end


#!/usr/bin/env ruby

have = ARGF.gets.chomp.split(' ').map{ |s| s.to_i }
want = ARGF.gets.chomp.split(' ').map{ |s| s.to_i }

for i in 0..2
    if have[i] < want[i]
        have[i] = 0
        want[i] = want[i] - have[i]
    else
        have[i] = have[i] - want[i]
        want[i] = 0
    end
end

for i in 0..2
    while want[i] > 0 and (have[(i+1) % 3] >= 2 or have[(i+2) % 3] >= 2)
        if have[(i+1) % 3] >= 2
            want[i] -= 1;
            have[(i+1) % 3] -=2
        end
        if have[(i+2) % 3] >= 2
            want[i] -= 1;
            have[(i+2) % 3] -=2
        end
    end
end

if want[0] > 0 or want[1] > 0 or want[2] > 0
    puts "No"
else
    puts "Yes"
end


a,b,c=gets.split.map(&:to_i)
arr=gets.split.map(&:to_i)
counter=0
counter+=(a-arr[0])/2
counter+=(b-arr[1])/2
counter+=(c-arr[2])/2
puts (counter>=0)?"YES":"NO"


count = 0
winner = 0
wagons, sp, cp = gets.chomp.split.map{|s| s.to_i}
h = gets.chomp == "to head"
gets.chomp.each_char do |c|
  count += 1
  if c.to_i == 0
    if sp < cp
      sp -= 1 unless sp == 1
    else
      sp += 1 unless sp == wagons
    end
    if h
      if cp == 1
        h = !h
        cp += 1
      else
        cp -= 1
      end
    else
      if cp == wagons
        h = !h
        cp -= 1
      else
        cp += 1
      end
    end
    if sp == cp
      winner = 1
      break
    end
  else
    if h
      if cp == 1
        h = !h
        cp += 1
      else
        cp -= 1
      end
    else
      if cp == wagons
        h = !h
        cp -= 1
      else
        cp += 1
      end
    end
    if cp == 1
      sp = 2
    elsif cp == wagons
      sp = wagons - 1
    else
      if h
        sp = wagons
      else
        sp = 1
      end
    end
  end
  puts "#{sp} #{cp}"
  raise "invalid position" if sp < 1 || wagons < sp || cp < 1 || wagons < cp
end
if winner == 0
  puts "Stowaway"
else
  puts "Controller #{count}"
end


count = 0
winner = 0
wagons, sp, cp = gets.chomp.split.map{|s| s.to_i}
h = gets[3] == 'h' ? 0 : 1
gets.chomp.each_char do |c|
  count += 1
  if c.to_i == 0
    if sp < cp
      if sp > 1
        sp -= 1
      else
        sp += 1
      end
    else
      if sp < wagons
        sp += 1
      else
        sp -= 1
      end
    end
    if sp == cp
      winner = 1
      break
    end
    if h
      if cp > 1
        cp -= 1
      else
        cp += 1
        h = !h
      end
    else
      if cp < wagons
        cp += 1
      else
        cp -= 1
        h = !h
      end
    end
    if sp == cp
      winner = 1
      break
    end
  else
    if h
      if cp > 0
        cp -= 1
      else
        cp += 1
        h = !h
      end
    else
      if cp < wagons
        cp += 1
      else
        cp -= 1
        h = !h
      end
    end
    sp = cp <= wagons / 2 > ? cp + 1 : cp - 1
  end
  #puts "#{sp} #{cp}"
end
if winner == 0
  puts "Stowaway"
else
  puts "Controller #{count}"
end


count = 0
winner = 0
wagons, sp, cp = gets.chomp.split.map{|s| s.to_i}
h = gets[3] == 'h' ? 0 : 1
gets.chomp.each_char do |c|
  count += 1
  if c.to_i == 0
    if sp < cp
      if sp > 1
        sp -= 1
      else
        sp += 1
      end
    else
      if sp < wagons
        sp += 1
      else
        sp -= 1
      end
    end
    if sp == cp
      winner = 1
      break
    end
    if h
      if cp > 1
        cp -= 1
      else
        cp += 1
        h = !h
      end
    else
      if cp < wagons
        cp += 1
      else
        cp -= 1
        h = !h
      end
    end
    if sp == cp
      winner = 1
      break
    end
  else
    if h
      if cp > 0
        cp -= 1
      else
        cp += 1
        h = !h
      end
    else
      if cp < wagons
        cp += 1
      else
        cp -= 1
        h = !h
      end
    end
    sp = cp <= wagons / 2 ? cp + 1 : cp - 1
  end
  #puts "#{sp} #{cp}"
  raise "error" if sp < 1 || wagons < sp || cp < 1 || wagons < cp
end
if winner == 0
  puts "Stowaway"
else
  puts "Controller #{count}"
end


#!/usr/bin/ruby
x,y=gets.split.map(&:to_i)
a=[x]*3
r=0
while a[2]!=y
	a[2]=[a[1]-a[0]+1,y].max
	a.sort!
	r+=1
end
p r


def printlol(ar)
  for i in 0..(ar.length-2) do
    print ar[i]
    print " "
  end
  print ar.last
  puts ""
end

input = gets.strip.split(' ').map(&:to_i)
x = input[0]
y = input[1]

lol = [x, x, x]
count = 0
while lol[0] != y or lol[1] != y or lol[2] != y do
  asd = lol.dup
  asd.slice!(lol.index(lol.max))
  fark = (asd[0] - asd[1]).abs
  if fark + 1 < y
    asd << [y, ((asd[0] + asd[1]) / 6)].max
  else
    if (fark + 1) >= ((asd[0] + asd[1]) / 6)
      asd << (fark + 1)
    else
      asd << ((asd[0] + asd[1]) / 6)
    end
  end
  lol = asd
  count += 1
  printlol(lol)
end

puts count


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

s = [m,m,m]

ans = 0
while s.min <= n
  s[0] = s[1] + s[2]
  s=s.sort
  ans += 1
end

puts ans


#!/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(" ")


#!/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(" ")


n, m = gets.split.map {|i| i.to_i}
arr = [nil] * n
k = 1 << (n - 1)
p1 = 0
p2 = -1
for i in 1..n
  if m >= k
    m -= k
    arr[p2] = i
    p2 -= 1
  else
    arr[p1] = i
    p1 += 1
  end
  k >>= 1
end
puts arr.join ' '


n,m = gets.chomp.split(" ")
n = n.to_i;
m = m.to_i;
div = m / n;

unless (m % n == 0)
    puts "-1";
end

moves = 0;

while div != 1 && m % n == 0 do
    if div % 2 == 0 then
        moves += 1;
        div /= 2;
    elsif div % 3 == 0 then
        moves += 1;
        div /= 3;
    else
        puts "-1";
        break;
    end
end

if (m % n == 0) then
    puts moves
end


tmp = $stdin.gets.chomp.split(' ')
n = tmp[0].to_i
m = tmp[1].to_i

nn=[]
mm=[]
nc=0
mc=0

while n%2==0
  nc+=1
  n/=2
end

nn.push(nc)
nc=0
while n%3==0
  nc+=1
  n/=3
end
nn.push(nc)

while m%2==0
  mc+=1
  m/=2
end

mm.push(mc)
mc=0
while m%3==0
  mc+=1
  m/=3
end
mm.push(mc)

#puts nn,mm

a1 = (mm[0]-nn[0])
a2 = (mm[1]-nn[1])
if a1<0 || a2<0
  puts "-1"
else
  puts a1+a2
end


x=gets.to_i
y=gets.to_i
if (y/x).to_i==y/x
  puts "-1"
else
  n=y/x
  res=0
  while n!=1
    if n%2==0
      n/=2
      res+=1
    elsif n%3==0
      n/=3
      res+=1
    else
      puts "-1"
      exit
    end
  end
  puts res
end


x=gets.to_i
y=gets.to_i
n=y.to_f/x.to_f
if n.to_i!=n
  puts "-1"
else
  res=0
  while n!=1
    if n%2==0
      n/=2
      res+=1
    elsif n%3==0
      n/=3
      res+=1
    else
      puts "-1"
      exit
    end
  end
  puts res
end


s = gets
if s.scan(/[a-z]/).size >= s.scan(/[A-Z]/).size
	puts s.downcase
else
	puts s.uppercase
end


 a = gets.chomp
 b = a.scan[/(A-Z)/].length
 c = a.scan[/(a-z)/].length
 if b > c
 puts a.upcase!
 else
 puts a.downcase!

 end


MAX_INT = 1<<31

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

class Array
    def diameter
        m = 0
        for i in 0...self.count
            for j in i+1...self.count
                t = (self[i]-self[j]).abs
                if t>m
                    m = t
                end
            end
        end
        m
    end

    def complect_and_count
        self.dup.complect_and_count!
    end

    def complect_and_count!
        a = self.shift
        h = {a => 1}
        k = 1
        while self.size>0
            b = self.shift
            if a != b
                h[a] = k
                h[b] = 1
                k = 0
            end
            a = b
            k += 1
        end
        h[b]=k
        h
    end
end

f_uniq = f.uniq
ff = f.sort!.complect_and_count

# found all with d=0
t_s = ff.collect do |key,value|
    [key]*value
end

# p t_s
t = t_s.dup

loop do

    min_d = MAX_INT
    min_d_count = 0
    min_i = nil
    for i in 0...t.count
        elem = t[i]
        elem_d = elem.diameter
        if elem_d <= min_d && (elem.count>=n || elem.count>=min_d_count)
            min_d = elem_d
            min_d_count = elem.count
            min_i = i
        end
    end

    #puts "min_d=#{min_d}, min_d_count=#{min_d_count}, min_i=#{min_i}"

    if t[min_i].count >= n
        p t[min_i].diameter
        break
    else
        tt = []
        t.each do |t_elem|
            ttt = f_uniq - t_elem
            #p ttt
            ttt.each do |ttt_elem|
                count = ff[ttt_elem]
                tt << t_elem + ([ttt_elem]*count)
            end
        end
        t = tt
        #puts t
    end

end
#t = solve(n,f)
#p t
#puts t[0...n].diameter


n, m = gets.split(' ').map(&:to_i)
a = gets.split(' ').map(&:to_i).sort
ret = 1000
for i in 0...a.size-n+1
  ret = [a[i+3] - a[i], ret].min
end
puts ret


students, puzzles = gets.chomp.split(/ /).map(&:to_i)
pieces = gets.chomp.split(/ /).map(&:to_i)
pieces.sort!
ans = []
min = -1
puzzles.times do |i|
    break if i + students >= pieces.length
    first = pieces[i]
    last = pieces[i + students - 1]
    diff = last - first
    min = diff if min > -1
    min = [min, diff].min
    ans = pieces[i..students]
end
puts ans[students - 1] - ans[0]


n = gets.to_i
phone = gets.split('')

eight_num = 0

for digit in phone
	eight_num += 1 if digit=='8'
end

puts [eight_num, phone.size/11].min


n = gets.to_i
s = gets.chomp
puts [s.count('8'), s.size/8].min


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


a = gets.split(/\s+/).map {|e| e.to_i}
x = a[1..-1]
print x.sort


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
}

inc = digits.map { |d| table[d] }.any?(&:nil?)
dec = digits.map { |d| table.invert[d] }.any?(&:nil?)

puts (inc && dec) ? "YES" : "NO"


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

ns = ['123', '456', '789', '*0#']
xs = []
ys = []
ds.each do |d|
  for y in 0..3
    for x in 0..2
      if d == ns[y][x]
        xd = x
        yd = y
      end
    end
  end
  xs << xd
  ys << yd
end

as = []
'1234567890'.split('').each do |d|
  f = true
  for y in 0..3
    for x in 0..2
      if d == ns[y][x]
        xn = x
        yn = y
      end
    end
  end
  for i in 0...xs.size
    f &= 0 <= xn + xs[i]
    f &= xn + xs[i] <= 2
    f &= 0 <= yn + ys[i]
    f &= ((xn + xs[i] == 1 && yn + ys[i] <= 3) || (xn + xs[i] != 1 && yn + ys[i] <= 2))
  end
  if f
    as << d
  end
end

puts as.size == 1 ? 'YES' : 'NO'


numLength = gets.chomp.to_i
num = gets.chomp.split('').map(&:to_i)
found = false

for i in 0...numLength
	num[i] = 10 if num[i] == 0
end

###############
prob = false

upper = num[0...numLength].map { |i| i -= 3}

upper.each do |i|
	prob = true if i < 0
end

#puts "Upper : #{prob}"
found = true if !prob
################
prob = false

left = num[0...numLength].map { |i| i -= 1}

left.each do |i|
	prob = true if i < 0 || i % 3 == 0
end

#puts "Left : #{prob}"
found = true if !prob
###############
prob = false

right = num[0...numLength].map { |i| i += 1}

right.each do |i|
	prob = true if i < 0 || i % 3 == 1
end

#puts "Right : #{prob}"
found = true if !prob
#################
prob = false

below = num[0...numLength].map { |i| i += 3 }

below.each do |i|
	prob = true if (i != 11 && i > 10)
end

#puts "below : #{prob}"
found = true if !prob
########################

puts 'YES' if !found
puts 'NO' if found


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

counter = 0
for i in 0..n-1
 for j in 0..m-1
   if i*i + j == n && i + j*j == m
       counter+=1
   end
 end
end

puts counter


#!/usr/bin/ruby
# -*- coding: cp932 -*-
include Mat

xs = gets.split.map{ |x| x.to_i}
nans = 0
1000.times do |a|
  1000.times do |b|
    next if a*a + b != xs[0]
    nans += 1 if a + b*b == sx[1]
  end
end
puts nans


# coding: utf-8

# a^2 + b = n
# a + b^2 = m

n, m = gets.split.map(&:to_i)
count = 0
# how many pairs of (a, b)
# 식을 재전개 해보면
# b = n - a^2
# a + b^2 = m 에 대입하면
# for i 0..100 정도면

for a in 0..(Math.sqrt(n).ceil)
    res = a + (n - a**2)**2
    # p res
    if res == m
        count += 1
    end
end

puts count


x = gets[1]
y = x.split()
n = y[1].to_i
m = y[2].to_i

i = 0
a = 0
b = 0

1000.times do
 1000.times do

  if
   a*a + b == n && a + b*b == m
  then
   i = i + 1
   a = a + 1
  else

   a = a + 1

  end
 end
 a = 0
 b = b + 1
end
puts i


n, k = gets.strip.split(' ').map(&:to_i);
a = gets.strip.split(' ').map(&:to_i);
an = 0
k -= 1
(0...n).each { |e|
  ok1 = a[k + e] if k + e < n
  ok2 = a[k - e] if (k - e) >= 0
  if e == 0 && ok1 == 1
    an += 1
    next
  end
  next if ok1.nil? && ok2.nil?
  if !ok1.nil?
    next if ok1.zero?
    an += 1 if !ok1.zero?
  end
  if !ok2.nil?
    next if ok2.zero?
    an += 1 if !ok2.zero?
  end
}
puts an


n, k = gets.strip.split(' ').map(&:to_i);
a = gets.strip.split(' ').map(&:to_i);
an = 0
k -= 1
(0...n).each { |e|
  ok1 = a[k + e] if k + e < n
  ok2 = a[k - e] if (k - e) >= 0
  if e == 0 && ok1 == 1
    an += 1
    next
  end
  next if ok1.nil? && ok2.nil?
  if !ok1.nil?
    next if ok1.zero?
  end
  if !ok2.nil?
    next if ok2.zero?
  end
  an += 2
}
puts an


input = gets.chars
bandidos = gets.split(" ")

cidades = input[0].to_i
limark = input[2].to_i
ele_pega = []
pegos = 0
todas_distancias = []

for i in 0..cidades-1
	if bandidos[i] == "1"
		distancia = (1+i) - limark
		distancia_final = distancia.abs
	todas_distancias << distancia_final
	end
end
for a in 0..todas_distancias.size-1
	next if todas_distancias[a] == 0
	if todas_distancias[a] > (1 - limark).abs || todas_distancias[a] > (bandidos.size + 1 - limark).abs
		pegos += 1
	end
	if ele_pega.include? todas_distancias[a]
		pegos += 2
	end
	ele_pega<< todas_distancias[a]
end
if todas_distancias.include? 0
	pegos += 1
end
puts pegos


n, a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
a -= 1
x = 0
ans = 0
if a <= n - 1 - a
    x = a
    ans = b[(x + a + 1)..-1].inject(:+).to_i
else
    x = n - 1 - a
    ans = b[0...(x - a)].inject(:+).to_i
end
c = b[(x - a)..(x + a)]
d = a * 2

x.times{|i|
    if c[i] == 1 && c[d - i] == 1
        ans += 2
    end
    }
p ans + c[x]


a = gets.chomp.to_i
b = gets.chomp.to_s

# check if valid
COLORS = ['C', 'M', 'Y']
$valid = true
def valids(b)
	prev = nil
	b.split("").each do |char|
		if !(char == '?' || char != prev)
			return false
		end
		prev = char
	end
	return true
end
ll = b.length
def permute(b, cols, i, str, out)
	if i == b.length
		out.push(str)
	elsif out.length > 3
		return out
	else
		prevc = nil
		prevc = str[i-1] if i > 0
		nextc = nil
		while str[i] != '?' && i < b.length
			prevc = str[i]
			i += 1
		end
		nextc = b[i+1]
		if i < b.length
			cola = cols - [prevc, nextc]
			cola.each do |col|
				str[i] = col
				out = permute(b, cols, i+1, str.dup, out)
				str[i] = '?'
			end
		else
			out.push(str)
		end
	end
	return out
end
if valids(b)
	out = permute(b, COLORS, 0, b.dup, [])
	if out.length == 2
		puts "Yes"
	else
		puts "No"
	end
	# puts out
else
	puts "No"
end


s = gets.strip
maxl = 1
len = 1
vowels = ['A', 'E', 'I', 'O', 'U', 'Y']
0.upto(s.length - 1) do |i|
  if vowels.include?(s[i])
    maxl = [maxl, len].max
    len = 1
  else
    len += 1
  end
end

printf("%d\n", maxl)


s = gets.strip
puts s.split(/[AEIOUY]/).map{|x| x.size+1}.max


def calc(m, k, filters)
  return 0 if k >= m

  socket_count = k - 1
  filter_count = 1

  filters.sort_by! { |x| -x }
  filters.each do |item|
    return filter_count if item + socket_count >= m

    socket_count += item - 1
    filter_count += 1
  end

  return -1
end

if $0 == __FILE__
  ARGF.readlines.each_slice(2) do |line1, line2|
    n, m, k = line1.chomp.split.map {|x| x.to_i }
    filters = lien2.chomp.split.map {|x| x.to_i }
    calc(m, k, filters)
  end
end


a=gets.chomp.split(" ")
0.upto(2) {|i| a[i]=a[i].to_i}
n=a[0]
m=a[1]
k=a[2]
b=gets.chomp.split(" ")
0.upto(n-1) {|i| b[i]=b[i].to_i}
b.sort!
b.reverse!

ans=0
sock=k

loop do
 if sock>=m
 sock=-100
 break
 end

 sock=sock+b[ans]-1
 ans+=1
 break if ans==n
 end

if sock==-100
puts "#{ans}"
else
puts "-1"
end


n,m,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i).sort {|i,j| j<=>i}

if k>=m
  puts 0
  exit
else
  i=0
  while k>0
    m-=a[i]
    i+=1
    k-=1
    if m<=k
      puts i
      exit
    end
  end
  while i<a.length
    m-=a[i]-1
    i+=1
    if m<=0
      puts i
      exit
    end
  end
end

puts -1


n,m,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i).sort_by {|x| -x}

i=0
while k<m && i<a.length
  k+=a[i]-1
  i+=1
end

if i<a.length
  puts i
else
  puts -1
end


stones = gets.chomp.to_i

threes = (stones / 3).to_i

remaining = stones - (threes * 3)

count = threes * 2

count += 1 if remaining == 1

count = 1 if count == 0

puts count


n = gets.to_i

ans = 0

i = 0
while true do
  if i.even?
    n -= 1
    ans += 1
  else
    break if n < 2
    n -= 2
    ans += 1
  end
  break if n == 0
  i += 1
end
puts ans


n = gets.to_i
cnt = 0
while n
  if n >= 1
    n -= 1
    cnt += 1
  else
    break
  end
  if n >= 2
    n -= 2
    cnt += 1
  else
    break
  end
end
puts cnt


x,y = gets.split.map(&:to_i)
prime = []
k = (y**0.5).to_i

a = Array.new(k+1,1)
i = 2
while i <= k
  if a[i] == 1
    prime << i
    (k/i).times do |j|
      a[i*j] = 0
    end
  end
  i += 1
end
isprime = false
while !isprime && x < y
  isprime = true
  prime.length.times do |i|
    isprime = false if y % prime[i] == 0
  end
  if isprime
    ans = y
  end
  y -= 1
end
ans = -1 if !isprime
puts ans


require 'prime'
p, y=gets.split.map &:to_i
y.downto(p+1){|x|
    if x.prime?
        puts x
        exit
    end
}
puts -1


ttfs = gets.chomp
ttfs = ttfs.split
two = ttfs[0].to_i
three = ttfs[1].to_i
five = ttfs[2].to_i
six = ttfs[3].to_i

thmax = 0
if two <= five && two <= six
	thmax = two
elsif five <= two && five <= six
	thmax = five
elsif six <= two && six <= five
	thmax = six
end

if three <= two
	twmax = three
else
	twmax = two
end

# 256 is greater
if two == 0 || three == 0
	print 0
elsif five == 0 || six == 0
	if three <= two
		print 32 * three
	else
		print 32 * two
	end
elsif thmax >= twmax && thmax >= twmax
	twofivesix = thmax * 256
	ttfs[0] = ttfs[0].to_i - thmax
	ttfs[2] = ttfs[2].to_i - thmax
	ttfs[3] = ttfs[3].to_i - thmax
	if ttfs[1].to_i <= ttfs[0].to_i
		twmax = ttfs[1].to_i
	else
		twmax = ttfs[0].to_i
	end
	threetwo = twmax * 32
	print twofivesix + threetwo
else
	if thmax == two
		if six <= five
			thmax = six
		else
			thmax = five
		end
		twofivesix = thmax * 256
		ttfs[0] = ttfs[0].to_i - thmax
		ttfs[2] = ttfs[2].to_i - thmax
		ttfs[3] = ttfs[3].to_i - thmax
		if ttfs[1].to_i <= ttfs[0].to_i
			twmax = ttfs[1].to_i
		else
			twmax = ttfs[0].to_i
		end
		threetwo = twmax * 32
	else
		twofivesix = thmax * 256
		ttfs[0] = ttfs[0].to_i - thmax
		ttfs[2] = ttfs[2].to_i - thmax
		ttfs[3] = ttfs[3].to_i - thmax
		if ttfs[1].to_i <= ttfs[0].to_i
			twmax = ttfs[1].to_i
		else
			twmax = ttfs[0].to_i
		end
		threetwo = twmax * 32
	end
	print twofivesix + threetwo
end


# a is 2
# b is 3
# c is 5
# d is 6

a = gets.to_i
b = gets.to_i
c = gets.to_i
d = gets.to_i

k256 = [a, c, d].min
a -= k256
k32 = [a, b].min
puts k256*256 + k32*32


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

x = [a,c,d].min
y = [a,b].min

puts 256*x + 32*y


require 'set'
parse_int = lambda{ gets.split.map{|el| el.to_i} }
sign = lambda {|el| (el>0?1:0)-(el<0?1:0)}

d2,d3,d5,d6 = parse_int.call

ans = 256*[d5,d6].min
d2 -= [d5,d6].min

ans += 32*[d2,d3].min

puts ans


if gets.chomp =~ /1{7}|0{7}/
  puts "YES"
else
  puts "NO"


a.gets
b = a.scan(/0000000/).count
c = a.scan(/1111111/).count

if b || c > 0
puts "YES"
else
puts "NO"
end


def function
a=gets.chomp.to_i
a+=1
c=1
while !a.to_s.include?('8')
c+=1
a+=1
end
c
end
function


n=gets.to_i
x=n
a=[]
10.times do
	a<<(x+1).to_s.split('')
	x+=1
end
ans=nil
for x in 0..9
	if a[x].include?'8'
		ans=x+1
		break
	end
end
puts ans


require 'pp'

n = STDIN.gets.chomp.to_i

ans = 0
while true
  n.to_s.split(//).each do |e|
    if e == '8'
      puts ans
      exit(0)
    end
  end
  n+=1
  ans+=1
end


a, b = [gets.chomp.to_i, -1]
until ((a + (b += 1)).to_s.include?('8'))
end
puts b


n = gets.to_i
moves = gets.strip.gsub(/(UR|RU)/, 'D')
puts moves


n = gets.to_i
puts ((Math.sqrt(n).floor + 1) * (Math.sqrt(n).floor) / 2 == n) ? "YES" : "NO"


a=[]
for i in 1..40
 a.push i*(i+1)/2
end
b=gets.to_i
if a.index(b)!=nil then
p "YES"
else
p "NO"
end


def color_to_bitmask(color)
  case color
  when 'B'
    bitmask = 0b00001
  when 'Y'
    bitmask = 0b00010
  when 'W'
    bitmask = 0b00100
  when 'G'
    bitmask = 0b01000
  when 'R'
    bitmask = 0b10000
  end
end

def card_strings_to_card_bitmasks(card_strings)
  card_strings.map {
    |c|
    color = c[0]
    value = c[1].to_i

    color_to_bitmask(color) * 0b100000 + 2 ** (value - 1)
  }
end

def check_ops_for_pair(ops, a, b)
  if a != b
    (a ^ b) & ops > 0
  else true
  end
end

def combinations(i1)
  (0...(i1.size-1)).inject([]) {|pairs,x| pairs += ((x+1)...i1.size).map {|y| [i1[x],i1[y]]}}
end

def check_ops_for_combinations(ops, combinations)
  combinations.inject {
    |result, combination|
    a, b = combination
    check = check_ops_for_pair(ops, a, b)
    if not check
      break(false)
    end
    check
  }
end

def count_ones(n)
  n.to_s(2).count('1')
end

strings = STDIN.read.split("\n")[1].split(" ")

bitmasks = card_strings_to_card_bitmasks(strings)

bitmasks_combinations = combinations(bitmasks)

best_solution = 10

for ops in 0..2 ** 10 - 1
  if check_ops_for_combinations(ops, bitmasks_combinations)
    ones = count_ones(ops)
    if ones < best_solution
      best_solution = ones
    end
  end
end

puts best_solution


s = gets
ans =nil
ans_count =0
m = {}
0.upto s.length do |i|
    1.upto s.length-i do |j|
        ss = s[i, j]
        b = false
        ss.each_char {|c| b ||= (c!='7' && c!='4')}
        if !b do
            count=m[ss]+1
            m[ss] = count
            if count > ans_count) do
                ans = ss
            elsif count== ans_count do
                ans = ss if ans==nil
                ans = min ans, ss
            end
        end
    end
end

puts ans


require 'jcode'
s = gets
ans = -1
ans_count =0
m = {}
0.upto s.length do |i|
    1.upto s.length-i do |j|
        ss = s[i, j]
        b = false
        ss.each_char {|c| b ||= (c!='7' && c!='4')}
        if !b
            count= (m[ss]== nil ? 0 : m[ss] )+1
            m[ss] = count
            if count > ans_count
                ans,ans_count=ss,count
            else count== ans_count
                ans = ss if ans==-1
                ans = ss if ss < ans
            end
        end
    end
end

puts ans


s = gets
v,f,a=s.count'7',s.count'4',-1
a=4 if f>0
a=7 if v>f
puts a


s = gets
r = ""
m = 0
for i in 0...s.length
	for j in i...s.length
		t = s[i, j]
		next if t =~ /[^47]/ or t.size < 1
		r = t if r.size < 1
		n = 0
		scan(t) { n += 1 }

	end
end
puts r == "" ? -1 : r


gets.to_i.times{
  puts [1, 4, 5, 8, 11].include?(gets.to_i) ? "NO" : "YES"
}


input = Array.new
n = $stdin.gets.to_i
n.times do
  line = $stdin.gets
  input << line.to_i
end

input.each do |a|
  if a == 0
    puts 'NO'
  elsif a%3 == 0
    puts 'YES'
  elsif a%7 == 0
    puts 'YES'
  elsif a%21 == 0
    puts 'YES'
  elsif a%3%7 == 0
    puts 'YES'
  elsif a%7%3 == 0
    puts 'YES'
  else
    puts 'NO'
  end
end


n = gets.chomp.to_i
arr = []
for i in (0..n-1)
	arr << gets.chomp.to_i
end

for i in (0..n-1)
	if arr[i]%3 == 0 || arr[i]%7 == 0
		puts "YES"
	else
		x = arr[i]
		while x >= 0
			if x - 7 >= 0
				x -= 7
			elsif x - 3 >= 0
				x -= 3
			else
				break
			end
		end
		if x == 0
			puts "YES"
		else
			puts "NO"
		end
	end
end


gets.to_i.times.map{|x| gets.to_i}.each{|x| puts (x%3 == 0 || x%7 == 0)? "YES" : "NO" }


p, k = gets.chomp.split
p = p.to_i
k = k.to_i

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!
puts(ans.length)
puts(ans.join(" "))


k, b, n, t = gets.split.map(&:to_i)
bg=1
n.times {bg=bg*k+b}
sec=0
while t<bg
  sec+=1
  t=t*k+b
end
puts sec


k, b, n, t = gets.split.map(&:to_i)
bg=1
sec=0
while bg<t
  sec+=1
  bg=bg*k+b
end
if bg ==t
puts n-sec
else
puts n-sec+1
end


n = gets.to_i

last = ?0
gets.split.each do |x|
	n -= 1 if last == ?1 && x == ?0
	last = x
end

p n


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

# lim = (n * 9) + 10
lim = ary.inject(:+) - 1

# def poss?(ary, ss)
#   i=j=0
#   sum = ary[0]
#   return false if sum > ss
#   sz = ary.size
#   while i<sz && j<sz do
#     if sum == ss
#       j = j+1
#       i = j
#       return true if j >= sz
#       sum = ary[j]
#     end
#   end
# end

def poss?(ary, ss)
  i=0
  sum = 0
  sz = ary.size
  while i<sz do
    sum += ary[i]
    if sum == ss
      sum = 0
    elsif sum > ss
      return false
    end
    i += 1
  end
  sum == 0
end

1.upto(lim).each do |ss|
  if poss?(ary, ss)
    # puts ss
    puts "YES"
    exit 0
  end
end

puts "NO"


require 'prime'

def f(a,b)
  c=0
  x = Prime.prime_division(a)
  while b>0 do
    tmp = b
    x.each do |ar|
      tmp = [tmp, b%ar[0]].min
    end
    c += tmp
    b -= tmp
    x.each do |ar|
      while b%ar[0]==0
        b/=ar[0]
        ar[1]-=1
        x.delete(ar) if ar[1]==0
      end
    end
  end
  return c
end


require 'prime'

def f(a,b)
  c=0
  x = Prime.prime_division(a)
  while b>0 do
    tmp = b
    x.each do |ar|
      tmp = [tmp, b%ar[0]].min
    end
    c += tmp
    b -= tmp
    x.each do |ar|
      while b%ar[0]==0
        b/=ar[0]
        ar[1]-=1
        x.delete(ar) if ar[1]==0
      end
    end
  end
  return c
end

input = gets.chomp
a = input.split(" ")[0]
b = input.split(" ")[1]
puts f(a,b)


require 'prime'

def f(a,b)
  c=0
  x = Prime.prime_division(a)
  while b>0 do
    tmp = b
    x.each do |ar|
      tmp = [tmp, b%ar[0]].min
    end
    c += tmp
    b -= tmp
    x.each do |ar|
      while b%ar[0]==0
        b/=ar[0]
        ar[1]-=1
        x.delete(ar) if ar[1]==0
      end
    end
  end
  return c
end

input = gets.chomp
a = input.split(" ")[0].to_i
b = input.split(" ")[1].to_i
puts f(a,b)


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

b = b.to_s

while true
  if a.to_s.gsub(/[12356890]/, "") == b
    p a
    break
  end
  a += 1
end


def lucky_n?(str)
  str.chars.all?{|i| i == "4" || i == "7" }
end

def lucky_s?(str,n)
  str = str.chars.to_a
  str[0,n/2].map(&:to_i).inject(:+) == str[(n/2)..-1].map(&:to_i).inject(:+)
end

def lucky?(n,str)
  lucky_n?(str) && lucky_s?(str,n)
end

def mask_of(str)
  str.chars.select{|i| i == "4" || i == "7" }.join
end

def solve(a,mask)
  i = a.to_i
  mask = mask.to_s
  while mask != mask_of(i.to_s)
    i += 1
  end
  i
end

a, mask = STDIN.gets.chomp.split(/ /)
p solve(a, mask)


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

max_arr = []
(1...arr.count).each do |i|
max_arr << arr[i] - arr[i-1]
end
initial_max = max_arr.max
max_arr = []
(1...arr.count-1).each do |i|
  max_arr << arr[i+1] - arr[i-1]
end
a = max_arr.any?{|x| x < initial_max}
puts a ? initial_max : max_arr.max


s = gets.chomp.to_i
ar = gets.chomp.split.map {|x| x.to_i}
min = []

unless ar.size != s
	(0...ar.size).each do |n|
		if ar[n] != ar.first and ar[n] != ar.last
			comb = (ar - [ar[n]])
			m = []
			(1...comb.size).each do |c|
				if comb[c] != comb.first and comb[c-1] != comb.first
					#puts "#{comb[c]} - #{comb[c-1]} = #{(comb[c] - comb[c-1])}"
					m << (comb[c] - comb[c-1])
				end
			end
			min << m.max
		end
		#if ar[n] == ar.first then puts ar.first end
	end
	p min.min
end


# 496A

n = gets.to_i
a = gets.chomp.split.map &:to_i
min, max = 1_000, 0
min_ind = 0
(n - 1).times do |i|
  diff = a[i + 1] - a[i]
  if diff < min && i < n - 2
    min = diff
    min_ind = i
  elsif diff > max
    max = diff
  end
end
puts [max, a[min_ind + 2] - a[min_ind]].max


t="
"+$<.read;puts t==t.reverse ?"YES":"NO"


buttons=[]
buttons[0]=gets.chomp
buttons[1]=gets.chomp
buttons[2]=gets.chomp
(0..2).each{|i|
  (0..2).each{|j|
    if buttons[i][j].chr=='X' && buttons[2-i][2-j].chr!='X' then print("NO\n");exit end
  }
}
print(buttons[1][1].chr=='X' ? 'NO':'YES',"\n")


s = gets.chomp
t = gets.chomp
divs = []
divt = []
(s.length-1).times do |i|
    divs.push(i) if s[i] != s[i+1]
end
divs.push(s.length-1)
(t.length-1).times do |i|
    divt.push(i) if t[i] != t[i+1]
end
divt.push(t.length-1)

ans = []

if s[0] == t[0]
    ps, pt = nil, nil
    if divs.length == 2 && divt.length >= 4
        ps = divs[0]+1
        pt = divt[1]+1
    elsif divt.length == 2 && divs.length >= 4
        ps = divs[1]+1
        pt = divt[0]+1
    end
    if ps
        ans.push([ps, pt])
        s, t = [
            t[0, pt] + s[ps, s.length],
            s[0, ps] + t[pt, t.length]
        ]
        divs = []
        divt = []
        (s.length-1).times do |i|
            divs.push(i) if s[i] != s[i+1]
        end
        divs.push(s.length-1)
        (t.length-1).times do |i|
            divt.push(i) if t[i] != t[i+1]
        end
        divt.push(t.length-1)
    end
end

if divs.length == 1 && divt.length == 1
    puts 0
    exit
end

if s[0] == t[0]
    if divs.length < divt.length
        ns = 0
        nt = (divt.length - divs.length)/2
        nt += 1 if nt % 2 == 0
    else
        nt = 0
        ns = (divs.length - divt.length)/2
        ns += 1 if ns % 2 == 0
    end
else
    if divs.length < divt.length
        ns = 1
        nt = (divt.length - 1 - divs.length)/2 + 1
        nt += 1 if nt % 2 == 0
    else
        nt = 1
        ns = (divs.length - 1 - divt.length)/2 + 1
        ns += 1 if ns % 2 == 0
    end
end

ps = ns == 0 ? 0 : divs[ns-1]+1
pt = nt == 0 ? 0 : divt[nt-1]+1
ans.push([ps, pt])
s, t = [
    t[0, pt] + s[ps, s.length],
    s[0, ps] + t[pt, t.length]
]

divs = []
divt = []
(s.length-1).times do |i|
    divs.push(i) if s[i] != s[i+1]
end
divs.push(s.length-1)
(t.length-1).times do |i|
    divt.push(i) if t[i] != t[i+1]
end
divt.push(t.length-1)

max = [divs.length, divt.length].max
[max-2, max-1].each do |i|
    next if i < 0
    divs[i] ||= divs[-1]
    divt[i] ||= divt[-1]
end

ps, pt = 0, 0
(max-1).times do |i|
    ps += divs[i] - (i > 0 ? divs[i-1] : -1)
    pt += divt[i] - (i > 0 ? divt[i-1] : -1)
    ans.push([ps, pt])
    ps, pt = pt, ps
end

puts ans.length
ans.each do |arr|
    puts arr.join(' ')
end


n,*a=$<.read.split.map &:to_i
s=a.reduce :+
puts (1<<n).times.any?{|i|t=n.times.select{|j|i[j]>0}.map{|j|a[j]}.reduce :+;t*2==n} ? :YES : :NO


n,*a=$<.read.split.map &:to_i
s=a.reduce :+
puts (1<<n).times.any?{|i|t=n.times.select{|j|i[j]>0}.map{|j|a[j]}.reduce 0,:+;t%360==0} ? :YES : :NO


#http://codeforces.com/contest/816/problem/A

time = gets.chop

h = time.slice(0, 2).to_i
m = time.slice(3, 2).to_i

tmp = h / 10
pal_m = (h - (tmp * 10)) * 10 + tmp
count = 0

f = false
  if(m == pal_m) then
    f = true
  end
while(!f) do
  m = m + 1
  if(m == 60) then
    m = 0
    h = h + 1
  end
  h = h % 24
  pal_m = (h % 10) * 10 + (h / 10)
  count = count + 1
  if(m == pal_m) then
    f = true
  end
end

puts count

if(m<10) then
  if(h<10) then
    puts "0" + h.to_s + ":0" + m.to_s
  else
    puts h.to_s + ":0" + m.to_s
  end
else
  if(h<10) then
    puts "0" + h.to_s + ":" + m.to_s
  else
    puts h.to_s + ":" + m.to_s
  end
end


#http://www.codeforces.com/contest/653/problem/B

class BearCompression
  def initialize(length)
    @output_length = length
    @operations = {}
  end

  def add_operation(input)
    a, b = input.split
    @operations[a] = b
  end

  def run
    @output = []
    @chars = ['a']

    (@output_length - 1).times do
      @output = @operations.select { |_, v| @chars.include?(v) }.keys
      @chars = @output.map { |x| x[0] }
    end

    puts @output.count
  end
end

string_length, operations_count = gets.chomp.split.map(&:to_i)

b = BearCompression.new(string_length)
operations_count.times do
  b.add_operation(gets.chomp)
end

b.run


n = gets.to_i
a = Hash.new(false)
a[n] = true
while n > 1
    n += 1
    n /= 10 while n%10 == 0
    a[n] = true
end
puts a.size


n, t, k, d = gets.split.map(&:to_i)
fst = n / 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")


# your code goes here
a, b = gets.chomp.split.map(&:to_i)

result = 1
(1..(b-a)).each do |i|
	result = result * (b + 1 - i)
end

puts result%10


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

r= 1

(a+1 .. b).each do |i|
	r  = r*i
	if r%10 == 0
		break
	end
end

print r%10


n = gets.to_i
a = gets.strip.split.map{|s| s.to_i}
cnt1 = a.select{|i| 100 == i}.length
cnt2 = n - cnt1
if 0 == cnt1 % 2 and 0 == cnt2 % 2 or 0 == cnt1 % 2 and 1 == cnt2 % 2 and cnt1 >= 2
	puts "YES"
else
	puts "NO"
end


n = gets.chomp.to_i

def solve(n)
  dp = [0] * (n + 1)
  dp[1] = 1
  for i in 2...n+1 do
    dp[i] = dp[i-1] + 2**i
  end
  return dp[n]
end

puts solve(n)


n = gets.strip.to_i
a = gets.strip.to_i
b = gets.strip.to_i
c = gets.strip.to_i
curr = 1
ans = 0
if n > 1
    i = 1
    while i < n
        dist = 0
        if curr.odd?
            dist = [a, b].min
            curr = 2
        else
            dist = [a, b, c].min
            curr = 1
        end
        ans += dist
        i += 1
    end
end
puts ans


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

if n==1
    p 0
end
len = 0

x = [a,b].min
len += x
n-=2

if n > 0
    len += [x,c].min*n
end

p len


#! /usr/bin/env ruby

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

n -= 1

ans = 0

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

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

puts ans


word = gets.chop.gsub!(/[aoyeui]/i,"").downcase.scan(/(.)/).flatten.map {|i| i = ".".concat(i) }
puts "#{word.join}"


word = gets.gsub!(/[aoyeui]/i,"").downcase.to_a.map {|i| i = ". + #{i}") }
puts "#{word.join}"


n=gets.to_i;d=Array.new(6){Array.new(6){0}}
n.times{f,t=gets.split.map(&:to_i);d[f][t]=d[t][f]=1}
for i in 1..m
        for j in 1..m
                next if i==j

                for k in 1..m
                        next if i==j || j==k || i==k

                        if d[i][j]+d[j][k]+d[k][i]==3 || d[i][j]+d[j][k]+d[k][i]==0
                                puts :WIN;exit
                        end
                end
        end
end
puts :FAIL


n=gets.to_i
g=Array.new(5){[]}

n.times{
	from,to=gets.split.map(&:to_i)

	from-=1
	to-=1

	g[from] << to
	g[to] << from
}

ans=false
g.each{|v|
	if [0, 3].include?(v.size)
		ans=true
		break
	end
}

puts ans ? "WIN" : "FAIL"


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


n = gets.to_i
count = Hash.new(0)
n.times do
	a,b = gets.split(" ").map(&:to_i)
	count[a] += 1
	count[b] += 1
end

ans = false
count.values.each do |a|
	if a >= 3 || a <= 1
		ans = true
		break
	end
end

puts ans ? "WIN" : "FAIL"


input=gets.chomp.to_i
output=0
i=1
loop do
break if i >= (input-(i*2))/2
	i+=1
	output+=1
end
puts output


#Паша и палка

def f(l, n, n2)
  return 0 if n-l%2==1 || l>=n2
  1 + f(l+2, n, n2)
end

n = gets.to_i
res = f(2, n, n/2)

puts res


# http://www.codeforces.com/contest/610/problem/A
class Solv
  def initialize(limit)
    @limit = limit
    @count = 0
  end

  def run
    target = @limit / 2
    (1..(target/2-1)).each do |i|
      @count += 1
    end

    @count += 1 if (target % 2 != 0) and target != 1
    @count
  end
end

len = gets.to_i
solv = Solv.new(len)
puts solv.run


# http://www.codeforces.com/contest/610/problem/A

class Solv
  def initialize(limit)
    @limit = limit
    @count = 0
  end

  def run
    target = @limit / 2
    @count = (target/2-1)
    @count = 0 if @count < 0

    @count += 1 if (target % 2 != 0) and target != 1
    @count
  end
end

len = gets.to_i
solv = Solv.new(len)
puts solv.run


s = gets.chomp
str = ""
if s.start_with? "ftp" then
    str += "ftp://"
    s = s[3..-1]
else
    str += "http://"
    s = s[4..-1]
end
k = s.index("ru")
if k == 0 then k = s.index("ru", 1)
end
str = str + s[0...k] + ".ru"
if !str[k+2] then
    str = str + "/" + s[k+2..-1]
end
puts "#{str}"


s = gets.strip
s.insert(s.index("p")+1, "://")
i = s.index("ru")
s.insert(i, ".")
i += 3
if i < s.length
  s.insert(i, "/")
end
print s


def foo()
    x,y=gets.split.map(&:to_i)
    u = x**2+y**2
    return "black" if (u**0.5).to_i**2 == u
    return "black" if x % 2 == 0 and x * y > 0
    return "black" if x % 2 > 0 and x*y < 0
    return "white"
end

puts foo


x,y = gets.split.map(&:to_i)
puts (((Math.sqrt(x*y))%2).zero?)? 'white' : 'black'


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

sqrt = ((x**2 + y**2)**0.5)

if sqrt == sqrt.to_i then
	puts 'black'
else
	unless ( (sqrt%2).zero? )
			puts (x*y > 0) ? 'black' : 'white'
		else
			puts (x*y > 0) ? 'white' : 'black'
	end
end


gets
puts gets.chomp.split(" ").sort.join(" ")


line = gets.split();
a,b = line[0].to_i,line[1].to_i;

if(b >= a*2)
   puts "0";
else
   n = 1;
   if(b > a)
      n = b - a;
   else
      a = b;
   end
   ans = (a-n-1)/2 + 1;
   puts"#{ans}";
end


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

amount = n - m + 1
if m == n
    if differ(m) >= s
        amount = 1
    else
        amount = 0
    end
end
puts amount


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

def fromdec(n)
  rtn = ""
  while 0 < n.to_i
    rtn = (n.to_i % 7).to_s + rtn
    n = (n.to_i / 7).to_s
  end
  return rtn
end

def todec(n)
  rtn = 0
  (0...n.size).each do |i|
    rtn += n[i].to_i * 7 ** (n.size - i - 1)
  end
  return rtn
end

ln = fromdec(n - 1).size
lm = fromdec(m - 1).size

ans = 0
(0...7).to_a.permutation(ln + lm) do |ps|
  pn = (0...ln).inject('') { |str , i| str + ps[i].to_s }
  pm = (ln...ln + lm).inject('') { |str , i| str + ps[i].to_s }
  if todec(pn) < n && todec(pm) < m
    ans = ans + 1
  end
end

puts ans


n, x = gets.split.map(&:to_i)
ans, a = 0, 0
1.upto(n) do |i|
  ans += 1 if i * i == x
  (i + 1).upto(n) do |j|
    a += 1 if i * j == x
  end
end
puts ans + a * 2


as=gets.chomp
a=as.split("")
bs=gets.chomp
b=bs.split("")
al=a.length
bl=b.length

if al==bl

if a.sort==b.sort
puts "array"
else
puts "need tree"
end

else

if al<bl
puts "need tree"

else

j=0
i=0
as
loop do
if as[i]==bs[j]
j+=1
end
i+=1
break if i==al
break if j==bl
end

if j==bl
puts "automaton"

else

t=1
b.uniq.each do |i|
if b.count(i)!=a.count(i)
t=0
end
end

if t==1
puts "both"
else
puts "need tree"
end

end
end
end


s = gets.chomp
t = gets.chomp

source_count = Hash.new(0)
s.each_char do |c|
	source_count[c] += 1
end

target_count = Hash.new(0)
t.each_char do |c|
	target_count[c] += 1
end

result = true
target_count.each_key do |k|
	if !source_count[k] || source_count[k] < target_count[k]
		result = false
		break
	end
end

ans = "need tree"
if result
	if s.length == t.length
		ans = "array"
	elsif s.include?(t)
		ans = "automaton"
	else
		ans = "both"
	end

end

puts ans


require 'set'

def sub_sequence?(a, chars)
  a =~ Regexp.new('^' + '.*' + chars.join('.*') + '.*' + '$')
end

s, t = gets.chomp, gets.chomp
s_chars, t_chars = s.split(''), t.split('')
puts (if sub_sequence?(s, t_chars)
        'automaton'
      elsif s_chars.sort == t_chars.sort
        'array'
      elsif s.size >= t.size && (Set.new(t_chars) - Set.new(s_chars)).empty?
        'both'
      else
        'need tree'
      end)


s = gets.chomp
t = gets.chomp

subs = s.dup
automaton = t.chars.all? do |c|
  n = subs.index(c)
  n.nil? ? false : subs = subs[n..-1]
end

array = s.chars.sort == t.chars.sort

subs = s.dup
both = t.chars.all? do |c|
  n = subs.index(c)
  n.nil? ? false : subs.slice!(n)
end

puts case
when automaton; "automaton"
when array; "array"
when both; "both"
else "need tree"
end


total_cards = gets.to_i
cards_1 = gets.split(' ')
total_cards_1 = cards_1[0].to_i
cards_2 = gets.split(' ')
total_cards_2 = cards_2[0].to_i
cards_1.shift
cards_2.shift
play_1 = cards_1.clone
play_2 = cards_2.clone
total = 0
loop do
    card_1 = play_1.shift.to_i
    card_2 = play_2.shift.to_i
    if card_1 > card_2
        play_1.push(card_2)
        play_1.push(card_1)
    else
        play_2.push(card_1)
        play_2.push(card_2)
    end
    total+=1
    break if (play_1==cards_1 && play_2==cards_2) || play_1.size == 0 || play_2.size == 0
end
if play_1.size == 0
    puts "#{total} 2"
elsif play_2.size == 0
    puts "#{total} 1"
else
    puts "-1"
end


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}
e=[]
e=e+b
c=gets.chomp.split(" ")
lc=c.delete_at(0).to_i
0.upto(lc-1) {|i|c[i]=c[i].to_i}
d=[]
d=d+c
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 e==b  && d==c
puts "-1"
break
end
end


def play_war(stack1, stack2)
  moves = 0

  while !stack1.empty? && !stack2.empty?
    val1 = stack1.shift
    val2 = stack2.shift

    if val1 < val2
      #opponet won
      stack2.push(val1)
      stack2.push(val2)
    else
      #I won
      stack1.push(val2)
      stack1.push(val1)
    end
    moves += 1
  end

  if stack1.empty?
    puts "#{moves} #{2}"
  else
    puts "#{moves} #{1}"

  end

end

gets
arr1 = gets.split(" ")
arr1.shift
arr2 = gets.split(" ")
arr2.shift
play_war(arr1, arr2)


def play_war(stack1, stack2)
  moves = 0
  i = 0
  while !stack1.empty? && !stack2.empty? && i < 5000000
    i++
    val1 = stack1.shift
    val2 = stack2.shift

    if val1 < val2
      #opponet won
      stack2.push(val1)
      stack2.push(val2)
    else
      #I won
      stack1.push(val2)
      stack1.push(val1)
    end
    moves += 1
  end

  if stack1.empty?
    puts "#{moves} #{2}"
  else
    puts "#{moves} #{1}"

  end

end

gets
arr1 = gets.split(" ")
arr1.shift
arr2 = gets.split(" ")
arr2.shift
play_war(arr1, arr2)


totalCard = gets.chomp.to_i

firstPerson = gets.chomp.split(' ').map {|item| item.to_i}
secondPerson = gets.chomp.split(' ').map {|item| item.to_i}

originalfPerson ||= []
originalsPerson ||= []

firstTurn = false
turnNum = 0
#####

firstPerson.each do |item|
	originalfPerson << item
end

secondPerson.each do |item|
	originalsPerson << item
end

loop do
	if (originalfPerson <=> firstPerson) == 0 &&
		(originalsPerson <=> secondPerson) == 0 && firstTurn

		puts -1
		break
	end

	if firstPerson.length == 1
		print "#{turnNum} 2"
		break
	end

	if secondPerson.length == 1
		print "#{turnNum} 1"
		break
	end

	if firstPerson[1] > secondPerson[1]
		firstPerson << secondPerson[1]
		firstPerson << firstPerson[1]

		firstPerson.delete_at(1)
		secondPerson.delete_at(1)
	else
		secondPerson << firstPerson[1]
		secondPerson << secondPerson[1]

		firstPerson.delete_at(1)
		secondPerson.delete_at(1)
	end

	turnNum += 1
	firstTurn = true;
end


gets
puts gets.split.reverse.uniq.reverse*' '


number_size = gets.to_i
numbers = gets.split(' ').map(&:to_i)

puts numbers.uniq.size
puts numbers.uniq.join(' ')


#include <cstdio>

const int MAXN = 128;

int main() {
	int r, c;
	char s[MAXN][MAXN];

	scanf("%d%d", &r, &c);
	for (int i = 0; i < r; ++i) {
		scanf("%s", s[i]);
	}
	for (int i = 0; i < r; ++i) {
		for (int j = 0; j < c; ++j) {
			bool flag = true;
			for (int k = 0; flag && k < r; ++k) {
				flag &= k == i || s[k][j] != s[i][j];
			}
			for (int k = 0; flag && k < c; ++k) {
				flag &= k == j || s[i][k] != s[i][j];
			}
			if (flag) {
				putchar(s[i][j]);
			}
		}
	}

	return 0;
}


# Hello World Program in Rub
r,g,b = gets.split(" ").map(&:to_f)
a = [((r/2).ceil-1)*3,((g/2).ceil-1)*4,((b/2).ceil-1)*5]
puts a.max+30


r, g, b = gets.split.map(&:to_i)
rr, gg, bb = [r, g, b].map { |i| i <= 2 ? 0 : (i % 2 == 0) ? i / 2 - 1 : i / 2 }
if r > g && r > b
  ret = 30 + rr * 3
elsif g > r && g > b
  ret = 31 + gg * 3
elsif b > r && b > g
  ret = 32 + bb * 3
else
  ret = 30 + rr * 3
end
puts ret


n = gets.to_i
a = n
(n..9013).each do |x|
  if x.to_s.split('').uniq.count>3
    a = x
    break
  end
end
puts a


n = gets.chomp.to_i + 1

while 1
  if(n.to_s.to_a.uniq.size == 4)
    break
  else
    n+=1
  end
end

puts n


class A
  def initialize
    n = gets.chomp
    size = n.size

    n.gsub!("144", "aaa")
    n.gsub!("14", "aa")
    n.gsub!("1", "a")

    n.delete!("123456789")

    if n.size == size
      puts "YES"
    else
      puts "NO"
    end
  end
end

a = A.new


d = gets.chomp.to_s

d.gsub!('144', '')
d.gsub!('14', '')
d.gsub!('1', '')
if d.size > 0
  puts 'NO'
else
  puts 'YES'
end


# coding: utf-8

# if __FILE__ == $0
# end

s = gets.chomp
ns = s.gsub("144", "").gsub("14", "").gsub("1", "")

if ns == ""
    puts "YES"
else
    puts "NO"
end


def fill_primes(max)
  primes = [2,3]
  now = primes[-1]
  loop{
    now+=2
    break if now>max
    prime, check = true, 0
    while primes[check]**2 <= now:
      prime = false if now%primes[check]==0
      break if not prime
      check+=1
    end
    primes << now if prime
  }
  return primes
end

def is_almost_prime(num, primes)
  check = 0
  divisors = [1]
  while num>1
    while num % primes[check] == 0
      num /= primes[check]
      divisors << primes[check] if divisors[-1] != primes[check]
    end
    check+=1
  end
  if divisors.length == 3
    1
  else
    0
  end
end

primes = fill_primes(3000)

primes, n, ans = fill_primes(3000), gets.chomp.to_i, 0
2.upto(n){|i| ans+=is_almost_prime(i, primes) }
p ans


solution = 0
if ARGV.length > 0
    input = ARGV[0].to_i
    lower = 6
    lower.upto(input) do |i|
        temp = i
        divisorCount = 0
        if (temp % 2 == 0)
            divisorCount = divisorCount + 1
            while (temp % 2 == 0)
                temp = temp / 2
            end
        end

        oddDivisor = 3
        while (temp >= oddDivisor)
            if (temp % oddDivisor == 0)
                divisorCount = divisorCount + 1
                if (divisorCount > 2)
                    break
                end
                while (temp % oddDivisor == 0)
                    temp = temp / oddDivisor
                end
            end
            oddDivisor = oddDivisor + 2
        end

        if divisorCount == 2
            solution = solution + 1
        end
    end
end

puts solution


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


# -*- encoding: utf-8 -*-
$stdin = File.open("input.txt")
$stdout = File.open("output.txt", "w")

n = gets.chomp.to_i
a = gets.chomp.split(" ").map{|n| n.to_i}

chest = 0
biceps = 0
back = 0

0.step(n, 3) do |idx|
  chest += a[idx] if not a[idx].nil?
  biceps += a[idx + 1] if not a[idx + 1].nil?
  back += a[idx + 2] if not a[idx + 2].nil?
end

if chest > biceps and chest > back
  puts "chest"
elsif biceps > chest and biceps > back
  puts "biceps"
else
  puts "back"
end


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

n.times{|i| x[i%3]+=a[i] }

case x.each_with_index.max[1]
	when 0
	puts "chest"
	when 1
	puts "back"
	when 2
	puts "biceps"
end


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

body_part = ["chest", "biceps", "back"]

answer = a.each_slice(3).to_a
(1 + a.length % 3).times { |_| answer[-1].insert(-1, 0) }

puts body_part[answer.map { |arr| arr.reduce(:+) }.each_with_index.max[1]]


#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']

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

n = inp[0]
n.times do
  n, d, k = inp
  onf = na(d+1)
  t = inp
  tail = 0
  min = 9999999
  now = 0
  t.size.times do |i|
    if(onf[t[i]] == 0)
      now += 1
    end
    onf[t[i]] += 1
    if(i-k >= 0)
      onf[t[i-k]] -= 1
      if(onf[t[i-k]] == 0)
        now -= 1
      end
      min = [now,min].min
    end
  end
  min = [now,min].min
  p min
end


gets
s=gets.chomp.downcase
p (s.each_char.to_a.uniq.size==26)?"YES":"NO"


stringLen = gets.chomp.to_i
str = gets.chomp.upcase

uniqLen = str.split('').uniq.length

if stringLen == uniqLen
    puts 'YES'
else
    puts 'NO'
end


a=gets.chomp.to_i
b=a%7
c=(a/7)*2
if b==0
puts"#{c} #{c}"
elsif b==1
puts "#{c} #{c+1}"
else
puts "#{c} #{c+2}"
end


n = gets.chomp.to_i
days = 0
if n <= 2
    puts "0 #{n}"
elsif n <= 5
    puts "0 2"
elsif n < 7
    puts "#{n - 5} 2"
else
    min_offs = 0
    remaining_days = n
    begin
        days += 7
        min_offs += 2
        remaining_days -= 7
    end while remaining_days >= 7
    max_offs = min_offs
    if remaining_days > 5
        max_offs += 2
    elsif remaining_days < 2
        max_offs += remaining_days
    else
        max_offs += 2
    end
    puts "#{min_offs} #{max_offs}"
end


n = gets.to_i
min = n/7*2
max = min
n%=7

if n < 7
	max += n%7 > 2 ? 2 : n%7
	puts "#{min} #{max}"
end


input = gets.chomp.to_i

portion = if input % 7 > 2
	2
else
	input % 7
end

min = (input / 7) * 2
max = ((input / 7) * 2) + portion

puts "#{min} #{max}"


n = gets.to_i

x = (n / 2.0).ceil - 1.0

puts x


s = gets.chomp.to_i
case s
when 1
  puts 2 ## ok
when 2
  puts 3
when 3
  puts 1 ## ok
when 4
  puts 2 ## ok
when 5
  puts 3
end


def neighbor(a,b)
  return (a[0].to_i - b[0].to_i).abs == 1 && a[1] == b[1]
end
def neighbor2(a,b)
  return (a[0].to_i - b[0].to_i).abs == 2 && a[1] == b[1]
end

t = gets.split.sort

if t[0] == t[1] && t[1] == t[2]
  p 0
elsif neighbor(t[0],t[1]) && neighbor(t[1],t[2])
  p 0
elsif t[0] == t[1] || t[1] == t[2]
  p 1
elsif neighbor(t[0],t[1]) || neighbor(t[1],t[2])
  p 1
elsif neighbor2(t[0],t[1]) || neighbor2(t[0],t[2]) || neighbor2(t[1],t[2])
  p 1
else
  p 2
end


s = gets.chomp.split.sort
ans = 2
if s[0] == s[1]
    if s[1] == s[2]
        ans = 0
    else
        ans = 1
    end
elsif s[1] == s[2]
    ans = 1
else
    a = Hash.new
    a["s"] = 0
    a["p"] = 1
    a["m"] = 2
    b = Array.new(3).map{Array.new(10,0)}
    3.times do |i|
        b[a[s[i][1]]][s[i][0].to_i] += 1
    end
    3.times do |i|
        c = 1
        (2..9).each do |j|
            if b[i][j] == 1 && b[i][j-1] == 1
                c += 1
                ans = [ans,3-c].min
            else
                c = 1
            end
        end
        (2..8).each do |j|
            if b[i][j-1] == 1 && b[i][j+1] == 1
                ans = 1
            end
        end
    end
end
puts ans


#!/usr/bin/env ruby

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

#

to_one_cache = { 0 => [0],
				 1 => [1]}
stack = []
c = n
while c > 1
  stack << c
  c = c / 2
end

#p stack

list_one = [c]
while d = stack.pop
  list_one = list_one + [d&1] + list_one
end

#p list_one
puts list_one[l - 1..r - 1].count(1)


#Bear and Big Brother
l_weight, b_weight = gets.chomp.split
y = 0
until l_weight > b_weight
  l_weight *= 3
  b_weight *= 2
  y += 1
end
puts y


a = gets.chomp.to_i
b = gets.chomp.to_i
c = 0
while a <= b
    a = a * 3
    b = b * 2
    c = c + 1
end

puts c


file = File.exist?('in.in') ? File.new('in.in', 'r') : STDIN

a,b = file.gets.split

years = 0
if a == b
  puts 1
else
  while a <= b
    a *= 3
    b *= 2
    years += 1
  end
  puts years
end


a, b = gets.chomp.split.map(&:to_i)
i =0
while(true) do
  a = a*3
  b = b*2
  i = i+1
  if(a>=b) then
    break
  end
end
print(i,"\n")


n=gets.to_i
(p 1;exit)if n<10
x=10**Math.log10(n).to_i
p (x-n)%x


n=gets.to_i
(p 1;exit)if n<10
x=10**Math.log10(n).to_i
n=(x-n-1)%x+1


def scan;gets.split.map(&:to_i);end
s = gets.chomp
sn = s[0].succ + '0'*s[1..-1].size
p sn
p sn.to_i - s.to_i


require 'prime'

class Integer
  def divisor_list
    return [] if self <= 0
    return [1] if self == 1

    prime_division.map.with_index { |(base, k), i|
      s = i.zero? ? 0 : 1
      (s..k).map { |n| base ** n }
    }.inject { |res, e| res + res.flat_map { |t| e.map { |v| t * v } } }.sort
  end
end

A, B = gets.split.map(&:to_i).sort

if A == B
  puts 0
  exit
end

k = B - A
min_lcm = Float::INFINITY
ans = Float::INFINITY

p k.divisor_list
k.divisor_list.each do |x|
  d = x * (A / x.to_f).ceil - A

  lcm = (A + d).lcm(B + d)

  if min_lcm > lcm
    min_lcm = lcm
    ans = d
  end
end

puts ans


require 'prime'

a,b = gets.split.map(&:to_i)
if a != b
    g = (a-b).abs
else
    g = a
end
x = Prime.prime_division(g)
m = a.lcm(b)
ans = 0

$p = []

def div(c,d,i)
    if i == d.size
        $p << c
    else
        div(c,d,i+1)
        d[i][1].times do |j|
            div(c*(d[i][0]**(j+1)),d,i+1)
        end
    end
end

div(1,x,0)
$p.sort!
$p.each do |k|
    l = (k-a%k)%k
    if m > (a+l)*(b+l)/k
        m = (a+l)*(b+l)
        ans = l
    end
end
puts ans


n,c=gets.split.map &:to_i
mas=gets.split.map &:to_i
i,k=0,0
while(i<mas.length-1)
  if (mas[i]-mas[i+1]) > k
    k=mas[i]-mas[i+1]
  end
  i+=1
end
puts k-c


n, c = gets.chomp.split(/ /).map(&:to_i)
prices = gets.chomp.split(/ /).map(&:to_i)
profits = []
if c > 0
    (1...n).each do |day|
        current_buy = prices[day - 1]
        next_sell = prices[day]
        profit = current_buy - next_sell - c
        profits << profit if profit > 0
    end
    puts profits.max
else
    puts "0"
end


n, c = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
diff = 0
(n - 1).times do |i|
  diff = [diff, a[i] - a[i + 1]].max
end
if diff > c
  puts diff - c
else
  puts diff
end


size=gets.chomp

rows= size.split(" ")[0].to_i
columns= size.split(" ")[1].to_i
a=[]
for i in (0...rows.to_i) do
  line=gets.chomp
  a<<line.split("")
end

count_pigs_eaten=0
for i in (0...rows) do
  for j in (0...columns) do
    if a[i][j]=="W"
      if i==0 #tpd
        if a[i][j-1]=="P" && j-1>0
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j-1]="."
        elsif a[i][j+1]=="P" && j+1<columns
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j+1]="."
        elsif a[i+1][j]=="P"
          count_pigs_eaten+=1
          a[i][j]="."
          a[i+1][j]="."
        end
      elsif i==rows-1 #tpt
        if a[i][j-1]=="P" && j-1>0
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j-1]="."
        elsif a[i][j+1]=="P" && j+1<columns
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j+1]="."
        elsif a[i-1][j]=="P"
          count_pigs_eaten+=1
          a[i][j]="."
          a[i-1][j]="."
        end

      else #tptd
        if a[i][j-1]=="P" && j-1>0
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j-1]="."
        elsif a[i][j+1]=="P" && j+1<columns
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j+1]="."
        elsif a[i+1][j]=="P"
          count_pigs_eaten+=1
          a[i][j]="."
          a[i+1][j]="."
        elsif a[i-1][j]=="P"
          count_pigs_eaten+=1
          a[i][j]="."
          a[i-1][j]="."
        end
      end

    end
  end
end

p count_pigs_eaten


size=gets.chomp

rows= size.split(" ")[0].to_i
columns= size.split(" ")[1].to_i
a=[]
for i in (0...rows.to_i) do
  line=gets.chomp
  a<<line.split("")
end

count_pigs_eaten=0
for i in (0...rows) do
  for j in (0...columns) do
    if a[i][j]=="W"
      if i==0 #tpd
        if a[i][j-1]=="P" && j-1>=0
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j-1]="."
        elsif a[i][j+1]=="P"
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j+1]="."
        elsif a[i+1][j]=="P"
          count_pigs_eaten+=1
          a[i][j]="."
          a[i+1][j]="."
        end
      elsif i==rows-1 #tpt
        if a[i][j-1]=="P" && j-1>=0
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j-1]="."
        elsif a[i][j+1]=="P"
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j+1]="."
        elsif a[i-1][j]=="P"
          count_pigs_eaten+=1
          a[i][j]="."
          a[i-1][j]="."
        end

      else #tptd
        if a[i][j-1]=="P" && j-1>=0
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j-1]="."
        elsif a[i][j+1]=="P"
          count_pigs_eaten+=1
          a[i][j]="."
          a[i][j+1]="."
        elsif a[i+1][j]=="P"
          count_pigs_eaten+=1
          a[i][j]="."
          a[i+1][j]="."
        elsif a[i-1][j]=="P"
          count_pigs_eaten+=1
          a[i][j]="."
          a[i-1][j]="."
        end
      end

    end
  end
end

p count_pigs_eaten


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

p = 0
for i in 0...n
  for j in 0...m
    p += 1 if g[i][j] == 'W' && [j > 0 ? g[i][j - 1] : nil,
                                 j + 2 < n ? g[i][j + 1] : nil,
                                 i > 0 ? g[i - 1][j] : nil,
                                 i + 2 < m ? g[i + 1][j] : nil].include?('P')
  end
end
puts p


line = gets.split
n = line[0].to_i
m = line[1].to_i
field = []

0.upto(n - 1) do |i|
  field[i] = gets.chomp.split('')
end

pigs = 0
pos = []

0.upto(n - 1) do |x|
  0.upto(m - 1) do |y|

    unless pos.empty?
      field[pos[0]][pos[1]] = ""
      pigs = pigs + 1
      pos = []
    end

    if field[x][y] == "W"

      if x > 0 and field[x - 1][y] == "P"
        pos = [x - 1, y]

      elsif x < n - 1 and field[x + 1][y] == "P"
        pos = [x + 1, y]

      elsif y > 0 and field[x][y - 1] == "P"
        pos = [x, y - 1]

      elsif y < m - 1 and field[x][y + 1] == "P"
        pos = [x, y + 1]
      end
    end
  end
end

puts pigs


n = gets.chomp.to_i

for i in 0..n-2
	for j in 0..n-2
		if i == 0
			print " " if j > 0
			print j+1
		else
			if j == 0
				print i
			else
				print " "
				print ((i+1)*(j+1)).to_s(n)
			end
		end
	end
	puts ""
end


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

require 'prime'

n,k = ascan

h = {}
Prime.prime_division(n).each{|k,v| h[k]=v}

h2 = h[2] || 0
h5 = h[5] || 0

h10 = [h2,h5].min

if h10 >= k
    p n
else
    n *= 2**(k-h2)
    n *= 5**(k-h5)
    p n
end


size = gets.split(' ').collect{|i| i.to_i}
b1 = gets.split(' ').collect{|i| i.to_i}
b2 = gets.split(' ').collect{|i| i.to_i}

board_area = size[0] * size[1]
b1_area = b1[0] * b1[1]
b2_area = b2[0] * b2[1]

if(board_area < b1_area + b2_area)
  puts 'YES'
else
  puts 'NO'
end


# Fitting Arts

inp = Array.new
for i in 0..2
	inp.push (gets.chomp.split().map { |e| e.to_i  }) ###
end

x = Array.new
for i in 0..2
	x.push(inp[i][0].to_i)
	x.push(inp[i][2].to_i)
end

input = Array.new(3) { |i| Array.new(2) }
for i in 0..5
	input[i/2][i%2] = x[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


board = gets.chomp.split(' ').map {|item| item.to_i}
painting = gets.chomp.split(' ').map {|item| item.to_i}
painting_2 = gets.chomp.split(' ').map {|item| item.to_i}
flag = false

if painting[0] + painting_2[0] == board[0]
	if painting[1] <= board[1] && painting_2[1] <= board[1]
		flag = true
	end
end

if painting[0] + painting_2[1] == board[0]
	if painting[1] <= board[1] && painting_2[0] <= board[1]
		flag = true
	end
end

if painting[1] + painting_2[0] == board[0]
	if painting[0] <= board[1] && painting_2[1] <= board[1]
		flag = true
	end
end

if painting[1] + painting_2[1] == board[0]
	if painting[0] <= board[1] && painting_2[0] <= board[1]
		flag = true
	end
end

if painting[0] + painting_2[0] == board[1]
	if painting[1] <= board[0] && painting_2[1] <= board[0]
		flag = true
	end
end

if painting[0] + painting_2[1] == board[1]
	if painting[1] <= board[0] && painting_2[0] <= board[0]
		flag = true
	end
end

if painting[1] + painting_2[0] == board[1]
	if painting[0] <= board[0] && painting_2[1] <= board[0]
		flag = true
	end
end

if painting[1] + painting_2[1] == board[1]
	if painting[0] <= board[0] && painting_2[0] <= board[0]
		flag = true
	end
end

puts "YES" if flag
puts "NO" if !flag


def calc(n,m,d)
	k = (n - 1) / d + 1
    t = (m - 1) / d + 1
    i = (n - 1) % d + 1
    j = (m - 1) % d + 1
    return k * t * i * j
end

def countone(x, d)
	dd = ((x-1)/d)+1
	return (((x-1) % d)+1)*dd
end

s = gets.chomp
ns,ms,ds = s.split(/ /)
n = ns.to_i
m = ms.to_i
d = ds.to_i
ret = n*m
if (n > d) || (m > d)
	if (n <= d)
		ni = 2
	else
		ni = countone(n,d)
	end
	if (m <= d)
		mi = 2
	else
		mi = countone(m,d)
	end
	ret = ni*mi
end
puts ret


def num_solutions(a1, b1, c1, a2, b2, c2)
  if a2 == 0 and b2 == 0
    if c2 != 0
      return 0
    else
      if a1 == 0 and b1 == 0
        return (c1 == 0 ? -1 : 0)
      elsif a1 == 0 or b1 == 0
        return 1
      else
        return -1
      end
    end
  end
  det = a1*b2-a2*b1
  return 1 if det != 0
  det2 = a1*c2-a2*c1
  det3 = b1*c2-b2*c1
  return ((det2 == 0  and det3 == 0) ? -1 : 0)
end

a1, b1, c1 = gets.split.map(&:to_i)
a2, b2, c2 = gets.split.map(&:to_i)

if a1 == 0 and b1 == 0
  a1, b1, c1, a2, b2, c2 = a2, b2, c2, a1, b1, c1
end

puts num_solutions(a1, b1, c1, a2, b2, c2)


def solve(n,k)
        big=n;small=k
        a=0
        while big!=1||small!=1
                big-=small
                if big<small then
                        big,small=small,big
                end
                a+=1
                if small==0 then return 9999999 end
        end
        a
end
# 最後の数字を家庭する
n=gets.to_i
ans=99999999

for i in 1..n
        ans=[ans,solve(n,i)].min
end
p ans


def solve(n,k)
        big=n;small=k
        a=0
        while big!=1||small!=1
                t=big/small
                if big%small==0 then t-=1 end
                if t==0 then return 99999999 end
                a+=t
                big-=t*small
                if big<small then
                        big,small=small,big
                end
                if small==0 then return 9999999 end
        end
        a
end
# 最後の数字を家庭する
n=gets.to_i
ans=99999999

for i in 1..n
        if ans.gcd(i)!=1 then next end
        ans=[ans,solve(n,i)].min
        # p [n,i,solve(n,i)]
end
p ans

[nomeaning@ne