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

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

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

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


#!/usr/bin/env ruby

require 'matrix'

rows = 8.times.map do gets.chars.to_a end
MATRIX = Matrix.rows(rows)

def win_in_colA(col_no)
  (0..7).each do |row_no|
    if MATRIX[row_no, col_no] == 'W'
      return row_no
    elsif MATRIX[row_no, col_no] == 'B'
      return 9
    end
  end
  return 9
end

def win_in_colB(col_no)
  (0..7).to_a.reverse.each do |row_no|
    if MATRIX[row_no, col_no] == 'B'
      return 7-row_no
    elsif MATRIX[row_no, col_no] == 'W'
      return 9
    end
  end
  return 9
end

steps_for_A = (0..7).map do |col_n|
  win_in_colA(col_n)
end

steps_for_B = (0..7).map do |col_n|
  win_in_colB(col_n)
end

who_won = (steps_for_B.min < steps_for_A.min) ? 'B' : 'A'
puts who_won


n = gets.to_i
least = Array.new()
least.push(1)
least.push(2)
if(n == 2)
	print 1,"\n"
else
	i = 2
	until (least[i-2] + least[i-1] > n)
		least.push(least[i-2] + least[i-1])
		i = i + 1
	end
	print i-1,"\n"
end


n = gets.to_i
a,b,i = 0,1,0
while a<=n
  a,b, = b, a+b
  i +=1
end
p i-3


line = gets
numbers = line.split(" ")
chips =numbers[1].to_i
peopleNum =numbers[0].to_i
i = 1
while chips > -1
if chips - i > -1
 chips -= i
else
 break
end
 i += 1
 if i == peopleNum +1
  i = 1
 end
end
puts chips


n,m=gets.split.map(&:to_i)
(1..n).cycle{|_|break if m<_;m-=_}
p m


n,m = gets.chomp.split(/ /).map!{|x| x.to_i}
i = 1
while true
  break if m < i
  m -= i
  i += 1
  i = 1 if n < i
end
puts m


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

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

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


N, k = gets.split.map(&:to_i)
S = gets.chomp.split('').map{|c| c.ord - 'a'.ord + 1}.sort
sum = 0
prev = -1
N.times do |i|
    if prev + 2 <= S[i]
        prev = S[i]
        sum += S[i]
        k -= 1
        break if k == 0
    end
end
puts k == 0 ? sum : -1


n, k = gets.strip.split.map(&:to_i)
hsh = {}
('a'..'z').each.with_index{|c, i| hsh[c]=i+1 }
str = gets.strip
ary = []
str.each_char{|c| ary << hsh[c]}
ary.sort!

i = 0
ans=[ary[i]]
2.upto(k).each do
  (i+1).upto(n-1).each do |j|
    if ary[j]-ary[i] > 1
      ans << ary[j]
      i = j
      break
    end
  end
end

if ans.size == k
  puts ans.inject(:+)
else
  puts -1
end


a,b=gets.split.map{|e| e.to_i}
d=2

ans=""
while a>=0 && b>=0
	a-=(d-1)
	if a<0 then
		ans="Vladik"
		break
	end
	b-=d
	if b<0 then
		ans="Valera"
		break
	end

	d+=2
end
puts ans


a,b=gets.split(" ").map(&:to_i)
p=1
while a>=0 && b>=0
	if a>=2*p-1
		a-=2*p-1
	else
		puts "Vladik"
		break
	end
	if b>=2*p
		b-=2*p
	else
		puts "Valera"
		break
	end
	p+=1
end


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

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


arr=[]
c=" abcdefgh"
(1..8).map{|e| (1..8).map{|i| arr<<[e,i]}}
s=gets.chomp

x=c.index(s[0])
y=s[1].to_i

ans=0
(-1..1).each{|dx|
	(-1..1).each{|dy|

		ans+=1 if arr.index([x+dx,y+dy])
	}
}
puts ans-1


cmd=gets.chomp
if cmd[0]=='a' || cmd[0]=='h'
  if cmd[1]=='1' || cmd[1]=='8'
    puts "3"
  else
    puts "5"
  end
else
  if cmd[1]=='1' || cmd[1]=='8'
    puts "5"
  else
    puts "8"
  end
end


n = gets.to_i

k = 2
s = 0
n.times do
   s += k
   k *= 2
end

puts s


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


n = gets.chomp.to_i

ans = 0

(1..n).each do |i|
ans = ans + (2**i)
end

puts ans


a, b = STDIN.readline.split.collect {|s| s.to_i }
if b > a
  puts 0
elsif b == a
  puts 'infinity'
else
  c = a-b
  count = 0
  x = 1
  while x*x <= c
    if c%x == 0
      y = c/x
      if y > b
        count += 1
      end
      if x != y and x > b
        count += 1
      end
    end
    x += 1
  end
  puts count
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 x%i == 0 && i>b
    ans+= 1 if x%i == 0 && x/i>b && x!=i**2
  end
  puts ans
end


def divisors_of(number)
  divisors = []
  n = 1
  while n <= number ** 0.5
    if number.modulo(n) == 0
      divisors << n
      divisors << number / n unless number / n == n
    end
    n += 1
  end
  divisors.sort
end

a,b = gets.split.map(&:to_i)
if a == b
  puts "infinity"
elsif a < b
  puts "0"
else
  puts divisors_of(a-b).select {|x| x > b}.count
end


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


n = gets.to_i
k = gets.to_i
a = gets.to_i
b = gets.to_i

s=0

unless k==1

     until n==0

         if n%k==0

             unless ((n-(n/k))*a) >= b
                  s=s+(n-(n/k))*a
                  n=n/k

             else
                  s=s+b
                  n=n/k
             end

         elsif n<k
             s=s+(n-1)*a
             n=1
             break

         else
             s=s+(n-(k*(n/k)))*a
             n=(n/k)*k
         end

     end

print s

else
     s=(n-1)*a
     print s
end


n = gets.to_i
k = gets.to_i
a = gets.to_i
b = gets.to_i
ans = 0
d = b/a
until n < k || n==1 || k==1
  m = n % k
  q = n / k

  n -= m
  ans += a * m

  ans += [a * (n-q),b].min
  n -= n-q
end
ans += a * (n-1)
puts ans


n = gets.to_i

k = Math.sqrt(n).to_i

puts k * 4 + 2 * ((n - k**2 + k - 1) / k)


a = gets.to_i
x = (a**0.5).ceil
y = a/x
if x*y == a
 p 2*(x+y)
else
 while x*y <= a
   y += 1
 end
 p 2*(x+y)
end


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

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

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

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

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

STDOUT.puts "YES"


n = gets.to_i
s = gets.chomp

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

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

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


def solve(a,b)
        x=[]
        y=[]
        a.length.times do |i|
                x.push(a[i].to_i)
                y.push(b[i].to_i)
        end
        x.sort!
        y.sort!

        a.length.times do |i|
                return false if x[i]>=y[i]
        end
        return true
end

n=gets.to_i
a=gets.split[0]
x=a[0..n-1]
y=a[n..2*n-1]
if solve(x,y) || solve(y,x)
        puts "YES"
else
        puts "NO"
end


#!/usr/bin/ruby
a,b=gets.split.map(&:to_i)
p Enumerator.new{|y|
	(1..1/0.0).each{|i|
		1.upto(i){|j|
			y<<('1'*j+'0'+'1'*(i-j)).to_i(2)
		}
	}
}.lazy.drop_while{|e|e<a}.take_while{|e|e<=b}.count


arr1 = gets.chomp.split(/ /).map(&:to_i)
a = arr1[0]
b = arr1[1]
str1 = a.to_s(2)
str2 = b.to_s(2)

index1 = str1.length
index2 = str2.length
count=0
loop do
	break if index1>index2
	index3=0
	temp = ""
	index1.times do
		temp=temp+"1"
	end
	loop do
		break if index3>=index1
		temp[index3] = "0"
		if temp.to_i(2) >= a && temp.to_i(2)<=b && temp[0] == "1"
			#puts temp
			count+=1
		end
		temp[index3] = "1"
		index3+=1
	end
	index1+=1
end

puts count
=begin
temp = gets.to_i

str = temp.to_s(2)

puts str

puts str.to_i(2)
=end


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

cnt = 0
for l in 2..60
  n = 2**l - 1
  for i in 0...l - 1
    m = n - 2**i
    if a <= m && m <= b
      cnt += 1
    end
  end
end

puts cnt


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
        case curr
        when 1
            if a <= b
                dist = a
                curr = 2
            else
                dist = b
                curr = 3
            end
        when 2
            if a <= c
                dist = a
                curr = 1
            else
                dist = c
                curr = 2
            end
        when 3
            if c <= b
                dist = c
                curr = 2
            else
                dist = b
                curr = 1
            end
        end
        ans += dist
        i += 1
    end
end
puts ans


n = gets.to_i
a = gets.to_i
b = gets.to_i
c = gets.to_i
n -= 1
x = [a, b, c].min
tot = 0
if n >= 1
  tot += [a, b].min
  tot += (n - 1) * x
end
puts tot


n = gets.to_i
a = gets.split.map &:to_i
ll = nil
l = a[0]
ans = 0
(1...n).each do |i|
  case l
  when 1 # circle
    case a[i]
    when 2 # triangle
      ans += 3
      ans -= 1 if ll == 3
    when 3 # square
      ans += 4
    end
  when 2 # triangle
    case a[i]
    when 1 # circle
      ans += 3
    when 3 # square
      ans = -1
      break
    end
  when 3 # square
    case a[i]
    when 1 # circle
      ans += 4
    when 2 # triangle
      ans = -1
      break
    end
  end
  ll = l
  l = a[i]
end
if ans < 0
  puts "Infinite"
else
  puts "Finite"
  puts ans
end


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 z==2 then sum -= 1 end }

print "Finite\n#{sum}"


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

greatest_common_divider = width.gcd(height)
normalized_width = width/greatest_common_divider
normalized_height = height/greatest_common_divider

a_times = (a/normalized_width).floor
b_times = (b/normalized_height).floor

puts [a_times, b_times].min


def maxx(x, y)
  x > y ? x : y
end

def minn(x, y)
  x < y ? x : y
end

def get_nod(k, j)
  mx = maxx(k, j)
  mn = minn(k, j)

  res = mx % mn;

  if res == 0
    return mn;
  else
    get_nod(res, mn);
  end
end

w, h, x, y = gets.chomp.split(' ').map(&:to_i)

nod = get_nod(x, y);
x = x / nod;
y = y / nod;

f = w / x;
s = h / y;

puts minn(f, s);


a,b,x,y = gets.chomp.split.map(&:to_i)
d = [a*y,b*x].min
k = x.lcm(y)
puts d/k


s=gets
puts (["Danil", "Olya", "Slava", "Ann", "Nikita"].map{|x| s.scan(x).count}.inject(:+)==1 ? "YES" : "NO")


names = %w(Danil Olya Slava Ann Nikita)
line = gets.chomp

count = names.map { |name| line.scan(name).count }.reduce(&:+)
puts count == 1 ? 'YES' : 'NO'


s=gets;a=%w(Danil Olya Slava Ann Nikita).select{|e|s.index e};puts a.size==1&&s.scan(a[0]).size==1?:YES: :NO


n = gets.to_i
a = gets.split.map(&:to_i).uniq.sort
if a.size > 3 || (a.size == 3 && a[2]-a[1] != a[1]-a[0])
    puts "-1"
elsif a.size == 1
    puts 0
elsif a.size == 3
    puts a[1]-a[0]
elsif (a[1]-a[0])%2 == 0
    puts (a[1]-a[0])/2
else
    puts a[1]-a[0]
end


N = gets.to_i
A = gets.split.map(&:to_i)

ans = Float::INFINITY

(0..200).each do |target|
  d = -1

  list = A.select { |a| a != target }

  if list.empty?
    d = 0
  else
    d = (list[0] - target).abs

    list.each do |a|
      if (target - a).abs != d
        d = -1
        break
      end
    end
  end

  if d != -1
    ans = d if ans > d
  end
end

if ans == Float::INFINITY
  puts -1
else
  puts ans
end


n = gets().to_i()
if(n==0) then puts 0
elsif((n&1)==1) then puts((n+1)/2)
else puts(n+1) end


class Fixnum
  def compare(fixnum)
    return -1 if self < fixnum
    return 1 if self > fixnum
    return 0 if self == fixnum
  end
end

class Tram
  attr_reader(:s, :x1, :x2, :t1, :t2, :p, :d)
  def initialize(s, x1, x2, t1, t2, p, d)
    @s = s
    @x1 = x1
    @x2 = x2
    @t1 = t1
    @t2 = t2
    @p = p
    @d = d
  end

  def direction
    x2.compare(x1)
  end

  def by_foot
    (x1 - x2).abs * t2
  end

  def wait_time
    if(direction == d)
      if(d == -1)
        if(p < x1)
         (((p + s) + (s - x1)))  * t1
        else
          (x1 - p).abs * t1
        end
      else
        if(p <= x1)
          (x1 - p).abs * t1
        else
         ((s - p) + s + x1)  * t1
        end
      end
    else
      if(d == -1)
        (p * t1) + (x1 * t1)
      else
        ((s - p) * t1) + ((s - x1) * t1)
      end
    end
  end

  def tram_time
    (x1 - x2).abs * t1
  end

  def by_tram
    wait_time + tram_time
  end
end

def main
  s, x1, x2 = gets.split(" ").map(&:to_i)
  t1, t2 = gets.split(" ").map(&:to_i)
  p, d = gets.split(" ").map(&:to_i)
  tram = Tram.new(s, x1, x2, t1, t2, p, d)
  cands = [tram.by_tram, tram.by_foot]
  puts cands.select{|x| x >= 0 }.min
end

main


s = gets.chomp
ans = ""
while (s.length > 0) do
    if s.length % 2 == 0
        ans += s.slice!(-1)
    else
        ans += s.slice!(0)
    end
end
puts ans.reverse


str = gets.chomp

newstr = ""

len = str.length
while len > 0
  if len%2==0
    newstr = str[-1] + newstr
    str = str[0..-2]
  else
    newstr = str[0] + newstr
    str = str[1..-1]
  end
  len -= 1
end

puts newstr


t = gets.strip
s = ''
while t.size > 0
    if t.size%2 == 0
        s = t[-1] + s
        t = t[0..-2]
    else
        s = t[0] + s
        t = t[1..-1]
    end
end
puts s


require 'date'

d = Date.new(1990, 1, 1)
list = (12*12).times.map do
  res = d.prev_day.day
  d = d.next_month
  res.chr
end.join

n = gets.to_i
arr = gets.split.map(&:to_i).map(&:chr).join

if list.include?(arr)
  puts "YES"
else
  puts "NO"
end


# Input
n = gets.to_i

# Processing
result = case n % 4
  when 1 then 1
  when 2 then 1
  when 3 then 0
  when 0 then 0
end

# Output
puts result


n=gets.to_i
res=n
(1..4).each{|i|
    [2, 3, 5, 7].combination(i).each{|j|
        x=j.inject :*
        if j.size.odd?
            res-=n/x
        else
            res+=n/x
        end
    }
}
p res


def read_ints
  gets.chomp.split.map {|s| s.to_i}
end

n, m = read_ints
a = read_ints
b = read_ints

ans = []
amb = false

def shares_one p, q, r, s
  ret = nil
  if p == r || p == s then ret = p end
  if q == r || q == s then ret = (ret == nil ? q : nil) end
  return ret
end

for i in 0...n do
  for j in 0...m do
    x = shares_one a[i * 2], a[i * 2 + 1], b[j * 2], b[j * 2 + 1]
    if x != nil then
      cand = []
      for k in 0...m do
        y = shares_one a[i * 2], a[i * 2 + 1], b[k * 2], b[k * 2 + 1]
        if y != nil then cand.push y end
      end
      for k in 0...n do
        y = shares_one a[k * 2], a[k * 2 + 1], b[j * 2], b[j * 2 + 1]
        if y != nil then cand.push y end
      end
      cand.uniq!
      if cand.length == 1 then
        ans.push cand[0]
      else
        amb = true
      end
    end
  end
end

ans.uniq!
if amb then
  puts -1
elsif ans.length == 1 then
  puts ans[0]
else
  puts 0
end


N, M = gets.split.map(&:to_i)
ast = gets.split.map(&:to_i)
bst = gets.split.map(&:to_i)
as, bs = [], []
N.times do |i|
    as[i] = ast.shift(2)
end
M.times do |i|
    bs[i] = bst.shift(2)
end

acand = []
as.each do |a|
    cand = []
    bs.each do |b|
        ab = a & b
        if ab.length == 1
            cand.push(ab[0])
        end
    end
    cand.uniq!
    if cand.length > 1
        puts -1
        exit
    else
        acand.concat(cand)
    end
end

bs.each do |b|
    cand = []
    as.each do |a|
        ab = a & b
        if ab.length == 1
            cand.push(ab[0])
        end
    end
    cand.uniq!
    if cand.length > 1
        puts -1
        exit
    end
end

acand.uniq!
if acand.length == 1
    puts acand[0]
elsif acand.length > 1
    puts 0
else
    puts -1
end


n,x,y,c=gets.split.map &:to_i
l,r,x,y=-1,n*2,x-1,y-1
while l+1<r do
  m=(l+r)/2
  t=m*m+(m+1)**2
  4.times do
    t-=(m-x)**2 if x<m
    t+=(m-x-y-1)*(m-x-y)/2 if x+y+2<=m
    x,y=y,n-1-x
  end
  if t>=c then r=m else l=m end
end
p r


def square(x)
    x * x
end

def triangle(x)
    x * (x + 1) / 2
end

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

n, x, y, c = gets.split.map(&:to_i)
low, high = 0, (n - 1) * 2
while low < high
    middle = (low + high) / 2
    if solve(n, x, y, middle) >= c
        high = middle
    else
        low = middle + 1
    end
end
p high


s=gets.chomp.sub(/0+$/,'')
puts s==s.reverse ? :YES : :NO


s = gets()
a = s.to_i
s.reverse!
b = s.to_i

while(1) do
    if(a%10==0)
        a=a/10
    else
        break
    end
end

if a==b
    puts "YES"
else
    puts "NO"
end


s=gets.chomp

li=s.count('-')
pe=s.count('o')

if pe==0
  puts 'YES'
else
  if li%pe==0
    puts 'YES'
  else
    puts 'NO'
  end
end


s=gets.chomp
c=s.count(?o)
puts c>0&&s.size%c>0?:No: :Yes


PEARL = 'o'
LINK = '-'

necklace = STDIN.gets.chomp

def adjustable?(necklace)
  pearl_count = necklace.count(PEARL)
  return true if pearl_count == 0
  link_count = necklace.count(LINK)

  link_count % pearl_count == 0
end

if adjustable?(necklace)
  puts 'YES'
else
  puts 'NO'
end


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


def automaton?(s,t)

    i = 0

    t.each do |tc|
        while (i < s.count)
            if s[i] == tc
                break
            end
            i = i + 1
        end
        if i == s.count
            return false
        else
            i = i + 1
        end
    end

    return true;

end

s = gets.strip().split("")

t = gets.strip().split("")

if (s.sort == t.sort)
    puts "array"
elsif automaton?(s,t)
    puts "automaton"
elsif automaton?(s.sort,t.sort)
    puts "both"
else
    puts "need tree"
end


s = gets.split('')
t = gets.split('')

def automaton sd,td
  k = 0
  sd.each do |i|
    if i == td[k]
      k += 1
    end
    if k == td.length
      return true
    end
  end

  return false
end

if automaton s,t
  puts 'automaton'
elsif s.sort! == t.sort!
  puts 'array'
elsif automaton s.sort!,t.sort!
  puts 'both'
else
  puts '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..-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


n = gets.to_i
s = gets.strip
feasible = false
1.upto(n) do |i|
  0.upto(n-1) do |j|
    okay = true
    0.upto(4) do |k|
      okay = false if s[j+i*k] != "*"
    end
    feasible = true if okay
  end
end
if feasible
  puts "yes"
else
  puts "no"
end


n = gets.to_i
s = gets
flag = 0
for dl in 1..n
	for q in 0..n-1 do
		sov = 0;
		while q < n and s[q]=='*'
			sov+=1
			q += dl
		end
		if sov == 5 then flag = 1
		end
	end

end

if flag == 1 then puts"yes"
else
puts"no"

end


H=Hash.new 0
gets
gets.chomp.chars{|c|H[c]+=1}
p [H['U'],H['D']].min+[H['L'],H['R']].min<<1


n= gets.to_i
a = gets.split("")
a.pop
a.each_index do |i|
	move = a[i]
	if move == 'L'
		k = a.index 'R'
		if k
			a[i] = 0
			a[k] = 0
		end
	end
	if move == 'R'
		k = a.index 'L'
		if k
			a[i] = 0
			a[k] = 0
		end
	end
	if move == 'U'
		k = a.index 'D'
		if k
			a[i] = 0
			a[k] = 0
		end
	end
	if move == 'D'
		k = a.index 'U'
		if k
			a[i] = 0
			a[k] = 0
		end
	end
end
count = 0
a.each do |move|
	count +=1 if move ==0
end
print(count)


gets
s = gets
p ([s.count('L'), s.count('R')].min + [s.count('U'), s.count('D')].min) * 2


a=gets.split.map(&:to_i).sort
if a.count(a[0])>=a[0] || a==[2, 4, 4]
    puts "YES"
else
    puts "NO"
end


a, b, f, k = gets.split.map(&:to_i)
u, v, t, r = (a-f)*2, f*2, b, 0
Enumerator.new do |y|
  y.yield f
  ((k-1)/2).times {
    y.yield u
    y.yield v
  }
  if k&1 == 0
    y.yield u
    y.yield f
  else
    y.yield a-f
  end
end.each do |s|
  if t < s
    t = b
    r += 1
    if t < s
      r = -1
      break
    end
  end
  t -= s
end
p r


a, b, f, k = gets.split.map(&:to_i)
step = Enumerator.new do |y|
  y.yield f
  u, v = (a-f)*2, f*2
  ((k-1)/2).times {
    y.yield u
    y.yield v
  }
  if k&1 == 0
    y.yield u
    y.yield f
  else
    y.yield a-f
  end
end
t, r = b, 0
step.each do |s|
  if t < s
    t = b
    r += 1
    if t < s
      r = -1
      break
    end
  end
  t -= s
end
p r


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

d = [f]
(1...k).each do |i|
	d << 2 * (a - f) if i.odd?
	d << 2 * f       if i.even?
end
d << (a - f) if k.odd?
d << f       if k.even?
d << -1

curB = b
ans = 0
d.each_with_index do |l, j|
	break if l == -1

	curB -= l

	if curB < 0
		p -1
		exit(0)
	elsif curB < d[j + 1]
		curB = b;
		ans += 1;
	end

end

p ans


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

x,y,z = lscan

pl = x + z > y
mi = y + z > x
ze = (x - y).abs <= z

if x == y && z == 0
  puts "0"
elsif pl && !mi && !ze
  puts "+"
elsif mi && !pl && !ze
  puts "-"
else
  puts "?"
end


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

if x > y
  if (x-y).abs > z
    puts '+'
  else
    puts '?'
  end
elsif x < y
  if (x-y).abs > z
    puts '-'
  else
    puts '?'
  end
else
  if z == 0
    puts 0
  else
    puts '?'
  end
end


a,b,c = gets.split.map(&:to_i)
if (a-b+c)*(a-b-c) <= 0 && c != 0
    puts "?"
elsif a-b > 0
    puts "+"
elsif a-b < 0
    puts "-"
else
    puts "0"
end


SORTED = ['B', 'G', 'R']

def solve(n, cards)
  return cards[0] if cards.uniq.size == 1
  return cards.uniq.sort.join if cards.uniq.size === 3

  g = cards.group_by { |x| x }
  x, y = g.map { |k, v| v.size }
  if x == 1 && y == 1
    (SORTED - cards).first
  elsif x > 1 && y > 1
    SORTED.join
  else
    (SORTED - g.select { |k, v| v.size > 1 }.keys).join
  end
end

puts solve(gets.strip.to_i, gets.strip.split(''))


n = gets.chomp.to_i
s = gets.chomp
r = 0
g = 0
b= 0
for i in 0..n-1
	r += 1 if s[i] == 'R'
	g += 1 if s[i] == 'G'
	b += 1 if s[i] == 'B'
end
if r >= 1 && g >= 1 && b >= 1
	puts "BGR"
	exit
end
if r > 1 && g >1 || g > 1 && b > 1 || r > 1 && b > 1
	puts "BGR"
	exit
end

if r == 0 && g == 0 || g == 0 && b == 0 || r==0 && b == 0
	puts "R" if r > 0
	puts "G" if g > 0
	puts "B" if b > 0
	exit
end

if r == 1 && g == 1 || g == 1 && b == 1 || r== 1 && b == 1
	puts "B" if b == 0
	puts "G" if g == 0
	puts "R" if r == 0
	exit
end

puts "BG" if r > 1
puts "GR" if b > 1
puts "BR" if g > 1


clock_wise = %w{v < ^ >}
counter_clock_wise = %w{v > ^ <}

start, finish = gets.strip.split
n = gets.strip.to_i

start_pos_cw = clock_wise.index start
start_pos_ccw = counter_clock_wise.index start
n %= 4

cw = clock_wise[start_pos_cw + (n - 4)] == finish
ccw = counter_clock_wise[start_pos_ccw + (n - 4)] == finish

if cw && !ccw
    puts "cw"
elsif ccw && !cw
    puts "ccw"
else
    puts "undefined"
end


a,b=gets.split
n=gets.to_i%4
if a==?^
    if b==?>
        puts (n==1 ? "cw" : n==3 ? "ccw" : "undefined")
    elsif b==?<
        puts (n==1 ? "ccw" : n==3 ? "cw" : "undefined")
    else
        puts "undefined"
    end
elsif a==?v
    if b==?>
        puts (n==1 ? "ccw" : n==3 ? "cw" : "undefined")
    elsif b==?<
        puts (n==1 ? "cw" : n==3 ? "ccw" : "undefined")
    else
        puts "undefined"
    end
elsif a==?>
    if b==?v
        puts (n==1 ? "cw" : n==3 ? "ccw" : "undefined")
    elsif b==?^
        puts (n==1 ? "ccw" : n==3 ? "cw" : "undefined")
    else
        puts "undefined"
    end
else
    if b==?v
        puts (n==1 ? "ccw" : n==3 ? "cw" : "undefined")
    elsif b==?^
        puts (n==1 ? "cw" : n==3 ? "ccw" : "undefined")
    else
        puts "undefined"
    end
end


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

puts foo


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

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

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


point=gets.split.map(&:to_i)
x=point[0]
y=point[1]
if x.abs==0 || y.abs==0 then
    puts "black"
    exit
end
d=x*x+y*y
1.upto(999999){|i|
    if (i%2==1 && x*y>0) || (i%2==0 && x*y<0) then
        #black
        if d<=i*i then
            puts "black"
            exit
        end
    else
        #white
        if d<i*i then
            puts "white"
            exit
        end
    end
}


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

puts (gs.split(/[AEIOUY]/).map(&:size)+[0]).max + 1


a=gets.chomp.split('')

max=0

l=0

a.each do |c|

    if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U'||c=='Y')

        if (l>max)

            max=l

        end

        l=0

    else

        l+=1

    end

end

if (l>max)

   max=l

end

max=max+1

puts max


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


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


n,a,b,x,y=gets.split.map(&:to_i)
c=n/a.lcm(b)
a=n/a
b=n/b
p a*x+b*y-c*[x,y].min


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


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


n = gets.to_i
t = gets.strip
i = 0
d = 1
s = ''
while i < t.size
    s += t[i]
    i += d
    d += 1
end
puts s


n=gets.to_i
s=gets.split('')
idx=0
ans=""
ctr=1
while idx<n do
  ans+=s[idx]
  idx+=ctr
  ctr+=1
end
puts ans


# Input
n = gets.to_i
t = gets.chomp

# Processing
m = (((8*t.length + 1)**0.5 - 1) / 2).round
str = ""
m.times do |i|
  str << t[(i + 2) * (i + 1) / 2 - 1]
end

# Output
puts str


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)/k
        ans = l
    end
end
puts ans


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

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


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

  def solve(s2, pos, i, x)
    return @memo[i][pos+10] if @memo[i][pos+10]

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

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

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

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

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


s1 = gets.chomp.chars
s2 = gets.chomp.chars

def get_position(chars)
  chars.map do |c|
    case c
      when '+' then 1
      when '-' then -1
    end
  end.inject(:+)
end

def get_combinations(chars)
  if chars.include? '?'
    pos_chars = chars.dup
    neg_chars = chars.dup
    index = chars.index('?')
    pos_chars[index] = '+'
    neg_chars[index] = '-'
    get_combinations(pos_chars) + get_combinations(neg_chars)
  else
    [chars]
  end
end

dest = get_position(s1)
opts = get_combinations(s2).map{|cs| get_position(cs) }

conforms = opts.count{|c| c == dest }

res = conforms.to_f / opts.count

puts "%.12f" % res


def f(n)
	if n == 0
		return 1
	else
		return f(n-1)*n
	end
end
s1 = gets.chomp
s2 = gets.chomp
x1 = x2 = n = 0
p = 0.5
for i in 0...s1.length
	if s1[i] == '+'
		x1 += 1
	else
		x1 -= 1
	end
end
for i in 0...s2.length
	if s2[i] == '+'
		x2 += 1
	elsif s2[i] == '-'
		x2 -= 1
	else
		n += 1
	end
end
k = (x1-x2).abs
if n < k or (k+n)%2==1
	puts 0.0
else
	l =(k+n)/2
	puts f(n)/(f(l)*f(n-l))*p**n
end


def pos s
  p=q=0
  s.strip.each_char do |c|
    case c
    when '+'
      p+=1
    when '-'
      p-=1
    else
      q+=1
    end
  end
  [p,q]
end

def comb n, k
  if k==0
    1
  else
    comb(n,k-1)*(n-k+1)/k
  end
end

def chance os, q
  if os.abs>q || os%2!=q%2
    0
  else
    1.0*comb(q,(q-os.abs)/2)/(2**q)
  end
end

tgt,_ = pos readline
src,q = pos readline

os = tgt-src
puts chance(os,q)


intended = STDIN.readline.strip
received = STDIN.readline.strip
target = intended.count('+') - intended.count('-')
origin = received.count('+') - received.count('-')
steps = received.count('?')
location = Hash.new(0.0)
location[origin] = 1.0
steps.times do
	next_location = Hash.new(0.0)
	location.each do |x, p|
		next_location[x-1] += p/2
		next_location[x+1] += p/2
	end
	location = next_location
end
puts location[target]


comment = []
comment = gets.chomp
receive = []
receive = gets.chomp

n = comment.length
comment_plus = 0
comment_minus = 0
for i in 0...n
    if comment[i] == "+"
        comment_plus += 1
    else
        comment_minus += 1
    end
end
receive_plus = 0
receive_minus = 0
receive_ques = 0
for i in 0...n
    if receive[i] == "+"
        receive_plus += 1
    elsif receive[i] == "-"
        receive_minus += 1
    else
        receive_ques += 1
    end
end

offset = ((comment_plus - receive_plus) - (comment_minus- receive_minus)).abs
a = (offset + receive_ques) / 2
b = (receive_ques - offset) / 2
probability = 1
receive_ques.times do
    probability = probability * 2
end
ques_factorial = (1..receive_ques).inject(:*) || 1
a_factorial = (1..a).inject(:*) || 1
c = (1..b).inject(:*) || 1
time = ques_factorial / a_factorial / c
percentage = time.to_f / probability
if comment_minus == receive_minus && comment_plus == receive_plus
    puts 1
elsif comment_plus < receive_plus || comment_minus < receive_minus
    puts 0
else
    puts percentage
end


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


def factorial(n)
  if n == 0 then
    return 1
  else
    return n * factorial(n - 1)
  end
end

input = gets.strip
output = gets.strip
input_pluses = input.count("+") || 0
input_minuses = input.count("-") || 0
output_pluses = output.count("+") || 0
output_minuses = output.count("-") || 0
output_questions = output.count("?") || 0
if output_pluses > input_pluses || output_minuses > input_minuses then
  probability = 0
else
  diff_pluses = input_pluses - output_pluses
  diff_minuses = input_minuses - output_minuses
  combinations = factorial(output_questions)/(factorial(diff_pluses) * factorial(output_questions - diff_pluses))
  # puts combinations
  # puts 2**output_questions
  probability = combinations/2**output_questions.to_f

end
puts probability


# coding: utf-8

# if __FILE__ == $0
# end

s1 = gets.chomp
s2 = gets.chomp
s1_lastpos = s1.count('+') - s1.count('-')
s2_detlastpos = s2.count('+') - s2.count('-')
s2_undetsigns = s2.count('?')
prob = 0.5

if s2_undetsigns == 0 && s1_lastpos == s2_detlastpos
    puts "1.0"
else
    total_cases = (2**s2_undetsigns)*1.0
    possible_cases = 0
    for mask in 0...(2**s2_undetsigns)
        s2_additional = 0
        for i in 0...s2_undetsigns
            if mask[i] == 0
                s2_additional -= 1
            else
                s2_additional += 1
            end
        end

        if (s2_detlastpos + s2_additional) == s1_lastpos
            possible_cases += 1
        end
    end

    puts possible_cases/total_cases
end


h={}
h[0]=1.0
s=gets.chomp
p1=0
s.chars{|e|
	if e=="+" then
		p1+=1
	else
		p1-=1
	end
}
s=gets.chomp
s.chars{|e|
	h1=Hash.new(0.0)
	case e
	when "+"
		h.each{|k,v|
			h1[k+1]=v
		}
	when "-"
		h.each{|k,v|
			h1[k-1]=v
		}
	when "?"
		h.each{|k,v|
				h1[k-1]+=v*0.5
				h1[k+1]+=v*0.5
		}
	end
	h=h1

}
if h.key?(p1) then
	puts h[p1]
else
	puts 0
end


s1 = gets.chomp
s2 = gets.chomp

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

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


#!/usr/bin/ruby
a=Array(1024)
1024.times{|i| a[i]=i.to_s(2).to_i}
n=gets.chomp.to_i
1024.times{|i| if a[i+1]>n then puts i;exit end}


n = gets.to_i
m = 0
loop do
	break if ('%b' % m).to_i > n
	m += 1
end
p m - 1


s = gets.strip

one = false

0.upto(s.length-1) do |i|
  if (s[i] > ?1) or (one)
    s[i] = ?1
    one = true
  end
end

n = s.to_i(2)
puts n


a = gets.chars.map(&:to_i)
lo, hi = a.slice(0, 3), a.slice(3, 3)
diff = hi.reduce(&:+) - lo.reduce(&:+)
lo, hi, diff = hi, lo, -diff if diff < 0
(lo.map {|x| 9 - x} + hi).sort!.reverse!.each_with_index do |x, i|
  if diff < 1
    p i
    break
  end
  diff -= x
end


a = gets.chars.map(&:to_i)
l, h = a.slice(0, 3), a.slice(3, 3)
d = h.reduce(&:+) - l.reduce(&:+)
l, h, d = h, l, -d if d < 0
a, i = (l.map {|x| 9 - x} + h).sort!, 0
while d > 0
  d -= a.pop
  i += 1
end
p i


d = [1,2,3,4]
a_p_ = Array.new(5, false)
a_from = Array.new(5, false)
a_to = Array.new(5, false)

4.times do |i|
  l, s, r, p_ = gets.chomp.split(" ").map(&:to_i)
  if l == 1
    a_from[i+1] = true
    l_to = d[i - 1]
    a_to[l_to] = true
  end
  if s == 1
    a_from[i+1] = true
    s_to = d[i - 2]
    a_to[s_to] = true
  end
  if r == 1
    a_from[i+1] = true
    r_to = d[i - 3]
    a_to[r_to] = true
  end
  if p_ == 1
    a_p_[i+1] = true
  end
end
5.times do |i|
  if a_p_[i]
    if a_from[i] || a_to[i]
      puts "YES"
      exit
    end
  end
end
puts "NO"


# Try Codeforces
# author: Leonardone @ NEETSDKASU
def xs()
    gets.strip.split.map &:to_i
end

l1, s1, r1, p1 = xs
l2, s2, r2, p2 = xs
l3, s3, r3, p3 = xs
l4, s4, r4, p4 = xs

f = p1 == 1 && l1+s1+r1+l2+s3+r4>0
f |= p2 == 1 && l2+s2+r2+r1+l3+s4>0
f |= p3 == 1 && l3+s3+r3+s1+r2+l4>0
f |= p4 == 1 && l4+s4+r4+l1+s2+r3>0

puts f ? :YES : :NO


n,l=gets.split.map &:to_i
a=gets.split.map.with_index{|e,i|[e.to_i,2**i]}.sort_by{|c,v|c.to_r/v}
m={}
f=->x,i{
    return 0 if !a[i]
    c,v=a[i]
    k=x/v
    m[[x,i]]||=[f[x-k*v, i+1]+k*c, (k+1)*c].min
}
p f[l, 0]


#!/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


cards = gets.chomp.split(' ').map(&:to_i)
numHash = {}

sum = 0

cards.each do |card|
	if numHash.has_key? card then numHash[card] += 1
	else numHash[card] = 1
	end

	sum += card
end

ans = sum

numHash.each do |k, v|
	if v > 1
		v = 3 if v > 3
		res = sum - k * v
		ans = res if ans > res
	end
end

puts ans


cards = gets.strip.split.map(&:to_i)
sum = cards.inject(:+)
count = Array.new(101, 0)
cards.each do |i|
    count[i] += 1
end
sub = 0
count.each_index do |i|
    if count[i] > 0
        sub = [sub, [count[i], 3].min * i].max if count[i] >= 2
    end
end
puts "#{sum - sub}"


n, k = gets.split.map &:to_i
a = gets.split.map &:to_i
p a.inject(k){|s, x| [s, k%x == 0 ? k/x : k].min}


n,m,*a=$<.read.split.map &:to_i
p m/a.select{|e|m%e<1}.max


n,k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
a.sort!
ans = 0
while (t = a.pop) && ans == 0
    ans = k/t if k % t == 0
end
puts ans


n = gets.to_i
cs = n.times.map{gets.split}

judge = lambda{|x|
  cs.permutation.each{|cc|
    s = x.to_s.chars
    cube = 0
    flag = true

    until s.empty?
      c = s.first
      if cube>=n
        flag = false
        break
      end
      if cc[cube].include?(c)
        s.shift
      end
      cube += 1
    end

    if flag
      return true
    end
  }
  return false
}

result = 0

(1..999).each{|x|
  if judge.call(x)
    result = x
  else
    break
  end
}
puts result


def solve(n, cubes)
  (1..1000).to_a.each do |x|
    digits = x.to_s.split('').map(&:to_i)
    if digits.size > cubes.size
      return x - 1
    end

    if digits.size == 1
      res = cubes.map { |c| c.include?(digits.first) }.reject { |z| !z }
      if res.empty?
        return x - 1
      end
    elsif digits.size == 2
      found = false
      for i in 0...(cubes.size)
        break if found
        for j in 0...(cubes.size)
          next if i == j
          if cubes[i].include?(digits.first) && cubes[j].include?(digits.last)
            found = true
            break
          end
        end
      end
      unless found
        return x - 1
      end
    elsif digits.size == 3
      found = false
      for i in 0...(cubes.size)
        break if found
        for j in 0...(cubes.size)
          break if found
          for k in 0...(cubes.size)
            next if [i,j,k].uniq.size != 3
            if cubes[i].include?(digits[0]) && cubes[j].include?(digits[1]) && cubes[k].include?(digits[2])
              found = true
              break
            end
          end
        end
      end
      unless found
        return x - 1
      end
    end
  end
end

n = gets.strip.to_i
cubes = []
n.times do
  cubes << gets.strip.split.map(&:to_i).sort
end

puts solve(n, cubes)


def solve(n, cubes)
  (1..1000).to_a.each do |x|
    digits = x.to_s.split('').map(&:to_i)

    permutations = (0...n).to_a.permutation.to_a
    res = permutations.map do |p|
      p.zip(digits).reject { |pd| pd.first.nil? || pd.last.nil? }.map do |pd|
        cubes[pd.first].include?(pd.last)
      end.select { |x| x }.size == digits.size
    end
    return x - 1 if res.select { |x| x }.empty?
  end
end

cubes = (1..gets.strip.to_i).map do
  gets.strip.split.map(&:to_i).sort
end

puts solve(cubes.size, cubes)


def test
	input = $stdin.read
	input = input.split("\n")
	chars = ['a','b','c','d','e','f','g','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


x,y,z,t1,t2,t3 = gets.split.map { |e| Integer(e)  }
stair  = t1 * (x - y).abs
elevator =  t2 * (z - x).abs +  t3*2 + t2*(x-y).abs + t3
puts (if elevator <= stair then "YES" else "NO" end)


class A
  def self.run(s)
    x, y, z, t1, t2, t3 = s.split.map(&:to_i)
    walk = (x-y).abs * t1
    lift = (x-z).abs * t2 + t3*2 + (x-y).abs * t2 + t3
    if lift <= walk
      true
    else
      false
    end
  end
end

if A.run(gets.chomp)
  puts 'YES'
else
  puts 'NO'
end


def solution
  x, y, z, stairs, elv, elv_doors = read_ints

  elv_time = ((z-x).abs + (x-y).abs) * elv + elv_doors * 3
  stair_time = (y-x).abs * stairs

  puts elv_time <= stair_time ? "YES" : "NO"

end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']


# Codeforces Beta Round #80
# Problem A -- Blackjack
cards = [0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 15, 4]
n = gets.to_i - 10
puts (0 <= n and n <= 11) ? cards[n] : 0


#104A
n=gets.to_i
if n <= 10
    puts "0"
elsif n >= 11 and n <= 19
    puts "4"
elsif n == 20
    puts "15"
elsif n == 21
    puts"4"
else
    puts "0"
end


def blackjack
  c = { }
  1.upto(11) { |i| c[i] = 4 }
  c[10] = 15

  goal = gets.to_i
  if goal-10 < 1
    puts 0
  else
    puts (c.has_key?(goal-10)) ? c[goal-10] : 0
  end
end

blackjack


n = gets.to_i

ans = 0

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

p [ans, abc.count(1)].max


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


row = gets.chomp

def possible(row)
  return 'No' if row.size < 3

  1.upto(row.size - 2) do |i|
    next if row[i] == '.' || row[i - 1] == '.' || row[i + 1] == '.'

    if [row[i - 1], row[i], row[i + 1]].uniq.count == 3
      return 'Yes'
    end
  end

  'No'
end

puts possible(row)


s = gets().chop()
if s.include?("ABC") || s.include?("ACB") || s.include?("BAC") ||
	s.include?("BCA") || s.include?("CAB") || s.include?("CBA")
	puts "Yes"
else
	puts "No"
end


L, R, X, Y = gets.split.map(&:to_i)
if Y % X != 0
    puts 0
    exit
end

div = Y/X
sq = Math.sqrt(div).ceil
ans = 0
(1..sq).each do |i|
    next if div%i != 0
    j = div/i
    next if i > j
    next if i.gcd(j) != 1
    if (i*X).between?(L, R) && (j*X).between?(L, R)
        ans += i==j ? 1 : 2
    end
end
puts ans


require 'prime'

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

x_factors = Prime.prime_division(x)
y_factors = Prime.prime_division(y)
all_factors = {}
x_factors.each do |fp|
  all_factors[fp[0]] = [fp[1]]
end
y_factors.each do |fp|
  if all_factors[fp[0]]
    all_factors[fp[0]] << fp[1]
  else
    all_factors[fp[0]] = [0, fp[1]]
  end
end

if all_factors.values.map(&:size).uniq != [2]
  puts 0
  exit
end

all_factors_ary = all_factors.keys.sort
bl = all_factors_ary.size
n = 1<<bl
# puts all_factors_ary.inspect
# puts all_factors.inspect

pairs = []
0.upto(n).each do |bm|
  a = 1
  b = 1
  0.upto(bl-1).each do |s|
    f = all_factors_ary[s]
    # puts f
    if (bm & (1<<s)) > 0
      b *= (f ** all_factors[f][1])
      a *= (f ** all_factors[f][0])
    else
      a *= (f ** all_factors[f][1])
      b *= (f ** all_factors[f][0])
    end
  end
  # puts "(#{a},#{b})"
  if l<=a && a<=r && l<=b && b<=r
    pairs << [a,b]
  end
end
puts pairs.uniq.size


n, cw, cb = gets.split(' ').map(&:to_i)
arr = gets.split(' ').map(&:to_i)
answer = 0

1.upto(n) do |index|
  q = arr[index - 1]
  w = arr[n -index]

  if (q == 0 && w == 1) || (q == 1 && w == 0)
    answer = -1
    break
  end

  if q == 2 && w == 2
  	answer += [cw, cb].min
  elsif q != w && q == 2
	answer += (w == 0 ? cw : cb)
  end
end

 p answer


#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

a = inp[0]
b = inp[0]
c = inp[0]
d = inp[0]
e = inp[0]
maxy = a*(c-1)+b*(d-1)
min = [e-maxy,0].max
if(c > d)
  max = [e/d,b].min
  e -= max * d
  max += e/c
else
  max = [e/c,a].min
  e -= max * c
  max += e/d
end
puts "#{min} #{max}"


a1 = gets.to_i
a2 = gets.to_i
k1 = gets.to_i
k2 = gets.to_i
n = gets.to_i
mn = [0,n-(k1-1)*a1-(k2-1)*a2].max
if k1 > k2
    k1,k2 = k2,k1
    a1,a2 = a2,a1
end
mx = [a1,n/k1].min
n -= mx*k1
mx += n/k2
puts "#{mn} #{mx}"


a1 = gets.to_i
a2 = gets.to_i
k1 = gets.to_i
k2 = gets.to_i
n = gets.to_i

n1 = n2 = n

maxi = 0

if (k1 < k2)
    out = [a1, n1 / k1].min
    n1 -= out * k1
    maxi += out

    out = [a2, n1 / k2].min
    maxi += out
else
    out = [a2, n1 / k2].min
    n1 -= out * k2
    maxi += out

    out = [a1, n1 / k1].min
    maxi += out
end

mini = 0

avail = a1 * (k1 - 1) + a2 * (k2 - 1)
n2 -= avail
if (n2 > 0)
    mini += n2
end

print("#{mini} #{maxi}")


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


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

hash = Hash.new(0)
as.each do |a|
    hash[a] += 1
end

puts (1..101).bsearch{|days|
    people = 0
    hash.values.each do |num|
        people += num/days
    end
    people < N
} - 1


n, m = gets.strip.split.map(&:to_i)
ary = gets.strip.split.map(&:to_i)
hsh = {}
ary.each {|ai| hsh[ai] = ((hsh[ai] || 0)+1) }

def can?(hsh, n, days)
  support_n = 0
  hsh.keys.each do |ai|
    support_n += (hsh[ai]/days)
  end
  support_n >= n
end

ans=0
105.downto(1).each do |d|
  if can?(hsh, n, d)
    ans=d
    break
  end
end
puts ans


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

food.each do |pa|
  h[pa]+=1
end

if k<n
  puts 0
  exit
end

day = 1

while true
  peoplecount = 0
  h.each do |foodtype,foodno|
    peoplecount += foodno/day
    if peoplecount >= n
      day+=1
      break
    end
  end

  if peoplecount < n
    puts day-1
    exit
  end
end


n = gets.strip.to_i
s = gets.strip

def move(s)
	t1 =	(s[0].ord - "A".ord).abs
	t1 = 26-t1 if t1 > 12
	t2 =	(s[1].ord - "C".ord).abs
	t2 = 26-t2 if t2 > 12
	t3 =	(s[2].ord - "T".ord).abs
	t3 = 26-t3 if t3 > 12
	t4 =	(s[3].ord - "G".ord).abs
	t4 = 26-t4 if t4 > 12
   t1+t2+t3+t4
end
step_move = move(s[0..3])
(4..n-1).each do |i|
  tmp = move(s[i-3..i])
  step_move = tmp if step_move > tmp
end

p step_move


n = gets.strip.to_i
s = gets.strip

def move(s)
  g = ["A", "C", "T", "G"]
  steps = 0
  4.times do |i|
    distance = (s[i].ord - g[i].ord).abs
    steps += distance > 12 ? 26 - distance : distance
  end
  steps
end

step_move = move(s[0..3])
(4..n-1).each do |i|
  tmp = move(s[i-3..i])
  step_move = tmp if step_move > tmp
end

p step_move


n = gets.to_i
s = gets.chomp
arr = []
res = 9999999

s.each_char do |i|
  al = [9999999]*26

  j = (i.ord-1).chr
  if j < 'A' then j = 'Z' end
  cnt = 1
  al[j.ord-65] = cnt

  while j != i
    j = (j.ord-1).chr
    cnt += 1

    if j < 'A'
      j = 'Z'
    end

    al[j.ord-65] = [al[j.ord-65], cnt].min
  end

  j = (i.ord+1).chr
  if j > 'Z' then j = 'A' end
  cnt = 1
  al[j.ord-65] = cnt

  while j != i
    j = (j.ord+1).chr
    cnt += 1

    if j > 'Z'
      j = 'A'
    end

    al[j.ord-65] = [al[j.ord-65], cnt].min
  end

  al[i.ord-65] = 0

  arr.push(al)
end

(n-3).times do |i|
  res = [res, arr[i][0]+arr[i+1][2]+arr[i+2][19]+arr[i+3][6]].min
end

p res


groups = gets.to_i
years = gets.split.map(&:to_i)
years.sort!
year = years[groups/2]
puts year


group_count = gets.to_i
groups = gets.split.sort
puts groups[group_count/2]


N, K, M, D = gets.split.map(&:to_i)
ans = 0
(1..D).each do |m|
    x = [M, N/((m-1)*K+1)].min
    ans = [ans, x*m].max
end
puts ans


def solve(n, k, m, d)
  ans = -1
  (1..d).each do |a|
    y = k*a - k + 1
    if y <= 2*n && n >= y
      z = [n/y, m].min
      ans = [ans, z*a].max
    end
  end
  p ans
end

loop do
  line = gets
  if line
    n, k, m, d = line.split.map(&:to_i)
    solve(n, k, m, d)
  else
    break
  end
end


require 'set'

$set=Set.new
$n=gets.to_i

def dfs num,x,y
  if (1..$n)===num
    $set<<num
    dfs(num*10+x,x,y)
    dfs(num*10+y,x,y)
  end
end

count=0

(0..9).each {|x|
  (0..9).each {|y|
    dfs(x,x,y)
  }
}

puts $set.size


require 'set'

$set=Set.new
$n=gets.to_i

def dfs num,x,y
  if (1..$n)===num
    $set<<num
    dfs(num*10+x,x,y)
    dfs(num*10+y,x,y)
  end
end

count=0

(0..9).each {|x|
  (0..9).each {|y|
    dfs(x,x,y)
    dfs(y,x,y)
  }
}

puts $set.size


n,a,b=gets.split.map &:to_i
a-=1
b-=1
c=1
while a/2!=b/2
	a/=2
	b/=2
	c+=1
end
puts 1<<c==n ? 'Final!' : c


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


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

tr = 0
while n != 0
	n = n/2
	tr +=1
end
tr -=1
out = 0
while true
	r = 0
	r = 1 if a % 2 != 0
	a = (a/2) + r
	r = 0
	r = 1 if b % 2 != 0
	b = (b/2) + r
	out += 1
	if a == b
		break
	end
end
out = "Final!" if out == tr
puts out


n, k=gets.split.map &:to_i
s=gets.split.map(&:to_i).inject(0, :+)
r=0
while 2*s<2*k*n-n
	r+=1
	s+=k;
	n+=1
end
puts r


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

n, k = gis
a = gis

sum = a.inject(:+) * 10

lim = (k - 1) * 10 + 5

i = n
loop do
    break if sum / i >= lim
    sum += k * 10
    i += 1
end

puts (i - n)


X, Y, L, R = gets.split.map( &:to_i )

A = Array.new( 64 ) { |i| X ** i }
B = Array.new( 64 ) { |i| Y ** i }

u = Array.new
u << L - 1
u << R + 1
A.each do |i|
	B.each do |j|
		if L <= i + j && i + j <= R then
			u << i + j
		end
	end
end
u.sort!

res = 0
( u.length - 1 ).times do |i|
	res = [ res, u[ i + 1 ] - u[i] - 1 ].max
end

p res


x, y, l, r = gets.chomp.split.map &:to_i

unlucky = []
for i in 0...64
    for j in 0...64
        m = x ** i + y ** j
        if m <= r && m >= l
            unlucky.push(m)
        end
    end
end

unlucky = unlucky.sort!
unlucky_neat = []
if !unlucky.empty?
    unlucky_neat.push(unlucky[0])
    for i in 1...unlucky.length
        if unlucky[i] != unlucky[i - 1]
            unlucky_neat.push(unlucky[i])
        end
    end
end

if !unlucky_neat.empty?
    max = (unlucky_neat[0] - l) > (r - unlucky_neat[unlucky_neat.length - 1]) ? (unlucky_neat[0] - l) : (r - unlucky_neat[unlucky_neat.length - 1])
    for i in 0...unlucky_neat.length - 1
        if unlucky_neat[i + 1] - unlucky_neat[i] - 1 > max
            max = unlucky_neat[i + 1] - unlucky_neat[i] - 1
        end
    end
    puts max
else
    max = r - l + 1
    puts max
end


x,y,l,r=gets.split(" ").map(&:to_i)
xp=[]
yp=[]
t=1
while t<r do
  xp.push(t)
  t*=x
end
t=1
while t<r do
  yp.push(t)
  t*=y
end

zp=[]

for i in xp do
  for j in yp do
    zp.push(i+j)
  end
end

zp.uniq!()
zp.sort!()

#p zp

ans=0
tl=l
for i in zp do
  next if i<tl
  if i>=r then
    #p [ans,tl,i]
    r-=1 if r==i

    ans=[ans,r+1-tl].max
    tl=r+1
    break
  end
  if tl<=i then
    ans=[ans,i-tl].max
    tl=i+1
  end
  #p [ans,i]
end
#p [ans,tl]
ans=[ans,r+1-tl].max

puts ans


y, b, r = gets.split.map &:to_i
ans = 6
(1..y).each do |i|
    if i + 1 <= b && i + 2 <= r
        ans = [ans, i + i + 1 + i + 2].max
    end
end
p ans


(a,b,c) = gets.split.map(&:to_i)
(x,y,z) = 1,2,3
while x < a and y < b and z < c
  x+=1 ; y+=1 ; z+=1
end
puts x+y+z


str = 2.times.map{'X' + gets.chomp + 'X'}

ans = 0
str[0].length.times do |i|
    next unless str[0][i] == '0' && str[1][i] == '0'
    [[0, i-1], [1, i-1], [0, i+1], [1, i+1]].each do |i2, j2|
        if str[i2][j2] == '0'
            str[0][i] = 'X'
            str[1][i] = 'X'
            str[i2][j2] = 'X'
            ans += 1
            break
        end
    end
end
puts ans


gets.chomp.to_i
pockets = gets.chomp.split.map(&:to_i)

repeats = {}
pockets.each do |element|
  if repeats[element]
    repeats[element] += 1
  else
    repeats[element] = 1
  end
end

puts repeats.values.max


n = STDIN.readline.strip
numbers = STDIN.readline.strip.split(/\s+/)

puts numbers.group_by {|v| v.to_i }.map {|k,v| v.length }.max


n=gets.to_i
k,s=0,0
for i in 1...n
	s=1
	for j in 1...n
		s*=i
		s%=n
		if (s-1)%n==0
			if j==n-1
				k+=1
			else
				break
			end
		end
	end
end
puts k


def is_root x,p
	n = 1;
	q = x;

	while q%p!=1 and n<p
		q = (x*q)%p
		n += 1
	end

	if n==p-1
	 return 1
	else
	 return 0
	end

end

n = gets.to_i
s = 0
(1...n).each { |x|
	s += is_root(x,n)
}

puts s


n, m, k = gets.split.map(&:to_i)
m -= 1
a = gets.split.map(&:to_i)
mi = 2 ** 100
(0 ... n).each {|i|
  if a[i] <= k && a[i] != 0
    mi = [mi, (m - i).abs * 10].min
  end
}
puts mi


n,m,k,*a=$<.read.split.map(&:to_i)
b=a.map.with_index(1){|e,i|0<e&&e<=k ? i : nil}.compact
p 10*b.map{|i|(i-m).abs}.min


n,x,m=gets.split.map{|e| e.to_i}
y=gets.split.map{|e| e.to_i}
l1=-1
r1=-1
y.size.times{|i|
	if y[i]!=0 && y[i]<=m then
		p=i+1
		if p<x then
			l1=p
		else
			r1=p
			break
		end
	end
}
ans=n
if l1!=-1 then
	ans=(x-l1)
end
if r1!=-1 then
	ans=[ans,(r1-x)].min
end
puts ans*10


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

fx,fy,tx,ty = ascan
x,y = ascan

tx-=fx
ty-=fy

if (x==0&&tx!=0 || y==0&&ty!=0)
    puts "NO"
end

if tx%x==0 && ty%y==0 && (x==0||y==0||((tx/x).odd? ==(ty/y).odd?))
    puts "YES"
else
    puts "NO"
end


x1, y1, x2, y2 = gets.chomp.split.map &:to_i
x, y = gets.chomp.split.map &:to_i

m = (x1 - x2).abs
n = (y1 - y2).abs
h = m / x
k = n / y
if m % x == 0 && n % y == 0 && h % 2 == k % 2
    puts "YES"
else
    puts "NO"
end


N, M, R = gets.split.map(&:to_i)
S = gets.split.map(&:to_i)
B = gets.split.map(&:to_i)

s = S.min
cnt = R / s
b = B.max
diff = b - s

money = R + diff * cnt
puts [money, R].max


n,m,r = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).min
b = gets.split.map(&:to_i).max
p a < b ? (r/a)*b + r % a : r


n, m, r = gets.chomp.split.map(&:to_i)
buy = gets.chomp.split.map(&:to_i).min
sell = gets.chomp.split.map(&:to_i).max

if sell > buy
  shares = r / buy
  profit = shares * (sell - buy)
  r += profit
end

puts r


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


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

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

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


n,m = gets.split.map(&:to_i)
t = gets.split.map(&:to_i)
tmp = []
sum = 0
ans = []
t.each do |v|
  sum += v
  if sum <= m
    ans << 0
  else
    tmp.sort!.reverse!
    a = 0
    ts = sum
    tmp.each do |x|
      ts -= x
      a += 1
      if ts <= m
        ans << a
        break
      else a == tmp.length
        a << 0
      end
    end
  end
  tmp << v
end
puts ans.join(" ")


n, m = gets.split.map(&:to_i)
minv = [n - 2 * m, 0].max
ng = -1
ok = n
while ok - ng > 1
    mid = (ng + ok) / 2
    if mid * (mid - 1) >= 2 * m
        ok = mid
    else
        ng = mid
    end
end
maxv = n - ok
puts "#{minv} #{maxv}"


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

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


a = gets.split.collect(&:to_i)
#s = "1 3 2 1 2 3"
#a = s.split.collect(&:to_i)
ans = "NO"
sum = 0
a.each{|x| sum+=x}
a.each_index{|x|
	a.each_index{
	|y| a.each_index{|z|
		if x>y and y > z and a[x]+a[y]+a[z] == sum/2.0 then
			ans = "YES"
		end
			}}}
puts ans


a = gets.split.map(&:to_i)
(1 << 6).times do |i|
    if i.to_s(2).count(?1) == 3
        c = [0, 0]
        6.times do |j|
            if (i & (1 << j)) > 0
                c[1] += a[j]
            else
                c[0] += a[j]
            end
        end
        if c[0] == c[1]
            puts "YES"
            exit
        end
    end
end
puts "NO"


a=gets.split.map &:to_i
s=a.inject :+
puts (a.combination(3).any?{|j| j.inject(:+)*2==s} ? "YES" : "NO")


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


k = gets
str = "1"
num = 1
while(str.length < k.to_i)
    num+=1
    str += num.to_s
end
print(str[k.to_i - 1])


input=gets.split[0]
dx={"L"=>-1,"R"=>1,"U"=>0,"D"=>0}
dy={"L"=>0,"R"=>0,"U"=>-1,"D"=>1}
d=["L","R","D","U"]
x=y=0
access=Hash.new
input.length.times{|i|
    access[[x,y]]=1
    x+=dx[input[i]]
    y+=dy[input[i]]
}
access[[x,y]]=1
mincost=Hash.new
mincost.default=999999999
queue=Array.new
queue.push([0,0])
mincost[[0,0]]=0
while queue.size()>0
    now=queue.shift
    nowcost=mincost[now]
    for k in 0..3
        next_state=[now[0]+dx[d[k]],now[1]+dy[d[k]]]
        next unless access.key?(next_state)
        if mincost[next_state]>nowcost+1 then
            mincost[next_state]=nowcost+1
            queue.push(next_state)
        end
    end
end
if mincost[[x,y]]==input.length then
    puts "OK"
else
    puts "BUG"
end


robot_movements = gets.chomp
route = [[0,0]]
robot_movements.split(//).each do |c|
  now = route.last
  tugi = []
  if c=='U'[0]
    tugi = [now[0],now[1]-1]
  elsif c == 'D'[0]
    tugi = [now[0],now[1]+1]
  elsif c == 'R'[0]
    tugi = [now[0]+1,now[1]]
  else
    tugi = [now[0]-1,now[1]]
  end
  if route.include?(tugi)
    puts "BUG"
    exit 0
  elsif route.size>=2 &&
      ( route[0..-2].include?([tugi[0]-1,tugi[1]]) ||
        route[0..-2].include?([tugi[0]+1,tugi[1]]) ||
        route[0..-2].include?([tugi[0],tugi[1]-1]) ||
        route[0..-2].include?([tugi[0],tugi[1]+1]))
    puts "BUG"
    exit 0
  else
    route << tugi
  end
end
puts "OK"


s = gets.chomp!

mas = Hash.new()
for i in -101..101
  mas[i] = Hash.new()
  for j in -101..101
    mas[i][j] = 999999;
  end
end

step = 0
x,y = 0,0
mas[x][y] = 0
s.chars { |c|
  mas[x+1][y] = [mas[x+1][y],mas[x][y] + 1].min
  mas[x-1][y] = [mas[x-1][y],mas[x][y] + 1].min
  mas[x][y-1] = [mas[x][y-1],mas[x][y] + 1].min
  mas[x][y+1] = [mas[x][y+1],mas[x][y] + 1].min

  case c
  when "R"
    x += 1
  when "L"
    x -= 1
  when "U"
    y += 1
  when "D"
    y -= 1
  end

  if mas[x][y] < step + 1
    puts "BUG"
    exit
  end

  step += 1
}

puts "OK"


route = gets.chomp
visited = {[0, 0] => 1}
x, y = 0, 0
bug = false
value = 1

route.each_char do |direction|
	case direction
	when "R" then x += 1
	when "L" then x -= 1
	when "U" then y += 1
	when "D" then y -= 1
	end

	(bug = true and break) if visited[[x, y]] && visited[[x, y]] > 0

	value += 1
	(-1..1).each do |i|
		(-1..1).each do |j|
			next unless i ** 2 + j ** 2 == 1
			if visited[[x + i, y + j]] && visited[[x + i, y + j]] > 0
				bug = true if visited[[x + i, y + j]] != value - 1
			end
		end
	end
	visited[[x, y]] = value
end

if bug then
	puts "BUG"
else
	puts "OK"
end


s = gets.chomp.split(//)
c = [0,0]
h = Hash.new(false)
h[c.dup] = true
s.each do |x|
  p = c.dup
  c[0] += 1 if x == 'R'
  c[0] -= 1 if x == 'L'
  c[1] += 1 if x == 'U'
  c[1] -= 1 if x == 'D'
  near = [ [c[0]+1,c[1]],
           [c[0]-1,c[1]],
           [c[0],c[1]+1],
           [c[0],c[1]-1]
         ]
  near.delete p
  if h[c] or h[near[0]] or h[near[1]] or h[near[2]]
    puts "BUG"
    exit 0
  else
    h[c.dup] = true
  end
end
puts "OK"


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

newarr = []

prev = [-1]

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

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

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

#p newarr
puts countarr.max


#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

require 'prime'

n = inp[0]
t = inp
t = t.sort
h = {}
ans = 0
t.each do |d|
   (t).each do |i|
    if(d%i == 0)
      h[i] = true
      break
    end
  end
end

p ((h.to_a.size))


n = gets.to_i
a = gets.split.map(&:to_i).sort
painted = Array.new(n, false)
colors = 0
for i in 0...n do
  if !painted[i]
    colors += 1
    painted[i] = true
    for j in (i+1)...n do
      if !painted[j] && a[j] % a[i] == 0
        painted[j] = true
      end
    end
  end
end
puts colors


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

ans = 0
while !a.empty?
  ans += 1
  t = a.shift
  idx = []
  a.length.times do |i|
    if a[i] % t == 0
      idx << i
    end
  end
  idx.reverse.each do |v|
    a.delete_at(v)
  end
end
p ans


def a(c,r)
	return [Rational(1),fin(c,r)].min
end
def fin(c,r)
	return 0 if c==0 || c ==r+1
	return $t if c==1&&r==1
	return fout(c-1,r-1)/2 + fout(c,r-1)/2;
end
def fout(c,r)
	return [Rational(0),fin(c,r)-Rational(1)].max
end

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

count = 0
for row in 1..n
	for g in 1..row
		if a(g,row)==1
			count+=1
		end
	end
end
puts count


numbers = gets

res = numbers.split(' ')

n = res[0].to_i
t = res[1].to_i

data = grid = Array.new(n+1) { Array.new(n+1) }
total = 0;
k = 0
p = 0
while(k < n+1)
    p = 0
    while (p < n+1)
       data[p][k] = 0.0
       p +=1
    end
    k+=1
end
i = 0
while (i < t) do
    data[0][0]+=1;
    k,p = 0;
    while(k < n)
        p = 0
        while (p <= k)
            if data[k][p] > 1.0
                delta = data[k][p] - 1.0
                data[k][p] = 1.0;
                data[k+1][p]+= delta/2.0
                data[k+1][p+1]+=delta/2.0
            end
            p +=1
        end
        k+=1
    end
    i = i + 1
end
total  = 0
k,p = 0;
while(k < n)
    p = 0
    while (p <= k)
        #puts k.to_s + ':' + p.to_s + ":::"+data[k][p].to_s
        if data[k][p] >= 1
            total +=1;
        end
       p +=1
    end
    k+=1
end

puts total


n, t = gets.split.map(&:to_i)
$mas = []
$res = 0
for i in 1..n+1 do
    $mas[i] = []
    for j in 1..n+1 do
        $mas[i][j] = 0
    end
end

def children(lft, rgt)
    if( $mas[lft][rgt] >= 1)
        $res += 1
        $mas[lft + 1][rgt] += ($mas[lft][rgt] - 1) * 0.5
        $mas[lft + 1][rgt + 1] += ($mas[lft][rgt] - 1) * 0.5
    end
end

$mas[1][1] = t
for i in 1..n do
    for j in 1..i do
        children(i, j)
    end
end
puts $res


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

r = vals1[0]
c = vals1[1]
n = vals1[2]
k = vals1[3]

violas = []
columns = []
c.times {|x| columns << 0}
r.times {|x| col = []; c.times {|y| col << 0}; violas << col }
minx = r
miny = c
maxx = -1
maxy = -1
n.times {
  indices = gets.chomp.split(' ').map {|x| x.to_i - 1}
  violas[indices[0]][indices[1]] = 1
  minx = indices[1] if indices[1] < minx
  miny = indices[0] if indices[0] < miny
  maxx = indices[1] if indices[1] > maxx
  maxy = indices[0] if indices[0] > maxy
}

Rect = Struct.new(:x, :y, :x2, :y2)

rectangles = []
(0..r-1).each do |y|
  (0..c-1).each do |x|
    (y..r-1).each do |y2|
      (x..c-1).each do |x2|
        v = 0
        (y..y2).each do |yi|
          (x..x2).each do |xi|
            v += violas[yi][xi]
          end
        end
        if v >= k
          rectangles << Rect.new(x, y, x2, y2) unless rectangles.any? {|r|
            r.x == x && r.y == y && r.x2 == x2 && r.y2 == y2
          }
        end
      end
    end
  end
end
puts rectangles.length


buf = gets.split(' ')
r,c,n,k = buf[0].to_i, buf[1].to_i, buf[2].to_i, buf[3].to_i
a=Array.new(22)
for i in 0..r
	a[i]=Array.new(22, 0)
end
for i in 1..n
	buf = gets.split(' ')
	x, y = buf[0].to_i, buf[1].to_i
	a[x][y]=1
end
for i in 1..r
	for j in 1..c
		a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1]
	end
end
ans=0
for x1 in 1..r
	for y1 in 1..c
		for x2 in x1..r
			for y2 in y1..c
				tot = a[x2][y2]-a[x1-1][y2]-a[x2][y1-1]+a[x1-1][y1-1]
				ans+=1 if tot>=k
			end
		end
	end
end
puts ans


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

hs=Hash.new(0)
gets.chomp.chars.each{|e|
	hs[e]+=1
}

v=hs.values.sort.pop

if v<=f then
	puts "YES"
else
	puts "NO"
end


n,k=gets.split.map(&:to_i)
s = gets.chomp
h = Hash.new(0)
s.chars do |c|
    h[c] += 1
end

f = true
[*?a..?z].each do |c|
    if h[c] == 0
        next
    elsif (h[c] - 1) / k > 0
        f = false
        break
    end
end
puts f ? "YES" : "NO"


n,k=gets.split.map &:to_i
h=Hash.new 0
gets.chomp.chars{|c|h[c]+=1}
puts h.values.any?{|e|e>k} ? :NO : :YES


Capital = ['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']

def volume(s)
	t = 0
	for char in s.split('')
		t += 1 if Capital.include?char
	end
	return t
end

n = gets.to_i
s = gets.chomp.split
best = 0
for word in s
	best = [best,volume(word)].max
end

puts best


n = gets
words = gets.chomp.split(" ")

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


class C
  def initialize
    a, b = gets.chomp.split(' ').map(&:to_i)

    puts dfs(a, b)
  end

  def dfs(a, b)
    count = 0

    if a == 1
      count += b
    else
      sub = a / b
      if sub > 0
        count += sub
        count += dfs( a % b, b )
      else
        count += dfs( b, a )
      end
    end

    count
  end
end

c = C.new


#!/usr/bin/ruby
require 'rational'
a,b=gets.split.map(&:to_i)
r=0
while b>0
	g=a.gcd(b)
	a/=g;b/=g
	r+=a/b
	a,b=b,a-a/b*b
end
p r


a, b = gets.split.collect{|i| i.to_i}
ans = 0
while b != 1
    a, b = b, a if a < b
    ans += a/b
    a = a%b
end
puts ans+a


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

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


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

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


#!/usr/bin/ruby
include Math

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

if k == 1 then
	puts n
else
	puts (1 << n.to_s(2).length) - 1
end


x = gets.chomp.split(' ')
n = x[0].to_i
k = x[1].to_i
if k == 1
  puts n
else
  ans = 0
  i = 0
  while 2**i <= n
    ans += 2 ** i
    i += 1
  end
  puts ans
end


n,k=gets.split.map &:to_i
p k==1 ? n : 2**n.to_s(2).size-1


time = gets.chomp

h = time.split(':').first.to_i
m = time.split(':').last.to_i
count = 0

loop do
  k = "#{h.to_s.rjust(2, '0')}:#{m.to_s.rjust(2, '0')}"
  if k == k.reverse
    break
  end

  m = (m+1)%60
  if m == 0
    h = (h+1  )%24
  end
  count += 1
end

puts count


#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
=begin
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
=end


def is_palin? hh, mm
    hour = "%.2d" % hh
    mins = "%.2d" % mm
    mins.reverse == hour && hour.reverse == mins
end
hh, mm = gets.strip.split(":").map(&:to_i)
ans = 0
while !is_palin?( hh, mm )
    ans += 1
    if mm < 59
        mm += 1
    else
        mm = 0
        if hh < 23
            hh += 1
        else
            hh = 0
        end
    end
end
puts ans


h,m=gets.split(':').map(&:to_i)
t=Time.gm(2008,7,7,h,m)+gets.to_i*60
puts t.strftime('%H:%M')


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

t = Time.new(2002, 10, 31, h, m, 0)
t2 = t + a * 60

puts "#{t2.hour.to_s.rjust(2, '0')}:#{t2.min.to_s.rjust(2, '0')}"


#!/usr/bin/ruby
def encode(n) '%02d:%02d'%[n/60,n%60] end
def decode(s)
	h,m=s.split(':').map(&:to_i)
	h*60+m
end
puts encode((decode(gets)+gets.to_i)%1440)


MOD = 10 ** 9 + 9
def pow2(x)
    st = 1
    pow = 2
    while(x != 0)
        if((x & 1) != 0)
            st = (st * pow) % MOD
        end
        pow = (pow * pow) % MOD
        x >>= 1
    end
    return st
end
n, m, k = gets.chop.split.map(&:to_i)
extra = n - m
if(extra * k >= n)
    print m % MOD
else
    st = n - extra * k
    cnt = st / k
    rest = st % k
    total = pow2(cnt + 1)
    total = (total - 2 + MOD) % MOD
    total = (total * k + rest) % MOD
    total += extra * (k - 1)
    total %= MOD
    print total
end


require 'openssl'

n, m, k = gets.chomp.split(' ').map(&:to_i)
d = 1000000009
x = [0, m - (n - n % k) / k * (k-1) - n % k].max

r = 2.to_bn.mod_exp(x+1, d)
r = (r-2)*k % d
r = (r + (m-x*k)) % d

while r < 0
	r += d
end

puts r


require 'openssl'
MOD = 10 ** 9 + 9
n, m, k = gets.chop.split.map(&:to_i)
extra = n - m
if(extra * k >= n)
    print m % MOD
else
    st = n - extra * k
    cnt = st / k
    rest = st % k
    total = ((2.to_bn.mod_exp(cnt + 1,MOD) - 2 + MOD) % MOD * k + rest + extra *(k - 1)) % MOD
    print total
end


require 'openssl'
n,m,k=gets.split.map(&:to_i)
d=10**9+9
p=n/k
q=m-n+p
if q<=0
  puts m
  exit
end
r=2.to_bn.mod_exp(q+1,d).to_i
r=((d+r-2)%d*k+m-q*k)%d
p r


module  STL
  module Algorithm
    def power_mod(a, b, c) #compute (a ^ b) mod c
      ans, tich = 1, a
      while b > 0
        ans = (ans * tich) % c if b % 2 == 1
        tich = (tich * tich) % c
        b /= 2
      end
      ans
    end
  end
end

include STL::Algorithm
MOD = 1000_000_009
def solve(f)
  n, m, k = f.gets.split.map(&:to_i)
  t = (n / k) * (k - 1) + n % k
  if m <= t
    puts m
  else
    a = m - t

    tmp = power_mod(2, (a + 1), MOD)
    puts (k * (tmp - 2) + m - a * k) % MOD
  end
end

def debug
  (1..3).each do |e|
    f = File.new("test#{e}.txt", 'r')
    solve f
    f.close
  end
end

def release
  solve STDIN
end

release


x= Integer(gets.chomp)

if (x%2==0)
  puts ("white")
  puts ("1 2")
else
  puts'black'
end


n=gets.to_i
puts n%2>0?"black":["white","1 2"]


n = gets.chomp.to_i
if( n % 2 != 0 )
	puts("black")
end
if( n % 2 == 0 )
	puts("white\n1 2")
end


inp = gets.chomp
n = inp.to_i

if(n%2==1)
    puts "black"
else
    puts "white"
    puts "1 2"
end


n = gets.to_i
as = gets.split.map(&:to_i)
lefted = []
(n-1).downto(0) do |i|
  a = as[i]
  lefted << a unless lefted.include?(a)
end
puts lefted.size
puts lefted.reverse.join(" ")


gets
a=gets.split.reverse.uniq.reverse
p a.size
puts a*' '


n=gets.to_i
r=1
k=2
loop{
	x=(2**k-1)*(2**~-k)
	break if n<x
	r=x if n%x==0
	k+=1
}
p r


# Input number n
n = Integer(gets.chomp)

# Get binary view of our n
binary_size = (n == 1) ? ('%2b' % n).length : ('%b' % n).length

=begin
Find closest beautiful binary number to n.
It have in average (binary_size) elements if (binary_size) is odd
and (binary_view.size - 1) otherwise.
=end
closest_number = (binary_size.even?) ?
                     ('1' * (binary_size / 2) + '0' * (binary_size / 2 - 1)) :
                     ('1' * (binary_size / 2 + 1) + '0' * (binary_size / 2))

while n % closest_number.to_i(2) != 0
  # Cut from number first and last element
  closest_number = closest_number[1...-1]
end

print closest_number.to_i(2)


#!/usr/bin/env ruby
class Integer
  def cnt
    res = 0
    t = self
    while t != 0
      res += 1 if t.odd?
      t >>= 1
    end
    res
  end
end
MAP = {}
MAP['R'] = 0; MAP['G'] = 1; MAP['B'] = 2; MAP['Y'] = 3; MAP['W'] = 4
_ = gets.to_i
all = gets.split.uniq.map { |x| MAP[x[0]] * 5 + x[1].to_i - 1 }
ans = 10
(1 << 5).times do |i|
  (1 << 5).times do |j|
    flag = true
    all.each do |x|
      all.each do |y|
        next if x == y
        a, b = x.divmod(5)
        c, d = y.divmod(5)
        next if a != c && (i & (1 << a) > 0 || i & (1 << c) > 0)
        next if b != d && (j & (1 << b) > 0 || j & (1 << d) > 0)
        flag = false
        break
      end
      break unless flag
    end
    next unless flag
    now = i.cnt + j.cnt
    ans = now < ans ? now : ans
  end
end
puts ans


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)
  #TODO this wastes a lot of time
  combinations.inject(true) {
    |result, combination|
    a, b = combination
    check = check_ops_for_pair(ops, a, b)
    result && 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


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(true) {
    |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


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

if ( n % 2 == 0 ) then
    puts "No"
elsif ( a[0] % 2 == 0 || a[n - 1] % 2 == 0 )
    puts "No"
else
    puts "Yes"
end


n,*a=$<.read.split.map &:to_i
puts n&a[0]&a[-1]&1>0 ? :YES : :NO


n = gets.to_i
a = gets.split.map(&:to_i)
if a.size % 2 == 1 && a[0] % 2 == 1 && a[-1] % 2 == 1
  puts "Yes"
else
  puts "No"
end


TEST_MODE = ARGV[0] == 't'

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

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

# actual codes here
DAYS = [ 0,
  31, 28, 31, 30,
  31, 30, 31, 31,
  30, 31, 30, 31
]

def solve(input, output)
  m, d = parse_int_array(input[0])
  first = 8 - d
  n = 1 + (DAYS[m]-first+6)/7
  output << n.to_s
end

tests do
  # test cases
  t("1 7", "6")
  t("1 1", "5")
  t("11 6", "5")
end

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


def read_ints; gets.chomp.split.map(&:to_i) end
@n = read_ints[0]
min1,max1 = read_ints
min2,max2 = read_ints
min3,max3 = read_ints

@rez = [min1,min2,min3]

def left; @n - @rez.inject(:+) end

@rez[0]+=[left,max1-@rez[0]].min
@rez[1]+=[left,max2-@rez[1]].min
@rez[2]+=[left,max3-@rez[2]].min

puts @rez.join(' ')


n = gets.to_i
a = 3.times.map{gets.split.map(&:to_i)}
p = a.map{|x, y| x}
n -= p.inject(:+)
3.times do |i|
  mi = [a[i][1] - a[i][0], n].min
  p[i] += mi
  n -= mi
  break if n <= 0
end
puts p.join(' ')


def search(idx, num)
	wk = num + $s[idx].to_i
	if idx == $s.length-1
		ret = 0
		if wk % 2 == 0
			ret = 1
		else
			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)
		end
		return $ar[idx][nn]
	else
		nn = wk / 2
		if $ar[idx][nn] == nil
			$ar[idx][nn] = search(idx+1, nn)
		end
		if $ar[idx][nn+1] == nil
			$ar[idx][nn+1] = search(idx+1, nn+1)
		end
		return $ar[idx][nn]+$ar[idx][nn+1]
	end
end

$same = 1
$s = gets.chomp
$ar = Array.new($s.length).map!{|x| Array.new($s.length)}
if $s.length == 1
	puts 9
else
	for i in 0..$s.length-2
		w1 = $s[i].to_i
		w2 = $s[i+1].to_i
		wk = (w1 + w2)/2
		if wk != w2 && (wk+1) != w2
			$same = 0
			break
		end
	end

	ret = 0
	for i in 0..9
		ret += search(1,i)
	end
	puts ret-$same
end


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

if p-k > 1 ;
	print ?<, ?<, ' '
end

(p-k).upto(p+k) do |i|
	if i == p ;
		print ?(, i, ?), ' '
	elsif i >= 1 && i <= n ;
		print i, ' '
	end
end

if p+k < n ;
	print ?>, ?>, ' '
end

print ?\n


def get_paginate(page_count, current_page, proper)
  prev_numbers = []
  index = 1
  proper.times.each do
    number = current_page - index
    break if number.eql?(0)

    prev_numbers << number
    index += 1
  end
  next_numbers = []
  index = 1
  proper.times.each do
    number = current_page + index
    break if number > page_count

    next_numbers << number
    index += 1
  end
  prev_simbol = nil
  prev_simbol = '<<' if !prev_numbers.empty? && !prev_numbers.min.eql?(1)
  next_simbol = nil
  next_simbol = ' >>' if !next_numbers.empty? && !next_numbers.last.eql?(page_count)
  puts generate_paginate(prev_simbol, next_simbol, prev_numbers, next_numbers, current_page)
end

def generate_paginate(prev_simbol, next_simbol, prev_numbers, next_numbers, current_number)
  paginate_string = ''
  paginate_string += prev_simbol unless prev_simbol.nil?
  prev_numbers.sort.each do |number|
    paginate_string += " #{number}"
  end
  paginate_string += " (#{current_number})"
  next_numbers.sort.each do |number|
    paginate_string += " #{number}"
  end
  paginate_string += next_simbol unless next_simbol.nil?
  paginate_string.strip
end

input = gets.chomp

intput_array = input.split(' ').map(&:to_i)

get_paginate(intput_array[0], intput_array[1], intput_array[2])


(n, p, k) = gets.split.map(&:to_i)
result = ""
result << "<< " if p-k > 1
(([1, p-k].max)..(p-1)).each {|x| result << "#{x} "}
result << "(#{p}) "
((p+1)..([p+k, n].min)).each {|x| result << "#{x} "}
result << ">>" if p+k < n
puts result


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

if a>=n || b>=n || c >= n || c>a || c>b || n<1
  puts -1
else
  f = n - (a + b - c)
  if f > 0
    puts f
  else
    puts -1
  end
end


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

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


n = gets.strip.to_i

denominator = n - 1
numerator = 1.0
max = 0
max_numerator = 0
max_denominator = 0
def gcd(a, b)
    b == 0 ? a : gcd(b, a.modulo(b))
  end
while (numerator < denominator)
    if (numerator % denominator != 0 && numerator/denominator > max && gcd(numerator, denominator) == 1)
        max = numerator/denominator
        max_numerator = numerator
        max_denominator = denominator
    end
    numerator += 1
    denominator -= 1
end
puts "#{max_numerator.to_i} #{max_denominator.to_i}"


n = gets.to_i

a = n / 2
b = n - a

while b.gcd(a) != 1
	a -= 1
	b += 1
end

print a, " ", b


n = gets.to_i
((n - 1) / 2).downto(1) do |a|
    b = n - a
    if a.gcd(b) == 1
        puts "#{a} #{b}"
        break
    end
end


a,b=gets.split
a=a.split('').map(&:to_i)
k=b.to_i
i=0
while k>0&&i<a.size
	p=a[i,k+1].index(a[i,k+1].max)+i
	k-=(p-i)
	a.insert(i,a[p])
	a.delete_at(p+1)
	i+=1
end
puts a*''


a, k = gets.split.map(&:to_i)
a = a.to_s.split(//)

(0..a.size-1).each do |i|
    bound = [i+k, a.size-1].min
    best = a[i..bound].max
    if best != a[i]
        best_index = a[i..-1].find_index(best) + i
        (i+1..best_index).to_a.reverse.each do |j|
            a[j] = a[j-1]
        end
        a[i] = best
        k -= (best_index-i)
    end

end

puts a.join("")


as,k = gets.split
len = as.length
keta = as.chars.map { |c| c.ord - '0'.ord }
k = k.to_i

len.times do |idx|
  best = keta[idx]
  bestj = idx
  (idx+1...len).each do |j|
    if j - idx <= k and best < keta[j]
      best = keta[j]
      bestj = j
    end
  end
  if bestj != -1
    k -= bestj - idx
    while bestj > idx do
       keta[bestj-1],keta[bestj] = [keta[bestj],keta[bestj-1]]
       bestj -=1
    end
  end
end

puts keta.join


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

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

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

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

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

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

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

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


#!/usr/bin/ruby
include Math

inputs = gets.split.map{ |x| x.to_i}
ps = inputs[0..3]
a = inputs[4]
b = inputs[5]
result = 0
for i in a..b
  org_i = i
  ps.each do |x|
    i %= x
  end
  result += 1 if i == org_i
end
puts result


def min (a,b) if a<b then a else b end end
d,c,e,f,a,b=gets.split.map(&:to_i)
puts (a..b).map{|v| (v%[d,c,e,f].min==v)?1:0}.find_all{|v| v==1}.size


def gcd(a, b)
  if b == 0 then return a end
  return gcd(b, a%b)
end

n = gets.to_i
count = 0
i = 1
while i**2 < n
  j = i+1
  while i**2 + j**2 <= n
    if (gcd(i, j) == 1)
      count += n/(i**2 + j**2)
	end
	j += 2
  end
  i += 1
end

puts count


n = gets.to_i()

def gcd(a,b)
	if b==0
		return a
	end
	return gcd(b,a%b)
end

i = 1
ans = 0
while i*i*2 <= n
	j = i

	while j*j+i*i <= n
		if (((i^j)&1)==1) && (gcd(i,j)==1)
			len=j*j+i*i
			ans+=n/len
		end

		j+=1
	end

	i+=1

end

p ans


#!/usr/bin/ruby1.9

triple_num = {}
triples = {}
c_max = STDIN.readline.to_i
count = 0

(1..c_max).each do |c|
	triple_num[c] = 0
	triples[c] = []
end

def gcd(a, b)
	return a if b == 0
	return gcd(b, a%b)
end

m = 1
while true
	break if m*m + 1 > c_max
	(1..m-1).each do |n|
		next if (m-n)%2 == 0
		next if gcd(m, n) > 1
		sum = m*m + n*n
		break if sum > c_max
		difference = m*m-n*n
		product = 2*m*n
		k = 1
		while k*sum <= c_max
			a = k*difference
			b = k*product
			c = k*sum
			triple_num[c] += 1
			triple = [a, b, c]
			triples[c].push(triple)
			count += 1
			k += 1
		end
	end
	m += 1
end

puts count


#!/usr/bin/ruby1.8

(n, m, capacity) = STDIN.readline.split.map {|s| s.to_i}

planets = []
(0..n-1).each do
	name = STDIN.readline.strip
	products = []
	(0..m-1).each do
		(buy, sell, buy_limit) = STDIN.readline.split.map {|s| s.to_i}
		products.push({'buy' => buy, 'sell' => sell, 'buy_limit' => buy_limit})
	end
	planets.push({'name' => name, 'products' => products})
end

best = 0
(0..n-1).each do |i|
	i_products = planets[i]['products']
	(0..n-1).each do |j|
		#puts 'buy on %s, sell on %s' % [planets[j]['name'], planets[i]['name']]
		j_products = planets[j]['products']
		pairs = []
		(0..m-1).each do |k|
			profit = i_products[k]['sell'] - j_products[k]['buy']
			limit = j_products[k]['buy_limit']
			pairs.push([profit, limit])
		end
		pairs = pairs.sort.reverse
		#puts pairs.inspect
		earned = 0
		remainder = capacity
		pairs.each do |(profit, limit)|
			if profit <= 0
				break
			end
			bought = [remainder, limit].min
			earned += bought*profit
			remainder -= bought
			if remainder == 0
				break
			end
		end
		best = [best, earned].max
	end
end
puts best


class Product
	def initialize(a,b,c)
		@buy = a
		@sell = b
		@count = c
	end

	def buy
		return @buy
	end
	def sell
		return @sell
	end
	def count
		return @count
	end
end

class City
	def initialize(nam)
		@prod = []
		@name = nam
	end

	def add(a)
		@prod.push a
	end

	def get(i)
		return  @prod[i]
	end
	def name
		return @name
	end
end

N,M,K = gets.to_s.split(" ").collect{|x| Integer x}

cities = []
N.times do |n|
	nam = gets.to_s
	city = City.new(nam)
	M.times do |m|
		a,b,c = gets.to_s.split(" ").collect {|x| Integer x}
		p = Product.new(a,b,c)
		city.add p
	end
	cities.push city
end

max = 0
cities.permutation(2).each do |c1,c2|
	prof = 0
	profits = []
	k = K
	M.times do |m|
		profits.push([-c1.get(m).buy + c2.get(m).sell, c1.get(m).count])
	end
	profits.sort!{|x,y| y <=> x}
	profits.each do |p|
		if p[0] <= 0 then
			next
		end
		if k- p[1] < 0 then
			prof += k * p[0]
			k = 0
			break
		end
		prof += p[0] * p[1]
		k -= p[1]
	end
  #	puts "#{c1.name} #{c2.name} #{prof}"
	if prof > max then
		max = prof
	end
end

puts max


n,m,k=gets.split.map(&:to_i)
planets=n.times.map{
	gets
	m.times.map{gets.split.map(&:to_i)}
}
ans=0
planets.permutation(2).each{|p1,p2|
	t=[]
	m.times{|i|t<<[p2[i][1]-p1[i][0],p1[i][2]]}
	kk=k
	a=0
	t.sort_by{|d,c|d}.reverse.each{|d,c|
		break if d<=0
		num=[kk,c].min
		a+=num*d
		kk-=num
		break if kk==0
	}
	ans=a if a>ans
}
p ans


require "set"

memo = {}
step = ->x {
  memo[x] ||= x.to_s.chars.map(&:to_i).uniq.map { |b| x * b }
}

n, x = gets.split.map(&:to_i)
queue = [[x, 1]]
seen = Set.new
while queue.size > 0
  x, depth = queue.shift
  next if seen.include?(x)
  seen << x
  step[x].each do |y|
    if y.to_s.size == n
      p depth
      exit
    end
    queue << [y, depth + 1]
  end
end

p -1


# Input
n = gets.to_i

# Processing
arr = [0, 1]
step = 2
while arr[-1] + step < n - 1
  arr << arr[-1] + step
  step += 1 if arr.length.even?
end
#p arr

# Output
case n
when 1
  puts 2
when 2
  puts 3
else
  puts 2 + arr.length
end


require 'set'
a, b, c = gets.split.map(&:to_i)
s = Set.new
i = 1
while i != -1
  a %= b
  i = -2 if s.include? a
  s.add a
  a *= 10
  break if c == a / b
  i += 1
end
p i


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

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

found = false

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

print "-1\n" if not found


s = gets
ret = 1
hash = {}
for i in 0...s.size
  adjust = i == 0 ? 1 : 0
  ret *= 10 - adjust if s[i] == '?'
  if s[i] >= 'A' && s[i] <= 'J'
    ret *= 10 - hash.size - adjust unless hash[s[i]]
    hash[s[i]] = true
  end
end
puts ret


#!/usr/bin/ruby
h={}
f=10
x=1
gets.chomp.each_char.with_index{|c,i|
	if i==0&&!('0'..'9').include?(c)
		f=9
	end
	if c=='?'
		x*=10
	elsif ('A'..'J').include?(c)
		h[c]=1
	end
}
p x*(11-h.size..10).reduce(1,:*)*f/10


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

m = n
cnt5 = 0
while m % 5 == 0
	m /= 5
	cnt5 += 1
end

m = n
cnt2 = 0
while (m & 1) == 0
	m >>= 1
	cnt2 += 1
end

mul = 2**[0, k - cnt2].max * 5**[0, k - cnt5].max

p n * mul


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,0].max)
    n *= 5**([k-h5,0].max)
    p n
end


n, k = gets.split.map &:to_i
u = 10**k % n
g = u.gcd n
p n / g * 10**k


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

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

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

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
x1,y1,x2,y2=gsmi
x3,y3,x4,y4=gsmi
x5,y5,x6,y6=gsmi

# full cover
if (x3<=x1&&y3<=y1&&x4>=x2&&y4>=y2)||(x5<=x1&&y5<=y1&&x6>=x2&&y6>=y2)
  putsend('NO')
end
# half cover
# たて
if (y3<=y1&&y4>=y2 &&y5<=y1&&y6>=y2) && ((x5<=x2&&x6>=x2&&x3<=x1&&x4>=x1&&x4>=x5)||(x3<=x2&&x4>=x2&&x5<=x1&&x6>=x1&&x6>=x3))
  putsend('NO')
end
# よこ
if (x3<=x1&&x4>=x2 &&x5<=x1&&x6>=x2) && ((y5<=y2&&y6>=y2&&y3<=y1&&y4>=y1&&y4>=y5)||(y3<=y2&&y4>=y2&&y5<=y1&&y6>=y1&&y6>=y3))
  putsend('NO')
end

putsend('YES')


#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

hx = {}
hy = {}
a = inp
b = inp
c = inp
x = [a[0],a[2],b[0],b[2],c[0],c[2]].sort
y = [a[1],a[3],b[1],b[3],c[1],c[3]].sort
x.each.with_index do |d,i|
  hx[d] = i
end
y.each.with_index do |d,i|
  hy[d] = i
end
t = na2(x.size,y.size,false)
(hx[a[0]]...hx[a[2]]).each do |x|
  (hy[a[1]]...hy[a[3]]).each do |y|
    t[x][y] = true
  end
end
(hx[b[0]]...hx[b[2]]).each do |x|
  (hy[b[1]]...hy[b[3]]).each do |y|
    t[x][y] = false
  end
end
(hx[c[0]]...hx[c[2]]).each do |x|
  (hy[c[1]]...hy[c[3]]).each do |y|
    t[x][y] = false
  end
end

puts (t.flatten.include?(true))? "YES" : "NO"


n = gets.to_i
array = gets.chomp!.split(' ').map(&:to_i).sort
array[0], array[-1] = array[-1], array[0]
puts array.join(' ')


def run
  n = $stdin.gets.to_i
  a = $stdin.gets.split.map(&:to_i)
  max = a.delete_at(a.index(a.max))
  min = a.delete_at(a.index(a.min))

  puts ([max] + a.sort + [min]).join(" ")
end

run if $0 == __FILE__


class A
  def initialize
    n = gets.chomp.to_i
    num_list = gets.chomp.split(' ').map(&:to_i)
    max_num = num_list.max
    min_num = num_list.min
    num_list.delete_at(num_list.find_index(max_num))
    num_list.delete_at(num_list.find_index(min_num))

    answer = [max_num] + num_list.sort + [min_num]

    puts answer.join(' ')
  end
end

a = A.new


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

x = (n/k).to_i

puts (x+1)*k


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


pass = gets.strip
n = gets.strip.to_i
words = Array.new(n)
n.times do |i|
    words[i] = gets.strip
end
words.sort!
ans = "NO"
has_first = has_second = false
words.each do |w|
    has_first = true if !has_first && pass[1] == w[0]
    has_second = true if !has_second && pass[0] == w[1]
    if w == pass || (has_first && has_second)
        ans = "YES"
        break
    end
end
puts ans


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

def cuadratic( d )
    return ( -5 + Math.sqrt( 25 + 40*d ) ) / 10
end

puts [ cuadratic(240-k).to_i, n ].min


n,k=gets.split.map &:to_i
k=240-k
res=0
while res<n and k>=5*(res+1)
    res+=1
    k-=5*res;
end
puts res


#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
  min = 9999999
  now = 0
  t.size.times do |i|
    if(i-k >= 0)
      min = [now,min].min
      onf[t[i-k]] -= 1
      if(onf[t[i-k]] == 0)
        now -= 1
      end
    end
    if(onf[t[i]] == 0)
      now += 1
    end
    onf[t[i]] += 1
  end
  min = [now,min].min
  p min
end


require 'set'

def solve
  n, k, d = gets.chomp.split.map(&:to_i)
  a = gets.chomp.split.map(&:to_i)

  cnt = 1000 # INF
  # We want to get th minimum cnt
  0.upto(n-d).each do |i|
    s = Set.new
    i.upto(i+d-1).each do |j|
      s << a[j]
    end
    if s.size < cnt
      cnt = s.size
    end
  end

  p cnt
end

t = gets.chomp.to_i
0.upto(t-1).each do
  solve
end


require 'prime'

n = gets.to_i
a = n.prime_division

count = 0
=begin
while true

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

  if maxpow == 1
    break
  end

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

#  p a
#  p odd
#  p maxpow

  count += 1

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

#puts count
ans = 1
a.each do |x|
  ans*=x[0]
end
=end
a = n.prime_division
ans = 1
a.each do |x|
  ans*=x[0]
end

maxpow = 0
powers = []
a.each do |z|
  maxpow = z[1] if z[1]>maxpow
  powers.push z[1]
end

if powers.uniq.length>1 and (Math.log(maxpow)/Math.log(2))%1==0
  count += 1
end

while maxpow>1
  if maxpow%2 == 0
    maxpow/=2
    count += 1
  else
    maxpow = 2**(Math.log(maxpow)/Math.log(2)).ceil
    count += 1
  end
end
puts "#{ans} #{count}"


#!/usr/bin/ruby
# coding: utf-8
require 'rational'

a, x, y = $stdin.gets.split(/\s+/).map(&:to_i)
h = a.to_r / 2

if y % a == 0
  puts -1
elsif y < a
  if -h < x and x < h
    puts 1
  else
    puts -1
  end
else
  n = 2
  y1 = 2*a
  loop do
    if y < y1
      if -h < x and x < h
        puts n
      else
        puts -1
      end
      break
    end
    y1 += a
    if y < y1
      if -a < x and x < 0
        puts n+1
      elsif 0 < x and x < a
        puts n+2
      else
        puts -1
      end
      break
    end
    y1 += a
    n += 3
  end
end


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

a, x, y = read_ints

if y <= 0
  puts -1
  exit
end

if y % a == 0
  puts -1
  exit
end

yindex = (y + a) / a

if yindex == 1
  if (x*2).abs < a
    puts 1
    exit
  end
elsif yindex % 2 == 0
  if (x*2).abs < a
    puts (yindex/2 - 1) * 3 + 2
    exit
  end
elsif yindex % 2 == 1
  if x.abs < a
    base = (yindex/2 - 1) * 3 + 2
    if x < 0
      puts base + 1
      exit
    elsif x > 0
      puts base + 2
      exit
    end
  end
end

puts -1


n = gets.to_i
s = 0
n.times do |x|
  a = gets.split.map{|t| t.to_i }
  a.each_with_index do |e,i|
    if (i==(n-1)/2 || x==(n-1)/2 || i==x || i==n-x-1) then s+=e end
  end
end
p s


n = gets.to_i
sum = 0
(1..n).each do |i|
  j = 1
  gets.split.each do |e|
    if i == j || (i + j == n + 1) || i == n / 2 + 1 || j == n / 2 + 1
      sum += e.to_i
    end
    j += 1
  end
end

puts sum