a=gets.chomp.split(" ")
stone=a[2].to_i
s=a[0].to_i
a=a[1].to_i
ans=""
tog=0
loop do
t2=0
if tog==0 && stone.gcd(s)<=stone
stone-=stone.gcd(s)
ans="0"
tog=1
t2=1
elsif tog==1 && stone.gcd(a)<=stone
stone-=stone.gcd(a)
ans="1"
tog=0
t2=1
end
break if stone<1 || t2==0
end
if ans=="0"
puts "0"
else
puts "1"
end

m=gets.split.map &:to_i;i=0;i^=1while 0<m[2]-=m[i].gcd(m[2]);p i

def gcd(a, b)
    if a > b
        t = a
        a = b
        b = t
    end
    if b%a == 0
        a
    else
        gcd(b%a, a)
    end
end

inputs = gets.split.collect{|element| element.to_i}
counter = 1
while true
    if inputs[2] == 0 || inputs[2] < gcd(inputs[2], inputs[(counter-1)%2])
        puts counter%2
        break
    else
        inputs[2] = inputs[2] - gcd(inputs[2], inputs[(counter-1)%2])
    end
    counter = counter+1
end

a,b,n = gets.chomp.split(" ").map(&:to_i)
i=1
while n>0
    n-=n.gcd [a,b][i^=1]
end
p i

vals = gets.split.map{|s| s.to_i}
turn = -1
n = vals[2]
while n > 0
	turn += 1
	n -= n.gcd(vals[turn%2])
end
print turn%2

def mygcd a, b
	if a == b
		gcd = a
	elsif a == 0
		gcd = b
	elsif b == 0
		gcd = a
	else
		a, b = b, a if a-b < 0
		b, a = a, gcd while (gcd = b%a) > 0
		gcd = a
	end
	gcd
end

vals = gets.split.map{|s| s.to_i}
turn = -1
n = vals[2]
while n > 0
	turn += 1
	n -= mygcd(n, vals[turn%2])
end
print turn%2

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

A = gets.split(' ').map{|i| i.to_i}
a, b, n = A[0], A[1], A[2]
while n >= 0 do
    n -= a.gcd(n)
    if n < 0
        print 1
        next
    end
    n -= b.gcd(n)
    print 0 if n < 0
end

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

while true do
  k = (i%2 == 0 ? a : b)
  if n.gcd(k) > n
    break
  else
    n-= n.gcd(k)
    i+=1
  end
end

puts (i-1)%2

a,b,n=gets.split.map &:to_i;i=1;n-=[a,b][i^=1].gcd(n)while n>0;p i

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

i=0
while n>0 do
	t=n.gcd([a,b][i])
	i^=1
	n-=t
end

puts i^=1

def GCD a, b
	if b == 0 then a else GCD b, a%b end
end

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

player = :a
loop do
	take = GCD n, (player == :a ? a : b)

	if n<take
		puts (player == :a ? "1" : "0")
		break
	end

	n-=take
	player = (player == :a ? :b : :a)
end

a, b, n = gets.split.map &:to_i
i=1
while n>0
	n-=n.gcd [a,b][i^=1]
end
p i

a, b, n = gets.split.map &:to_i
i=1
n-=n.gcd [a,b][i^=1] while n>0
p i

a,b,n=gets.split.map &:to_i;i=1;n-=[a,b][i^=1].gcd n while n>0;p i

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

i=0
while n>0
	n-=n.gcd([a,b][i])
	i^=1
end

p i^=1

k=gets.split
a,b,k=k[0].to_i,k[1].to_i,k[2].to_i

while true
	break if k==0
	k-=k.gcd(a)
	ans=0
	break if k==0
	k-=k.gcd(b)
	ans=1
end
p ans

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

while true
  n = n - n.gcd(a)
  if n  < 0
    puts 1
    exit
  end
  n = n - n.gcd(b)
  if n < 0
    puts 0
    exit
  end
end

simon, antisimon, heap = gets.split.map(&:to_i)
simon_wins = false

while(true)
  heap -= simon.gcd(heap)
  break if heap < 0
  simon_wins = true
  heap -= antisimon.gcd(heap)
  break if heap < 0
  simon_wins = false
end

if simon_wins
  puts "0"
else
  puts "1"
end

a=Array.new(2){0}
a[0],a[1],n = gets.split.map(&:to_i)

def go(a, n)
idx = 0
loop do
    d = a[idx].gcd(n)
    return 1 - idx if d > n

    n -= d
    idx = 1 - idx
end
end

puts go(a,n)

# Epic Game
# http://codeforces.com/problemset/problem/119/A

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

simon, antisimon, stones = gets.chomp.split.map { |e| e.to_i }

count = 0

loop do
  if count % 2 == 0
    stones = stones - gcd(simon, stones)
  else
    stones = stones - gcd(antisimon, stones)
  end

  break if stones <= 0
  count += 1
end

puts "#{(count % 2 == 0) ? "0" : "1"}"

input = $stdin.readline.chop.split(' ')
a = input[0].to_i; b = input[1].to_i; n = input[2].to_i

loop do

  need = a.gcd(n)
  if n<need
    puts "1"
    break
  end
  n -= need

  need = b.gcd(n)
  if n<need
    puts "0"
    break
  end
  n-= need

end

input = gets.chomp
nums = input.split
a = Integer(nums[0])
b = Integer(nums[1])
n = Integer(nums[2])

def gcd(x,y)
    if x == y
        return  x
    else
        if x > y
            return gcd(y, x-y)
        else
            return gcd(x, y-x)
        end
    end
end

def sov(a,b,n,x)
    if n == 0
        if x == 0
            return 1
        else x == 1
            return 0
        end
    else
        if x == 0
            return sov(a,b,n-gcd(a,n),1-x)
        else x == 1
            return sov(a,b,n-gcd(b,n),1-x)
        end
    end
end

puts sov(a,b,n,0)

def gcd(a, b)
  min = a.abs
  max = b.abs
  while min > 0
    tmp = min
    min = max % min
    max = tmp
  end
  max
end

input = STDIN.gets().split(" ")

a = input[0].to_i
b = input[1].to_i
n = input[2].to_i

gameover = false
until gameover
  n = n- gcd(a,n) #S
  if(n < 0)
    gameover = true
    p 1
  else
      n = n- gcd(b,n) #A
      if(n < 0)
        gameover = true
        p 0
      end
  end

end

a,b,n=gets.split.map &:to_i
gcd=->(x,y){ y==0 ? x : gcd[y,x%y]}
x,y=1,0
while true
  g=gcd[a,n]
  if (g>n)
    puts x
    break
  end
  n-=g
  a,b,x,y=b,a,y,x
end

m=gets.split.map &:to_i;i=0;i^=1while 0<m[2]-=m[i].gcd(m[2]);p i

a, b, c = gets.split.map &:to_i
win = 1
while c > 0 do
  if win == 1
    c -= c.gcd(a)
  else
    c -= c.gcd(b)
  end
  win ^= 1
end
puts win

def NOD(a, b)
	while b != 0
		a, b = b, a % b
	end

	return a
end

l = gets.split.map(&:to_i)
o = 0

loop do
	t = NOD(l[o], l[2])
	if l[2] < t
		break
	end
	l[2] -= t
	o = 1 - o
end

puts 1 - o

a, b, n = gets.split.map(&:to_i)
i = 0
while (n > 0) do
	n -= n.gcd(i % 2 == 0 ? a : b)
	i += 1
end
p (i - 1) % 2

a, b, n = gets.split.map(&:to_i)
i = 0
while (n >= 0) do
	n -= n.gcd(i % 2 == 0 ? a : b)
	i += 1
end
p i % 2

a, b, n = gets.split.map{|_|_.to_i}
until n<0
  n -= n.gcd(a)
  if n<0
    puts 1
    break
  end
  n -= n.gcd(b)
  if n<0
    puts 0
  end
end

a1=gets
k=a1.split(" ")
a=k[0].to_i
b=k[1].to_i
n=k[2].to_i
#n=gets.to_i\
#puts a
#puts b
#puts n

while 1
i=a.gcd n
#puts i
if(n-i<0)
print "1"
exit
end
n=n-i
#puts n
j=b.gcd n
#puts j
if(n-j<0)
print "0"
exit
end
n=n-j
#puts n
end

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

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

i=0
pl=[a,b]
loop do
  v = pl[i%2]
  g = gcd(v, n)
  break if g > n
  n-=g
  i+=1
end

puts i.even? ? 1 : 0

class Integer
  def gcd(b)
    a = self
    a,b = b,a if b > a
    while b > 0
      a,b = b, a % b
    end
    a
  end
end
input = gets.chop.split(" ").map(&:to_i)
left = input[2]
took = 0
turn = 0
while left >= took do
  took = left.gcd(input[turn])
  (left < took)? (puts turn; exit) : left -= took
  (left < took)? (puts turn; exit) : left
  if turn == 0
    turn = 1
    else
    if turn == 1
    turn = 0
    end
  end
end

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

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

def get_abn(io)
  line = io.gets
  parts = line.split(' ')
  parts.map! {|p| p.to_i}
  return parts[0], parts[1], parts[2]
end

# greatest common divisor
def gcd(x, y)
  m, n = [x, y].sort
  until n==0 do
    m, n = [n, m%n]
  end
  return m
end
# def gcd(x, y)
#   for i in (1..100).to_a.reverse do
#     if x%i == 0 and y%i == 0 then
#       return i
#     end
#   end
# end

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

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

def nod(x,y)
    if x == 0 then
        return y
    elsif y == 0 then
        return x
    else
        if x > y then
            return nod(x-y,y)
        elsif y > x then
            return nod(x,y-x)
        else
            return x
        end
    end
end

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

x = a
res = 0
while nod(x,n) < n do
    n -= nod(x,n)
    if x == a then
        x = b
        res = 1
    else
        x = a
        res = 0
    end
end

puts res

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

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

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

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

a,b,n = gets.chomp.split(/ /).map!{|x| x.to_i}
while true do
  nn = a.gcd(n)
  n -= nn
  if n < 0 then
    puts 1
    break
  end
  nn = b.gcd(n)
  n -= nn
  if n < 0 then
    puts 0
    break
  end
end

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

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

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

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

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

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

def gcd x, y
	t = x > y ? y : x
	num = 1
	num.upto t do |e|
		num = e  if (x % e == 0 and y % e == 0)
	end
	num
end

loop do
	n -= turn ? gcd(b,n) : gcd(a,n)
	break if n == 0
	turn = !turn
end

puts turn ? 1 : 0

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

def gcd x, y
	t = x > y ? y : x
	num = 1
	num.upto t do |e|
		num = e  if (x % e == 0 and y % e == 0)
	end
	num
end

loop do
	n -= turn == 0 ? gcd(a,n) : gcd(b,n)
	break if n == 0
	turn = turn == 0 ? 1 : 0
end

puts turn

input=gets.split.map(&:to_i)
heap=input[2]
k=0
10000.times{|k|
        heap-=heap.gcd(input[k%2])
        if heap<0 then
                puts 1-k%2
                exit
        end
}

m=gets.split.map &:to_i;i=0;i^=1while 0<m[2]-=m[i].gcd(m[2]);p i

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

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

def get_abn(io)
  line = io.gets
  parts = line.split(' ')
  parts.map! {|p| p.to_i}
  return parts[0], parts[1], parts[2]
end

# greatest common divisor
def gcd(x, y)
  for i in (1..100).to_a.reverse do
    if x%i == 0 and y%i == 0 then
      return i
    end
  end
end

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

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

def gcd(a, b)
  while a!=0 and b!=0 do
    if a > b
        a = a % b
    else
        b = b % a
    end
  end
  return a+b
end

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

loop do
  n -= gcd(a, n)
  if n < 0
    puts 1
    break
  end

  n -= gcd(b, n)
  if n < 0
    puts 0
    break
  end
end

a = gets.chomp.split.map(&:to_i)
n = a[2]
a.pop
(n + 1).times do |i|
	n -= a[i % 2].gcd(n)
	if n < 0
		if i % 2 == 1
			puts 0
		elsif i % 2 == 0
			puts 1
		end
		break
	end
end

def solve(a, b, n)
  while true
    d = a.gcd(n)
    return 1 if d>n
    n -= d

    d = b.gcd(n)
    return 0 if d>n
    n -= d
  end

end

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

n = readline.split(' ')
semen = true
antisemen = true

while (semen && antisemen) do

  if(n[2].to_i - (n[0].to_i.gcd n[2].to_i) >= 0 && n[2].to_i != 0)
    begin
      n[2] = n[2].to_i - (n[0].to_i.gcd n[2].to_i)
      semen = true
    end
  else
    semen = false
    break
  end

  if(n[2].to_i - (n[1].to_i.gcd n[2].to_i) >= 0  && n[2].to_i != 0)
    begin
      n[2] = n[2].to_i - (n[1].to_i.gcd n[2].to_i)
      antisemen = true
    end
  else
    antisemen = false
    break
  end

end

if(semen == true && antisemen == false)
  puts "0"
else
  puts "1"
end

#! ruby -Ku
# -*- coding: UTF-8 -*-
begin
  inputs = open("input.txt")
rescue
  inputs = STDIN
end

a_b_n = inputs.gets.split.map(&:to_i)
a = a_b_n.shift
b = a_b_n.shift
n = a_b_n.shift

antisimon_wins = 1
(n+1).times do |i|
  if i % 2 == 0 # Simon
    div = n.gcd(a)
    if div > n
      antisimon_wins = 1
      break
    else
      n -= div
    end
  else # Antisimon
    div = n.gcd(b)
    if div > n
      antisimon_wins = 0
      break
    else
      n -= div
    end
  end
end

if antisimon_wins == 1
  puts antisimon_wins
else
  puts antisimon_wins
end

s,a,n=gets.split.map &:to_i
loop do
  if s.gcd(n)>n
    puts 1
    break
  else
    n=n-s.gcd(n)
  end

  if a.gcd(n)>n
    puts 0
    break
  else n=n-a.gcd(n)
  end
end

module Emaxx
    def self.gcd(a,b)
        while b!=0
            a, b = b, a%b
        end
        a
    end
end

l = $stdin.readline.split
a = l[0..1].map(&:to_i)
n = l.last.to_i

who = 0
t = Emaxx.gcd(a[who], n)
while t>0 && n>=t
    n -= t
    who = (who+1)%2
    t = Emaxx.gcd(a[who], n)
end

puts (who+1)%2

def gcd(a, b)
  return gcd(b, a) if a < b

  return a if b.zero?

  gcd(b, a%b)
end

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

z = [a, b]
id = 0
while true
  p = gcd(z[id], n)
  id = 1 - id

  break if n < p

  n -= p
end

puts id

def gcd(a, b)
    if (a == 0) then return b end
    return gcd(b%a, a)
end
a,b,n = gets.split(' ').map{|s| s.to_i}
w = 0
while (1)
  if (w == 0) then n -= gcd(n, a)
  else n -= gcd(n, b)
  end
  w ^= 1
  if (n < 0) then break end
end
puts w

def game
  stuff = gets.split.map(&:to_i)
  a = stuff[0]
  b = stuff[1]
  stones = stuff[2]
  games = 0

  while stones > 0
    if games % 2 == 0
      stones -= a.gcd(stones)
    else
      stones -= b.gcd(stones)
    end
    games += 1
  end

  puts (games % 2 == 0) ? 1 : 0
end

game

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

a, b, n = gets.split(' ').map { |i| i.to_i }
cur = 0
while true
  if cur == 0
    take = gcd(n, a)
  else
    take = gcd(n, b)
  end
  break if take > n
  n -= take
  cur = 1 - cur
end
puts 1 - cur

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

ans=0
loop do
    ans=1-ans
    x=n
    y=a
    while y>0
        z=x%y
        x, y = y, z
    end
    a, b = b, a
    if n<x then break end
    n-=x
end
puts ans

a,b,c=gets.split.map(&:to_i)
i=1
while c>0
	i=1-i
	c-=([a,b][i]).gcd(c)
end
p i

#!/usr/bin/ruby -Ks

input = $stdin.gets.chomp.split
$a = input[0].to_i
$b = input[1].to_i
$n = input[2].to_i

def getGCD(n1, n2)
  if n1 == n2
    return n1
  end
  if n2 == 0
    return n1
  end

  if n1 < n2
    start_value = n1
  else
    start_value = n2
  end

  start_value.downto(1) do |i|
    if (( n1 % i == 0 )&&( n2 % i == 0 ))
      return i
    end
  end
end

isSimon = true
while true
  x = 0
  if isSimon
    x = getGCD($a,$n)
  else
    x = getGCD($b,$n)
  end
  $n -= x
  if $n < 0
    if isSimon
      puts 1
      break
    else
      puts 0
      break
    end
  end

  if isSimon
    isSimon = false
  else
    isSimon = true
  end
end

# EOF

#!/usr/bin/ruby

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

def gcd(a, b)
  while b > 0
    c = a % b
    a = b
    b = c
  end
  return a
end

def solve(a, b, n)
  loop do
    n -= gcd(a, n)
    return 1 if n < 0
    n -= gcd(b, n)
    return 0 if n < 0
  end
end

### main

a, b, n = readNextValues

puts(solve(a, b, n))

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

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

end

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

loop do
	[a, b].each.with_index do |i, k|
		n -= i.gcd n
		if n <= 0
			puts k == 1 ? 1 : 0
			exit
		end
	end
end

m=gets.split.map &:to_i;i=0;i^=1while 0<m[2]-=m[i].gcd(m[2]);p i

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

simon = input[0]
antisimon = input[1]
heap = input[2]
isSimon = true
calcGcd = 0

loop do
	calcGcd = simon.gcd(heap) if isSimon
	calcGcd = antisimon.gcd(heap) if !isSimon

	break if heap < calcGcd

	heap -= calcGcd

	isSimon = !isSimon
end

puts 0 if !isSimon
puts 1 if isSimon

def f(n,t,a,b)
		if n==0 then
			return t
		end
		t=(t==0)?1:0
		n2=n-n.gcd(a)
		return f(n2,t,b,a)
end

a,b,n=gets.split.map{|e| e.to_i}
puts f(n,1,a,b)

a,b,n=gets.split.map &:to_i;i=1;n-=[a,b][i^=1].gcd(n)while n>0;p i

#!/usr/bin/env ruby
require 'mathn'

a,b,c = gets.split.map{ |i| i.to_i }
d = 0
while true
  e = a.gcd(c)
  if d == 1
    e = b.gcd(c)
  end
  c -= e
  break if c < 0
  d ^= 1
end
d ^= 1
puts d

a, b, n = gets.split.map(&:to_i)
g = a.gcd(n)
flag = 0
while n >= g
	if flag == 0
		n -= g
		g = b.gcd(n)
		flag = 1
	else
		n -= g
		g = a.gcd(n)
		flag = 0
	end
end
if flag == 1
	puts 0
else
	puts 1
end

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

i=1
while n>0
	n-=n.gcd([a,b][i^=1])
end

p i

a,b,n=gets.split.map &:to_i;i=1;n-=n.gcd [a,b][i^=1] while n>0;p i

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

i=0
while n>0
	t=n.gcd([a,b][i])
	i^=1
	n-=t
end

p i^=1

a,b,n=gets.split.map &:to_i;i=0;n-=n.gcd [a,b][i^=1]until n<0;p i

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

i=0
while n>0
	n-=n.gcd([a,b][i^=1])
end

p i^=1

include Math

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

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

include Math
line = gets
parts = line.split(' ')
a = parts[0]
b = parts[1]
n = parts[2]
print a, b, n

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

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

def get_abn(io)
  line = io.gets
  parts = line.split(' ')
  parts.map! {|p| p.to_i}
  return parts[0], parts[1], parts[2]
end

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

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

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

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

#! ruby -Ku
# -*- coding: UTF-8 -*-
begin
  inputs = open("input.txt")
rescue
  inputs = STDIN
end

a_b_n = inputs.gets.split.map(&:to_i)
a = a_b_n.shift
b = a_b_n.shift
n = a_b_n.shift

antisimon_wins = 1
(n+1).times do |i|
  if i == 0 # Simon
    div = n.gcd(a)
    if div > n
      antisimon_wins = 1
      break
    else
      n -= div
    end
  else # Antisimon
    div = n.gcd(b)
    if div > n
      antisimon_wins = 0
      break
    else
      n -= div
    end
  end
end

if antisimon_wins == 1
  puts antisimon_wins
else
  puts antisimon_wins
end

#! ruby -Ku
# -*- coding: UTF-8 -*-
begin
  inputs = open("input.txt")
rescue
  inputs = STDIN
end

a_b_n = inputs.gets.split.map(&:to_i)
a = a_b_n.shift
b = a_b_n.shift
n = a_b_n.shift

antisimon_wins = 1
n.times do |i|
  if i == 0 # Simon
    div = n.gcd(a)
    if div > n
      antisimon_wins = 1
      break
    else
      n -= div
    end
  else # Antisimon
    div = n.gcd(b)
    if div > n
      antisimon_wins = 0
      break
    else
      n -= div
    end
  end
end

if antisimon_wins == 1
  puts antisimon_wins
else
  puts antisimon_wins
end

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

end

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

def gcd a, b
	return a if b.zero?
	gcd b, a % b
end

loop do
	[a, b].each do |i|
		gcd = gcd i, n
		if n - gcd < 0
			puts i == a ? 1 : 0
			exit
		end
		n -= gcd
	end
end

a1=gets
k=a1.split(" ")
a=k[0].to_i
b=k[1].to_i
n=k[2].to_i
#n=gets.to_i
while 1
i=a.gcd n
if(n-i<0)
print "1"
exit
end
n=n-i
j=b.gcd n-i
if(n-j<0)
print "0"
exit
end
n=n-j
end

a1=gets
k=a1.split(" ")
a=k[0].to_i
b=k[1].to_i
n=k[2].to_i
#n=gets.to_i\
puts a
puts b
puts n

while 1
i=a.gcd n
puts i
if(n-i<0)
print "1"
exit
end
n=n-i
puts n
j=b.gcd n
puts j
if(n-j<0)
print "0"
exit
end
n=n-j
puts n
end

def f(n,t,a,b)
		if n<a then
			return t
		end
		t=(t==0)?1:0
		n2=n-n.gcd(a)
		return f(n2,t,b,a)
end

a,b,n=gets.split.map{|e| e.to_i}
puts f(n,1,a,b)


x, y = gets.split(' ').map(&:to_i)
t1 = x.abs+y.abs
t2 = y>0 ? t1 : -t1
if x>0
  puts "0 #{t2} #{t1} 0"
else
  puts "#{-t1} 0 0 #{t2}"
end

# The truth is, the sample doesn't correct
# A(0,2y) C(2x,0) makes the area minimum

# if x>0
#   puts "0 #{2*y} #{2*x} 0"
# else
#   puts "#{2*x} 0 0 #{2*y}"
# end

#monkeycode
x, y =gets.split.map{|i| i.to_i}
if(x>0) then
  if(y>0) then
    puts("0 #{x+y} #{x+y} 0")
  else
    puts("0 #{y-x} #{x-y} 0")
  end
else
  if(y>0) then
    puts("#{x-y} 0 0 #{y-x}")
  else
    puts("#{x+y} 0 0 #{x+y}")
  end
end

x,y=gets.split(" ").map { |i| i.to_i}
z=x.abs+y.abs
if x>0 and y>0
	print "0 ",z," ",z," 0"
elsif x>0 and y<0
	 print "0 ",-z," ",z," 0"
elsif x<0 and y>0
	print -z," 0  0 ",z
else
	print -z," 0 0 ",-z
end

def run
  x, y = $stdin.gets.split.map(&:to_i)
  puts [[(x.abs + y.abs) * (x / x.abs), 0].join(" "),
   [0, (y.abs + x.abs) * (y / y.abs)].join(" ")].sort.join(" ")
end

run if $0 == __FILE__

a=gets.chomp.split(" ")
x=a[0].to_i
y=a[1].to_i
an=x.abs+y.abs
if x>0 && y>0
puts "0 #{an} #{an} 0"
elsif x<0 && y>0
puts "#{-an} 0 0 #{an}"
elsif x>0 && y<0
puts "0 #{-an} #{an} 0"
elsif x<0 && y<0
puts "#{-an} 0 0 #{-an}"
end

x,y=gets.split.map(&:to_i)
if x > 0 and y > 0
    puts "0 #{x+y} #{x+y} 0"
elsif x > 0 and y < 0
    puts "0 #{y-x} #{x-y} 0"
elsif y > 0
    puts "#{-(y-x)} 0 0 #{y-x}"
else
    puts "#{x+y} 0 0 #{x+y}"
end

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

r = x.abs + y.abs

x1 = 0
y1 = r*y/y.abs
x2 = r*x/x.abs
y2 = 0

if x1 >= x2
	puts "#{x2} #{y2} #{x1} #{y1}"
else
        puts "#{x1} #{y1} #{x2} #{y2}"
end

x, y = gets.split.map { |x| x.to_i }
if 0 < x && 0 < y
  x1 = 0
  y1 = y + x
  x2 = x + y
  y2 = 0
else
  if 0 < x && 0 > y
    x1 = 0
    y1 = y - x
    x2 = x - y
    y2 = 0
  else
    if 0 > x && 0 > y
      x1 = x + y
      y1 = 0
      x2 = 0
      y2 = y + x
    else
      x1 = x - y
      y1 = 0
      x2 = 0
      y2 = y - x
    end
  end
end
print x1, ' ', y1, ' ', x2, ' ', y2

x, y = gets.split.map { |x| x.to_i }

sum = x.abs + y.abs

if x > 0
	if y > 0
		puts [0, sum, sum, 0].join ' '
	else
		puts [0, -sum, sum, 0].join ' '
	end
else
	if y > 0
		puts [-sum, 0, 0, sum].join ' '
	else
		puts [-sum, 0, 0, -sum].join ' '
	end
end

x,y=gets.split(" ").map { |i| i.to_i}
z=x.abs+y.abs
if x>0 and y>0
	print "0 ",z," ",z," 0"
elsif x>0 and y<0
	 print "0 ",-z," ",z," 0"
elsif x<0 and y>0
	print -z," 0  0 ",z
else
	print "0 ",-z," ",-z," 0"
end

x,y=gets.split(" ").map { |i| i.to_i}
z=x.abs+y.abs
if x>0 and y>0
	print "0 ",z," ",z," 0"
elsif x>0 and y<0
	 print "0 ",-z," ",z," 0"
elsif x<0 and y>0
	print -z," 0  0 ",z
else
	print "0 ",-z,-z," 0"
end

x,y=gets.split(" ").map { |i| i.to_i}
z=x.abs+y.abs
if x>0 and y>0
	print "0 ",z," ",z," 0"
elsif x>0 and y<0
	 print "0 ",-z,z," 0"
elsif x<0 and y>0
	print -z," 0  0 ",z
else
	print "0 ",-z,-z," 0"
end

x,y=gets.split.map(&:to_i)
if x > 0 and y > 0
    puts "0 #{x+y} #{x+y} 0"
elsif x > 0 and y < 0
    puts "0 #{x-y} #{x-y} 0"
elsif y > 0
    puts "#{-(y-x)} 0 0 #{y-x}"
else
    puts "#{x+y} 0 0 #{-(x+y)}"
end

x,y=gets.split.map(&:to_i)
if x > 0 and y > 0
    puts "0 #{x+y} #{x+y} 0"
elsif x > 0 and y < 0
    puts "0 #{y-x} #{x-y} 0"
elsif y > 0
    puts "#{-(y-x)} 0 0 #{y-x}"
else
    puts "#{x+y} 0 0 #{-(x+y)}"
end

x,y=gets.split.map(&:to_i)
if x > 0 and y > 0
    puts "0 #{x+y} #{x+y} 0"
elsif x > 0 and y < 0
    puts "0 #{x-y} #{x-y} 0"
elsif y > 0
    puts "#{-(y-x)} 0 0 #{y-x}"
else
    puts "#{x+y} 0 0 #{x+y}"
end


userInput = gets.chomp

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

result ||= 0
result +=

loopNum = 0

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

	loopNum += 1
end

puts result

# coding: utf-8

s = gets.chomp.split(//).reverse
o = 0
for i in 0...s.length
    if s[i] == "4"
        o += 1 * (2**i)
    else
        o += 2 * (2**i)
    end
end

puts o

number = gets.chomp()

number.reverse!

sum = 0

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

puts sum

number = gets.chomp()

number.reverse!

table = {4=>1, 7=>2}

sum = 0

for i in 0..number.length
	sum += (2 ** i) * table[number[i].to_i].to_i
end

puts sum

n = gets.chomp.chars.map(&:to_i)
ans = 0
n.size.times do |i|
  if n[i] == 4
    ans += 2 ** (n.size - i - 1)
  elsif n[i] == 7
    ans += 2 * 2 ** (n.size - i - 1)
  end
end
puts ans

def lucky_numbers(numbers, current, limit)
    numbers.push(current)

    if current >= limit
        return numbers
    end

    numbers = lucky_numbers(numbers, current * 10 + 4, limit)

    lucky_numbers(numbers, current * 10 + 7, limit)
end

n = gets.to_i

puts lucky_numbers([], 4, n).concat(lucky_numbers([], 7, n)).sort.find_index(n) + 1

#!/usr/bin/env ruby
a=[]
1.step(9){|i|
	[4,7].repeated_permutation(i){|e|a<<e.join.to_i}
}
a.sort!
p a.index(gets.to_i)+1

# http://codeforces.com/contest/535/problem/B
digits = gets.chomp.chars.map(&:to_i)

ans = 0
digits.each.with_index do |v, idx|
  case v
  when 4
    ans += 2**(digits.length - idx - 1)
  when 7
    ans += (2**(digits.length - idx - 1)) * 2
  end
end

puts ans

n = gets.chomp
l = n.length
a = 0
(0..l-1).each do |i|
	a <<= 1
	if n[i] == '7'
		a += 1
	end
end
a += (1 << l) - 1
puts a

def f(n,b,ans)
	if n.size==0 then
		return ans
	else
		e=n[n.size-1]
		if e=="4" then
			return f(n.chop,b*2,ans+b)
		else
			return f(n.chop,b*2,ans+b*2)
		end
	end
end

s=gets.chomp
puts f(s,1,0)

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

c = gets.chomp
len = c.length
max = 0
for i in 0..len-1
	if c[i] == '4'
		max += 2**(len - i -1)
	else
		max += 2**(len -i -1)*2
	end
end
puts max

userInput = gets.chomp
result = 0

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

loopNum = 0

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

	loopNum += 1
end

puts result

number = gets.chomp()

number.reverse!

sum = 0

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

puts sum


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

p = q = 0
a.each_with_index do |v, i|
  dp = (v - (i*2+1)).abs
  dq = (v - (i*2+2)).abs
  p += dp
  q += dq

end

puts p < q ? p : q

n=gets.to_i
a=gets.split.map(&:to_i).sort
p [1,2].map{|s|a.zip(s.step(n,2).to_a).map{|x,i|(x-i).abs}.reduce :+}.min

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

p = q = 0
a.each_with_index do |v, i|
  p += (v - (i+1)).abs
  q += (v - (i+2)).abs
end

puts p < q ? p : q

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

p = q = 0
a.each_with_index do |v, i|
  dp = (v - (i*2+1)).abs
  dq = (v - (i*2+2)).abs
  p += dp
  q += dq

end

puts p < q ? p : q


def solution(n, pices)
  pices.sort!
  min_diff = 2 << 2048

  for i in n-1...pices.size do
    min_diff = [min_diff, pices[i] - pices[i - n + 1]].min
  end

  min_diff
end

n, m = gets.strip.split().map { |i| i.to_i }
pices = gets.strip.split().map { |i| i.to_i }

puts solution(n, pices)

# cook your code here
n,m=gets.split(/ /).map! {|e| e=e.to_i}
sa=gets.split(/ /).map! {|e| e=e.to_i}
sa = sa.sort { |x,y| x<=>y  }
min=100000000000

for i in 0..(m-n)
    j=i+n-1
    l=(sa[j]-sa[i])
    if(min>l)
        min=l
    end
end
puts min

#!ruby

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

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

ans = 9999

p.sort!

(m-n+1).times do |i|
  ans = [ans, p[i+n-1]-p[i]].min
end

puts ans

n, m = gets.split(' ').map(&:to_i)
ar = gets.split(' ').map(&:to_i).sort
n -= 1
puts (n...m).map{|x| ar[x]-ar[x-n]}.min

n_m = gets.split.map &:to_i
n = n_m[0]
m = n_m[1]

puzzles = gets.split.map(&:to_i).sort

min_difficulty = Float::INFINITY

(m - n + 1).times do |i|
  difficulty = puzzles[i+n-1] - puzzles[i]
  min_difficulty = difficulty if difficulty < min_difficulty
end

puts min_difficulty

#!/usr/bin/ruby
n=gets.to_i
p gets.split.map(&:to_i).sort.each_cons(n).map{|e|e.last-e.first}.min

tmp = gets.chomp.split(" ")
n = tmp[0].to_i
m = tmp[1].to_i
a = gets.chomp.split(" ").map {|x| x.to_i}
a = a.sort
min = a[n-1] - a[0]
for i in 1..m-n
  min = a[i+n-1]-a[i] if a[i+n-1]-a[i] < min
end
puts min

m,n = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
min_diff = arr.max
arr.sort!
for i in (m-1..n-1)
  diff = arr[i] - arr[i- m +1]
  min_diff = diff if diff < min_diff
end
puts min_diff

g=->{gets.split.map(&:to_i)}
n,m=g[]
a=g[].sort
p (m-n+1).times.map{|i|a[i+n-1]-a[i]}.min

n, m=gets.split(" ").map {|i| i.to_i}
arr = gets.split(" ").map{|i| i.to_i}.sort!
answer=(m-n+1).times.map{|i| arr[i+n-1]-arr[i]}.min

p answer

a, b = gets.split.map &:to_i
p = gets.split.map(&:to_i).sort!

min = p[-1] - p[0]

for i in (a-1..b-1)
  min = [min, p[i] - p[i - a + 1]].min
end

puts min

inp = STDIN.gets <<-EOF
EOF

inp = inp.split("\n")

stu_num = inp[0].split(" ")[0].to_i
puz_num = inp[0].split(" ")[1].to_i
puz_difs = inp[1].split(" ").map(&:to_i).sort

min_dif_div = if stu_num < puz_num
                puz_difs.first(puz_difs.length - stu_num + 1).inject([]) do |divs,i|
                  divs << (puz_difs[puz_difs.index(i) + stu_num - 1] - i)
                end.sort.first
              else
                puz_difs.max - puz_difs.min
              end

puts min_dif_div

n,m=gets.chomp.split.map(&:to_i)
a=gets.chomp.split.map(&:to_i).sort
min=a.last
i=0
while i+n-1<m
	if (a[i+n-1]-a[i])<min
		min=a[i+n-1]-a[i]
	end
	i+=1
end
puts min

n,m = gets.split(" ").map(&:to_i)
a = gets.split(" ").map(&:to_i).sort
min = 99999
for i in 0..(m-n)
    if(min>a[i+n-1]-a[i])
        min = a[i+n-1]-a[i]
    end
end
print(min)

n,m=gets.split.map(&:to_i)
a=gets.split.map(&:to_i).sort
r=1001
(m-n+1).times {|i|r=[r,a[i+n-1]-a[i]].min}
p r

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

min = 2000
while a.size >= n
  min = [min, a[n-1]-a.first].min
  a.shift
end

puts min

MAX_INT = 1<<31

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

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

    def complect_and_count
        self.dup.complect_and_count!
    end

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

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

# t_s = ff.collect do |key,value|
#   [key]*value
# end

# t = t_s.dup

# идея алгоритма: вокруг каждого элемента начинаю образовывать круг минимального радиуса
# когда будет построен круг содержащий более n элементов - начинаю смотреть какой из кругов лучший

# for each f_uniq we build an area
areas = []
for i in 0...f_uniq.count
    elem = f_uniq[i]
    areas[i] = {elements: [elem]*ff[elem], diameter: 0}
end

def area_found?(areas,n)
    a_with_max_count = areas.max_by {|e| e.count}
    a_with_max_count[:elements].count >= n
end

l = 1
until area_found?(areas,n)
    # add one elem to each area
    areas.each do |a|
        elements = a[:elements]

        next if elements.count >= l

        a_min = elements.min
        a_max = elements.max
        t_min, _ = f_uniq.partition {|el| el<a_min}
        t_max, _ = f_uniq.partition {|el| el>a_max}
        t_min = t_min.last
        t_max = t_max.first
        d1 = t_min.nil? ? MAX_INT : a_min - t_min
        d2 = t_max.nil? ? MAX_INT : t_max - a_max

        if d2 <= d1
            a[:elements] = elements + [t_max]*ff[t_max]
            a[:diameter] += d2
        elsif d1 < d2
            a[:elements] = ([t_min]*ff[t_min]) + elements
            a[:diameter] += d1
        end
    end
    l+=1
end

min = areas.min_by {|el|  el[:elements].count >= n ? el[:diameter] : MAX_INT }
puts min[:diameter]

input = ARGV ? $< : File(ARGV[0])

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

arr.sort!()

diff = arr[n-1] - arr[0]
stop = m - n
for i in (1..stop)
	local = arr[n-1+i] - arr[i]
	diff = [diff, local].min
end

puts diff

m, n = gets.chomp.split.map { |x| x.to_i }
a = gets.chomp.split.map { |x| x.to_i }.sort
min = 1000
for i in 0..n - m
  diff = a[i + m - 1] - a[i]
  min = diff if diff < min
end
puts min

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

ary = gets.chomp.split(' ').map!(&:to_i).sort!.reverse!

min = ary.first - ary.last

for i in 0..(ary.size - n) do
  if ary[i] - ary[i + n - 1] < min
    min =  ary[i] - ary[i + n - 1]
  end
end

p min

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

def run
  n, m = $stdin.gets.split.map(&:to_i)
  f = $stdin.gets.split.map(&:to_i)
  f.sort!

  min = 1 << 31
  for i in 0..(m - n)
    d = f[i,n].max - f[i,n].min
    min = [d, min].min
  end

  puts min
end

run if $0 == __FILE__

n, m = gets.split.map &:to_i
arr = gets.split.map &:to_i
arr = arr.sort
min = arr.max
(m - n + 1).times do |fig|
  min = arr[fig + n - 1] - arr[fig] if min > arr[fig + n - 1] - arr[fig]
end
puts min

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

a,b=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}.sort
ans=x[a-1]-x[0]
0.upto(x.size-a){|i|
	ans=[ans,x[i+a-1]-x[i]].min
}
puts ans

n, m = gets.split.map(&:to_i)
f = gets.split.map(&:to_i).sort
min_diff = 1001

(0..m-n).each do |i|
	if f[i+n-1]-f[i] < min_diff
		min_diff = f[i+n-1] - f[i]
	end
end
puts min_diff

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

mins = []
(0..m-n).each do |i|
	mins << f[i+n-1] - f[i]
end

puts mins.min

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

min = array.last
0.upto(m - n) do |index|
  min = [array[index + n - 1] - array[index], min].min
end

puts min

nm = gets.chomp
nm = nm.split
students = nm[0].to_i
puzzles = nm[1].to_i
quantities = gets.chomp
quantities = quantities.split

for i in (0..puzzles-1)
	for x in (i..puzzles-1)
		if i != x
			if quantities[i].to_i > quantities[x].to_i
				temp = quantities[i].to_i
				quantities[i] = quantities[x].to_i
				quantities[x] = temp
			end
		end
	end
end

minimum = 0
for i in (0..puzzles-1)
	if quantities[i+students-1].to_i - quantities[i].to_i < minimum && i > 0 && i+students-1 <= puzzles - 1
		minimum = quantities[i+students-1].to_i - quantities[i].to_i
	elsif i == 0 && i+students-1 <= puzzles - 1
		minimum = quantities[i+students-1].to_i - quantities[i].to_i
	end
end

print minimum

#!/usr/bin/env ruby

# Solves http://codeforces.com/problemset/problem/337/A

n, m = $stdin.gets.split.map { |s| s.to_i }
nums = $stdin.gets.split.map { |s| s.to_i }
nums.sort!
diffs = (0..(m-n)).map { |i| nums[n + i - 1] - nums[i] }
puts diffs.sort![0]

n, m = gets.split.map{|x| x.to_i}
a = gets.split.map{|x| x.to_i}.sort
p (m - n + 1).times.map{|i| a[i + n - 1] - a[i]}.min

def main
    n = gets.chomp.to_i
    fi = gets.chomp.split(" ").map{ |e| e.to_i }.sort

    r = 0
    r_s = fi[n-1]-fi[0]
    i = 1
    k = fi.size - n + 1
    while  i<k
        s = fi[i+n-1]-fi[i]
        if (s<r_s)
            r = i
            r_s = s
        end
        i += 1
    end
    puts r_s
end

main()

def main
    n = gets.chomp.to_i
    fi = gets.chomp.split(" ").map{ |e| e.to_i }.sort

    r = 0
    r_s = fi[n-1]-fi[0]
    i = 1
    k = fi.size - n
    while  i<k
        s = fi[i+n-1]-fi[i]
        if (s<r_s)
            r = i
            r_s = s
        end
        i += 1
    end
    puts r_s
end

main()

# cook your code here
n,m=gets.split(/ /).map! {|e| e=e.to_i}
sa=gets.split(/ /).map! {|e| e=e.to_i}
sa = sa.sort { |x,y| x<=>y  }
puts (sa[n-1]-sa[0])

n_m = gets.split.map &:to_i
n = n_m[0]
m = n_m[1]

puzzles = gets.split.map(&:to_i).sort

min_difficulty = Float::INFINITY

(m - n).times do |i|
  difficulty = puzzles[i+n-1] - puzzles[i]
  min_difficulty = difficulty if difficulty < min_difficulty
end

puts min_difficulty

#!/usr/bin/ruby
n=gets.to_i
p gets.split.map(&:to_i).sort.each_cons(n).min{|e|e.last-e.first}

tmp = gets.chomp.split(" ")
a = gets.chomp.split(" ").map {|x| x.to_i}
a = a.sort
puts a[tmp[0].to_i-1]-a[0]
#puts array[0] + ", " + array[n]

inp = STDIN.gets <<-EOF
EOF

inp = inp.split("\n")

stu_num = inp[0].split(" ")[0].to_i
puz_num = inp[0].split(" ")[1].to_i

puz_difs = inp[1].split(" ").map(&:to_i).sort
min_dif_div = puz_difs.first(puz_difs.length - stu_num).inject([]){|divs,i| divs << (puz_difs[puz_difs.index(i) + stu_num - 1] - i)}.first
puts min_dif_div

inp = STDIN.gets <<-EOF
EOF

inp = inp.split("\n")

stu_num = inp[0].split(" ")[0].to_i
puz_num = inp[0].split(" ")[1].to_i
puz_difs = inp[1].split(" ").map(&:to_i).sort

min_dif_div = if stu_num < puz_num
                puz_difs.first(puz_difs.length - stu_num).inject([]){|divs,i| divs << (puz_difs[puz_difs.index(i) + stu_num - 1] - i)}.first
              else
                puz_difs.max - puz_difs.min
              end

puts min_dif_div

MAX_INT = 1<<31

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

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

    def complect_and_count
        self.dup.complect_and_count!
    end

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

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

# t_s = ff.collect do |key,value|
#   [key]*value
# end

# t = t_s.dup

# идея алгоритма: вокруг каждого элемента начинаю образовывать круг минимального радиуса
# когда будет построен круг содержащий более n элементов - начинаю смотреть какой из кругов лучший

# for each f_uniq we build an area
areas = []
for i in 0...f_uniq.count
    elem = f_uniq[i]
    areas[i] = {elements: [elem]*ff[elem], diameter: 0}
end

def area_found?(areas,n)
    a_with_max_count = areas.max_by {|e| e.count}
    a_with_max_count[:elements].count >= n
end

l = 1
until area_found?(areas,n)
    # add one elem to each area
    areas.each do |a|
        elements = a[:elements]

        next if elements.count >= l

        a_min = elements.min
        a_max = elements.max
        t_min, _ = f_uniq.partition {|el| el<a_min}
        t_max, _ = f_uniq.partition {|el| el>a_max}
        t_min = t_min.last
        t_max = t_max.first
        d1 = t_min.nil? ? MAX_INT : a_min - t_min
        d2 = t_max.nil? ? MAX_INT : t_max - a_max

        if d2 <= d1
            a[:elements] = elements + [t_max]*ff[t_max]
            a[:diameter] += d2
        elsif d1 < d2
            a[:elements] = ([t_min]*ff[t_min]) + elements
            a[:diameter] += d1
        end
    end
    l+=1
end

min = areas.min_by {|el| el[:diameter]}
puts min[:diameter]

    MAX_INT = 1<<31

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

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

        def complect_and_count
            self.dup.complect_and_count!
        end

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

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

    t_s = ff.collect do |key,value|
        [key]*value
    end

    t = t_s.dup

    diameters = Array.new(t_s.count, 0)

    loop do

        min_d = MAX_INT
        min_d_count = 0
        min_i = nil

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

        #p t[min_i]
        #puts "total count = #{t.count}, d = #{min_d}, count = #{min_d_count}"

        if t[min_i].count >= n
            p t[min_i].diameter
            break
        else
            tt = []
            for i in 0...t.count
                t_elem = t[i]
                ttt = f_uniq - t_elem
                t_elem_min = t_elem.min
                t_elem_max = t_elem.max

                tmp1,_ = ttt.partition {|el| el<t_elem_min}
                tmp1_max = tmp1.max
                count1 = tmp1_max.nil? ? 0 : ff[tmp1_max]
                d1 = tmp1_max.nil? ? MAX_INT : tmp1_max - t_elem_min

                tmp2,_ = ttt.partition {|el| el>t_elem_max}
                tmp2_min = tmp2.min
                count2 =tmp2_min.nil? ? 0 : ff[tmp2_min]
                d2 = tmp2_min.nil? ? MAX_INT : t_elem_max - tmp2_min

                to_add = nil
                if d2 < d1
                    tt << t_elem + ([tmp2_min]*count2)
                elsif d1 < d2
                    tt << ([tmp1_max]*count1) + t_elem
                elsif d1==d2 && d1!=MAX_INT
                    tt << ([tmp1_max]*count1) + t_elem
                    tt << t_elem + ([tmp2_min]*count2)
                end

                # if count1+count2>0
                #   tmp3 = ([tmp1_max]*count1) + t_elem + ([tmp2_min]*count2)
                #   tt << tmp3.compact
                # end
            end

            t = tt
        end

    end

    MAX_INT = 1<<31

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

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

        def complect_and_count
            self.dup.complect_and_count!
        end

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

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

    t_s = ff.collect do |key,value|
        [key]*value
    end

    t = t_s.dup

    diameters = Array.new(t_s.count, 0)

    loop do

        min_d = MAX_INT
        min_d_count = 0
        min_i = nil

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

        #p t[min_i]
        #puts "total count = #{t.count}, d = #{min_d}, count = #{min_d_count}"

        if t[min_i].count >= n
            p t[min_i].diameter
            break
        else
            tt = []
            for i in 0...t.count
                t_elem = t[i]
                ttt = f_uniq - t_elem
                t_elem_min = t_elem.min
                t_elem_max = t_elem.max

                tmp1,_ = ttt.partition {|el| el<t_elem_min}
                tmp1_max = tmp1.max

                count1 = 0
                unless tmp1_max.nil?
                    count1 = ff[tmp1_max]
                end

                tmp2,_ = ttt.partition {|el| el>t_elem_max}
                tmp2_min = tmp2.min
                count2 = 0
                unless tmp2_min.nil?
                    count2 = ff[tmp2_min]
                end

                # if count1>0
                #   tmp3 = ([tmp1_max]*count1) + t_elem
                #   tt << tmp3.compact
                # end

                # if count2>0
                #   tmp3 = t_elem + ([tmp2_min]*count2)
                #   tt << tmp3.compact
                # end

                if count1+count2>0
                    tmp3 = ([tmp1_max]*count1) + t_elem + ([tmp2_min]*count2)
                    tt << tmp3.compact
                end
            end

            t = tt
        end

    end

MAX_INT = 1<<31

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

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

	def complect_and_count
		self.dup.complect_and_count!
	end

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

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

# t_s = ff.collect do |key,value|
# 	[key]*value
# end

# t = t_s.dup

# идея алгоритма: вокруг каждого элемента начинаю образовывать круг минимального радиуса
# когда будет построен круг содержащий более n элементов - начинаю смотреть какой из кругов лучший

# for each f_uniq we build an area
areas = []
for i in 0...f_uniq.count
	elem = f_uniq[i]
	areas[i] = {elements: [elem]*ff[elem], diameter: 0}
end

def area_found?(areas,n)
	a_with_max_count = areas.max_by {|e| e.count}
	a_with_max_count[:elements].count >= n
end

until area_found?(areas,n)
	# add one elem to each area
	areas.each do |a|
		elements = a[:elements]
		a_min = elements.min
		a_max = elements.max
		t_min, _ = f_uniq.partition {|el| el<a_min}
		t_max, _ = f_uniq.partition {|el| el>a_max}
		t_min = t_min.last
		t_max = t_max.first
		d1 = t_min.nil? ? MAX_INT : a_min - t_min
		d2 = t_max.nil? ? MAX_INT : t_max - a_max

		if d2 <= d1
			a[:elements].push(t_max)
			a[:diameter] += d2
		elsif d1 < d2
			a[:elements].unshift(t_min)
			a[:diameter] += d1
		end
	end

end

min = areas.min_by {|el| el[:diameter]}
puts min[:diameter]

    MAX_INT = 1<<31

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

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

        def complect_and_count
            self.dup.complect_and_count!
        end

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

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

    t_s = ff.collect do |key,value|
        [key]*value
    end

    t = t_s.dup

    diameters = Array.new(t_s.count, 0)

    loop do

        min_d = MAX_INT
        min_d_count = 0
        min_i = nil

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

        #p t[min_i]
        #puts "total count = #{t.count}, d = #{min_d}, count = #{min_d_count}"

        if t[min_i].count >= n
            puts t[min_i].diameter
            break
        else
            tt = []
            for i in 0...t.count
                t_elem = t[i]
                ttt = f_uniq - t_elem
                t_elem_min = t_elem.min
                t_elem_max = t_elem.max

                tmp1,_ = ttt.partition {|el| el<t_elem_min}
                tmp1_max = tmp1.max

                count1 = 0
                unless tmp1_max.nil?
                    count1 = ff[tmp1_max]
                end

                tmp2,_ = ttt.partition {|el| el>t_elem_max}
                tmp2_min = tmp2.min
                count2 = 0
                unless tmp2_min.nil?
                    count2 = ff[tmp2_min]
                end

                # if count1>0
                #   tmp3 = ([tmp1_max]*count1) + t_elem
                #   tt << tmp3.compact
                # end

                # if count2>0
                #   tmp3 = t_elem + ([tmp2_min]*count2)
                #   tt << tmp3.compact
                # end

                if count1+count2>0
                    tmp3 = ([tmp1_max]*count1) + t_elem + ([tmp2_min]*count2)
                    tt << tmp3.compact
                end
            end

            t = tt
        end

    end

MAX_INT = 1<<31

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

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

    def complect_and_count
        self.dup.complect_and_count!
    end

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

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

# t_s = ff.collect do |key,value|
#   [key]*value
# end

# t = t_s.dup

# идея алгоритма: вокруг каждого элемента начинаю образовывать круг минимального радиуса
# когда будет построен круг содержащий более n элементов - начинаю смотреть какой из кругов лучший

# for each f_uniq we build an area
areas = []
for i in 0...f_uniq.count
    elem = f_uniq[i]
    areas[i] = {elements: [elem]*ff[elem], diameter: 0}
end

def area_found?(areas,n)
    a_with_max_count = areas.max_by {|e| e.count}
    a_with_max_count[:elements].count >= n
end

until area_found?(areas,n)
    # add one elem to each area
    areas.each do |a|
        elements = a[:elements]
        a_min = elements.min
        a_max = elements.max
        t_min, _ = f_uniq.partition {|el| el<a_min}
        t_max, _ = f_uniq.partition {|el| el>a_max}
        t_min = t_min.last
        t_max = t_max.first
        d1 = t_min.nil? ? MAX_INT : a_min - t_min
        d2 = t_max.nil? ? MAX_INT : t_max - a_max

        if d2 <= d1
            a[:elements] = elements + [t_max]*ff[t_max]
            a[:diameter] += d2
        elsif d1 < d2
            a[:elements] = ([t_min]*ff[t_min]) + elements
            a[:diameter] += d1
        end
    end

end

min = areas.min_by {|el| el[:diameter]}
puts min[:diameter]

MAX_INT = 1<<31

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

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

def solve(n,f)
	if n == 1
		f
	else
		md = MAX_INT
		mf = []
		for i in 0...f.count
			tf = f[0...i] + f[(i+1)..-1] # f \ f[i]
			td = tf.diameter
			if td < md
				ff = solve(n-1, tf)
				md = td
				mf = ff + [f[i]]
			end
		end
		mf
	end
end

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

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

f.sort!
t = f[0...n]
puts t.max - t.min

    MAX_INT = 1<<31

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

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

        def complect_and_count
            self.dup.complect_and_count!
        end

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

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

    t_s = ff.collect do |key,value|
        [key]*value
    end

    t = t_s.dup

    diameters = Array.new(t_s.count, 0)

    loop do

        min_d = MAX_INT
        min_d_count = 0
        min_i = nil

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

        p t[min_i]
        puts "total count = #{t.count}, d = #{min_d}, count = #{min_d_count}"

        if t[min_i].count >= n
            p t[min_i].diameter
            break
        else
            tt = []
            for i in 0...t.count
                t_elem = t[i]
                ttt = f_uniq - t_elem
                t_elem_min = t_elem.min
                t_elem_max = t_elem.max

                tmp1,_ = ttt.partition {|el| el<t_elem_min}
                tmp1_max = tmp1.max

                count1 = 0
                unless tmp1_max.nil?
                    count1 = ff[tmp1_max]
                end

                tmp2,_ = ttt.partition {|el| el>t_elem_max}
                tmp2_min = tmp2.min
                count2 = 0
                unless tmp2_min.nil?
                    count2 = ff[tmp2_min]
                end

                # if count1>0
                #   tmp3 = ([tmp1_max]*count1) + t_elem
                #   tt << tmp3.compact
                # end

                # if count2>0
                #   tmp3 = t_elem + ([tmp2_min]*count2)
                #   tt << tmp3.compact
                # end

                if count1+count2>0
                    tmp3 = ([tmp1_max]*count1) + t_elem + ([tmp2_min]*count2)
                    tt << tmp3.compact
                end
            end

            t = tt
        end

    end

n, m = gets.split.map &:to_i
arr = gets.split.map &:to_i
if arr.size - arr.uniq.size == n - 1
  puts "0"
else
  arr = arr.sort
  puts arr[n - 1] - arr.first
end

n, m = gets.split.map &:to_i
arr = gets.split.map &:to_i
arr = arr.sort
puts arr[n - 1] - arr[0]

n, m = gets.split.map &:to_i
arr = gets.split.map &:to_i
arr = arr.sort
if arr.uniq.size == arr.size
  puts arr[n - 1] - arr[0]
else
  puts 0
end

students = gets.chomp.split(/ /).map(&:to_i)[0]
pieces = gets.chomp.split(/ /).map(&:to_i)
pieces.sort!
puts pieces[students - 1] - pieces[0]

#!/usr/bin/env ruby

# Solves http://codeforces.com/problemset/problem/337/A

n, m = $stdin.gets.split.map { |s| s.to_i }
nums = $stdin.gets.split.map { |s| s.to_i }
nums.sort!
diffs = (0..(m-n)).map { |i| nums[n + i - 1] - nums[0] }
puts diffs.sort![0]

n,m = gets.split(" ").map(&:to_i)
a = gets.split(" ").map(&:to_i).sort
min = 99999
for i in 0..(m-n-1)
    if(min>a[i+n-1]-a[i])
        min = a[i+n-1]-a[i]
    end
end
print(min)

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

min = 2000
while a.size >= m
  min = [min, a[n-1]-a.first].min
  a.shift
end

puts min


# your code goes here

$store = {}

def trim_trailing_zeroes(y)
	y_string = y.to_s
	i = y_string.length - 1
	index_non_zero = 0
	while i >= 1 do
		if y_string[i] != "0"
			index_non_zero = i
			break
		end
		i -= 1
	end

	return y_string[0..index_non_zero]
end

def cal(x)
	if !$store[x].nil?
		return
	end
	$store[x] = 1
	execute_for = trim_trailing_zeroes(x + 1)
	cal(execute_for.to_i)
end

value = gets.to_i
cal(value)

p $store.keys.size

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

require "set"

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

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

N = gets.to_i
n = N
checked = {}

while n > 9
  checked[n] = true
  n += 1
  n = n.to_s.reverse.to_i.to_s.reverse.to_i
end

puts checked.keys.count + 9

require 'set'

n = gets.strip.to_i
seen = Set.new

while !seen.include?(n)
  seen.add(n)
  begin
    n += 1
    seen.add(n) if !seen.include?(n) && n % 10 != 0
  end while n % 10 != 0
  while n % 10 == 0
    n /= 10
  end
end

puts seen.size

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

n = gets.to_i
ans = 1
while n > 1
    n += 1
    n /= 10 while n%10 == 0
    ans += 1
end
puts ans

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


x = gets.chomp.to_i
sum = (0)
for i in 1 .. x
  sum = sum + 2**i
end

print sum

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

n = gets.to_i

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

puts s

#!/usr/bin/ruby

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

res = 0

(1..N).each do |x|
	res += 2 ** x
end

puts res

n = gets.strip.to_i
puts (1..n).inject(0){|r, el| r + 2**el}

n = gets.to_i

k = 2
s = 2
(n-1).times do
   s += 6
   k *= 2
end

puts n == 1 ? 2 : s

n = gets.to_i

k = 2
s = 0
(n-1).times do
   s += k*3
   k *= 2
end

puts n == 1 ? 2 : s


n=gets.to_i
res=n
for i in 1..n-1
    res+=i*(n-i)
end
puts res

x=gets.chomp.to_i
y=1
z=x
ans=0
while y!=x do
  ans=ans+((z-1)*y)
  y=y+1
  z=z-1
end
puts ans+x

n = Integer(gets)
a = 0
for i in 1..n
	a = a + n*i - i * i + 1
end
puts a

a = c = gets.chomp.to_i

b = 0; sum = 0

while a > 1 do
    a -= 1
    b += 1
    sum += b * a
end

puts sum + c

number = gets.to_i
count = 0

(number-1).times do |i|
  k = i+1
  count += (number-k)*k
end

p count + number

n = gets.chomp.to_i
s = 0
(1..n).each{ |k|
  s += (k*(n-k)+1)
}
puts s

n = gets.to_i
i = 1
ans = 0
for i in 1..n do
    ans = ans + (i * (n-i))
end
puts ans+n

n = gets.to_i
ans = n
i = 1
while n-i > 0 do
  ans += (n-i)*i
  i+=1
end
puts ans

n = gets.to_i
need = [0]
n.times do |i|
    need << need[-1] + (n - i - 1) * (i + 1) + 1
end
puts need[n]

#!/usr/bin/env ruby

n = gets.to_i
puts n + (n - 1) * n * (n + 1) / 6;

$i=0;
$sum=0;
gets.chomp.to_i.-(1).times do
    $i+=1
    $sum+=$i*($_.to_i-$i)
end
puts $sum+$_.to_i

n = gets.to_i

result = 0
t = []

1.upto(n)do |i|
	result += (n-i)*i
end
puts result + n

n = gets.to_i
ans = n
n.times do |i|
	ans += (i + 1) * (n - i - 1)
end
puts ans

n = gets.to_i
s = 0
n.times do |time|
  s += (n - time - 1) * (time + 1) + 1
end
puts s

def func(x, h)
    y = x+(x-1)*h
    return y
end

n = gets.to_i

result = 0

i = n
while i > 0 do
    result += func(i, n-i)
    i -= 1
end

puts result

val = gets.to_i ;
val = (val**3 + 5*val)/6 ;
puts val ;

#!/usr/bin/ruby
n=gets.to_i
p (n*n+5)*n/6

sum, multi = gets.chomp.to_i, 1
(sum-1).downto 1 do |i|
	sum += (i*multi)
	multi += 1
end
puts sum

n = gets.to_i
k = n
sum = 0
while k > 0 do
  sum += (n-k + 1) * (k - 1) + 1
  k -= 1
end
puts sum

n = gets.to_i
sum = n
for k in 1..n do
  sum += (n-k + 1) * (k - 1)
end
puts sum

#!/usr/bin/ruby1.9

n = STDIN.readline.to_i

prefix = 0
total = 0
(1..n).reverse_each do |k|
	total += (prefix+1)*(k-1) + 1
	prefix += 1
end

puts total

n = gets.to_i
i = 1
r = 0
while i<=n-1 do
	r = r + (i * (n-i))
	i=i+1
end
puts r+n

x = gets.chomp.to_i

total = x

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

puts total

n = $stdin.readline.to_i

def s(n)
    n*(n-1)/2
end

def k(n,l)
    if n == 1
        return 1+l
    else
        if l > 0
            return n*l + k(n-1, l+1)
        else
            return n-1 + k(n-1, l+1)
        end
    end
end

puts k(n,0)

n = gets.to_i
ar = (1...n).to_a
puts n + ar.reverse.zip(ar).map{|x,y| x*y}.inject(:+) if n>1
puts 1 if n==1

def run
  n = ARGF.readline.to_i

  sum = 0
  n.times do |i|
    sum += (n - i - 1) * (i + 1) + 1
  end

  puts sum
end

run if $0 == __FILE__

n=gets.to_i
i=0
ans=n+n-1
while n>1
	n=n-1
	ans=ans+n*i+i
	i=i+1
end
puts ans

n = gets.to_i
sum = 0
n.times do |j|
  sum += n - j + j * (n - j - 1)
end
puts sum

n = gets.to_i
r = 0
n.downto(1) do |i|
	r += i + (i - 1)*(n - i)
end
puts r

n=gets.to_i
count=0
2.upto(n){|i| count+=(i-1)*(n-i)+(i-1)}
puts(count+n)

require 'set'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

n = gets.to_i

if n == 1 then
  print 1
  exit
end

result = n + 1
2.upto(n - 1) do |value|
  result += 1
  result += value * (n - value)
end

print result

def func(x)
    y = x+(x-1)
    return y
end

n = gets.to_i

result = n
i = n-1
while i > 0 do
    result += func(i)
    i -= 1
end

puts result

def Click(n)
  return 1 if n == 1
  return 3 if n == 2
  return n - 1 + 2 * Click(n - 1) - 1
end

puts Click(gets.to_i)

n = $stdin.readline.to_i

def k(n)
    if n == 1
        return 1
    else
        return 2*(n-1) + k(n-1)
    end
end

puts k(n)

require 'set'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

n = gets.to_i

result = n + 1
2.upto(n - 1) do |value|
  result += 1
  result += value * (n - value)
end

print result

require 'set'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

n = gets.to_i
if n == 1 then
  print 1
else
  result = 0
  n.times do |p|
    result+=2**p
  end

  print result
end

n = gets.to_i

result = 0
t = []

if n > 3
	n.downto(1) do |i|
		if i > 1
			n.downto(i+1) do |j|
				t[j] += i
			end
		end
		t[i] = i

	end
	t.each{|x| result += x == nil ? 0 : x}
	puts result
elsif n == 3
	print 7
else
	print 3
end

def sum(n)
	n + [0, n*n/2 - n].max
end

n = gets.to_i

result = 0

n.times do |i|
	result += sum(i+1)
end

puts result


n,m,a,b=gets.split.map &:to_i
p [n%m*b,(m-n%m)*a].min

n, m, a, b = gets.strip.split.map(&:to_i)
td = n % m
tb = m - td
puts [td*b, tb*a].min

n, m, a, b = gets.split.map{|e| e.to_i }
mod = n % m
puts [mod*b, ((m-mod)%m)*a].min

n,m,a,b=gets.split.map &:to_i
x=n%m
puts [x*b, (m-x)*a].min

n, m, a, b = gets.split.map &:to_i
x = [n / m - 1, 0].max
ans = Float::INFINITY
x.upto(x + 2) do |i|
  ans = [ans, i * m < n ? (n - i * m) * b : (i * m - n) * a].min
end
puts ans

n, m, a, b = gets.split.map(&:to_i)
d = n % m
if d == 0
    puts 0
    exit
end
puts [b * d, a * (m - d)].min

def delegate_boxes
  n, m, a, b = gets.split.map(&:to_i)
  l = n % m
  return 0 if l == 0

  if n < m
    return min(n * b, (m - n) * a)
  end

  min(l * b, (m - l) * a)
end

def min(a, b)
  a <= b ? a : b
end

puts delegate_boxes

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

div, mod = n.divmod(m)

puts [mod * b, (m - mod) * a].min


#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

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

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

p arr_used.uniq.count

#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.reverse
h = {}
ans = 0
t.each do |d|
   ((t.min)..100).each do |i|
    if(d%i == 0)
      h[i] = true
      break
    end
  end
end

p ((h.to_a.size))

#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.reverse
h = {}
ans = 0
t.each do |d|
   (2..100).each do |i|
    if(d%i == 0)
      h[i] = true
      break
    end
  end
end
p h.to_a.size

#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.reverse
h = {}
ans = 0
t.size.times do |i|
  f = false
  next if(h[t[i]])
  ((i+1)...(t.size)).each do |j|
    if(t[i]%t[j]==0)
      h[t[j]] = true
    end
  end
  ans += 1
end
p ans

#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.size.times do |i|
  f = false
  ((i+1)...(t.size)).each do |j|
    if(t[j]%t[i]==0)
      f = true
      break
    end
  end
  ans += 1 unless f
end
p ans


n, m = gets.split.map(&:to_i)
b = {}
m.times do
	x, y = gets.split.map(&:to_i)
	b.has_key?(y) ? b[y] += x : b[y] = x
end
tot, cc = 0, 0
b.sort.reverse.each do |v, i|
	tot += [n - cc, i].min * v
	cc += i
	break if cc > n
end
puts tot

n,m=gets().chomp().split(' ').map(&:to_i) ;h={}
h = m.times.to_a.map{ gets.split.map(&:to_i) }
(h=h.sort_by {|_key, value| value}).reverse! ;count=0
h.each do |key, value|
  count+=(key>n)?((n)*value):(key*value) ;n-=key ;break if (n<=0)
end
p count

n,m=gets.split.map(&:to_i)
s=0
m.times{$*<<gets.split.map(&:to_i)}
$*.sort{|c,d|d[1]<=>c[1]}.each{|a,b|if n>a
s+=a*b
n-=a
else
s+=n*b
break
end}
p s

tab=[]; result=0
n, m = gets.split.map(&:to_i)
m.times{tab << gets.split.map(&:to_i)}
tab.sort!{|a, b| a[1] <=> b[1]}.reverse!
tab.each do |x|
	result += [x[0], n].min * x[1]
	n -= [x[0], n].min
end

puts result

class Box
	attr_accessor :count, :value

	def initialize(count, value)
		@count, @value = count, value
	end
end

t = gets.split.collect{|x| x.to_i}
n,m = t[0], t[1]

a = []

m.times do |i|
	t = gets.split.collect{|x| x.to_i}
	a << Box.new(t[0], t[1])
end

a.sort!{|x,y| y.value <=> x.value}

result = 0
a.each do |b|
	if b.count <= n
		result += b.count * b.value
		n-=b.count
	else
		result += n * b.value
		n = 0
	end
end

puts result

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

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

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

puts ret

s = 0
n, m = gets.split.map{|i| i.to_i}
m.times.to_a.map{gets.split.map{|i| i.to_i}.reverse}.sort.reverse.each do |i|
	t = [n, i[1]].min
	n -= t
	s += i[0] * t
end
p s

s = 0
n, m = gets.split.map{|i| i.to_i}
m.times{$*<<gets.split.map(&:to_i)}
$*.sort{|a, b| b[1] <=> a[1]}.each do |a, b|
	t = [n, a].min
	n -= t
	s += b * t
end
p s

__END__
# 	When 	Who 	Problem 	Lang 	Verdict 	Time 	Memory
212857 	Dec 9, 2010 9:59:45 PM 	watashi 	B - Burglar and Matches 	Ruby 	Accepted 	80 ms 	2992 KB

n,m=gets.split.map(&:to_i)
cnt=Array.new(11,0)
m.times{
        a,b=gets.split.map(&:to_i)
        cnt[b]+=a
}
ans=0
10.downto(1){|i|
        if(n>=cnt[i]) then
                n-=cnt[i]
                ans+=cnt[i]*i
        else
                ans+=n*i
                n=0
        end
}
p ans

#!/usr/bin/ruby

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

matches = []
m.times do
  a, b = gets.chomp.split.map{|e| e.to_i}
  matches.push [a, b]
end

matches.sort! {|l, r|
  r[1] <=> l[1]
}

rest = n
cur_box = 0
acc = 0
while (true)
  break if cur_box >= m
  if (matches[cur_box][0] <= rest)
    acc += matches[cur_box][0] * matches[cur_box][1]
    rest -= matches[cur_box][0]
    cur_box += 1
    next
  else
    acc += matches[cur_box][1] * rest
    break
  end
end
puts acc

result = 0
n, m = gets.split.map(&:to_i)
elements = m.times.to_a.map{ gets.split.map(&:to_i).reverse }
elements.sort.reverse.each{ |x, y| t = [n, y].min; n -= t; result += x * t; break if n == 0 }
puts result

result = 0
n, m = gets.split.map(&:to_i)
elements = m.times.to_a.map{ gets.split.map(&:to_i).reverse }
elements.sort.reverse.each{ |x, y| puts x; puts y; t = [n, y].min; puts t; n -= t; puts n; result += x * t; break if n == 0 }
puts result

n,m=gets().chomp().split(' ').map(&:to_i) ;h=Hash.new
m.times { a,b=gets().chomp().split(' ').map(&:to_i); h[a]=b; }
(h=h.sort_by {|_key, value| value}).reverse! ;count=0
h.each do |key, value|
  count+=(key>n)?((n)*value):(key*value) ;n-=key ;break if (n<=0)
end
p count


t,s,x = gets.chomp.split

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

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

I =->{gets.split.map &:to_i}
t, s, x = I[]
if x == t || x >= s + t && ((x - t) % s == 0 || ((x - t - 1)) % s == 0)
  print 'YES'
else
  print 'NO'
end

t,s,x = gets.split(" ").map(&:to_i)
x -= t
puts ((x>=0 and x%s < 2 and x!=1) ? "YES" :"NO")

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

#!/usr/bin/env ruby

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

if t == x
    puts "YES"
    exit
end
if x == t + 1 or x < t
    puts "NO"
    exit
end

if (x - t).modulo(s) == 0 or (x - t - 1).modulo(s) == 0
    puts "YES"
else
    puts "NO"
end

t,s,x = gets.split(' ').map(&:to_i)
a = x - t
b = x - t - 1
if a%s == 0 && (a >= 0)
  puts 'YES'
elsif b%s == 0 && (b > 0)
  puts 'YES'
else
  puts 'NO'
end

input1 = gets.chomp
num = input1.split(" ").map(&:to_i)
if num[2]>=num[0] && num[2]!= (num[0]+1) && ((num[2]-num[0])%num[1] == 0 || (num[2] - num[0] - 1)%num[1] == 0)
  print "YES"
else
  print "NO"
end

t, s, x = gets.split.map { |x| x.to_i }

if x >= t and (x - t) % s == 0
	puts 'YES'
elsif x >= t + s + 1 and (x - t - 1) % s == 0
	puts 'YES'
else
	puts 'NO'
end

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

n = (x - t) / s
num = t + n * s

first = true if (n == 0)
oneTime = (n > 0)

#puts "n : #{n}"

if n < 0
	puts 'NO'
	exit
end

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

	oneTime = !oneTime
	first = false

	#puts "Num : #{num}"
end

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

t,s,x = gets.chomp.split

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

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

t,s,x = gets.split(" ").map(&:to_i)
x -= t
puts (x>=0 and x%s < 2 and x!=1 ? "YES" :"NO")

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

puts (x-t) % s < 2 && t+1 != x ? 'YES' : 'NO'

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

puts (x-t) % s < 2 ? 'YES' : 'NO'

#!/usr/bin/env ruby

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

if t == x
    puts "YES"
    exit
end
if x == t + 1
    puts "NO"
    exit
end

if ((r1 = (x - t).modulo(s)) == 0 and r1 >= t) or ((r2 = (x - t - 1).modulo(s)) == 0 and r2 >= t)
    puts "YES"
else
    puts "NO"
end

#!/usr/bin/env ruby

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

if t == x
    puts "YES"
    exit
end
if x == t + 1
    puts "NO"
    exit
end

if (x - t).modulo(s) == 0 or (x - t - 1).modulo(s) == 0
    puts "YES"
else
    puts "NO"
end

t,s,x = gets.split(' ').map(&:to_i)
a = x - t
b = x - t - 1
if a%s == 0
  puts 'YES'
elsif b%s == 0 && (b%s > 0)
  puts 'YES'
else
  puts 'NO'
end

t,s,x = gets.split(' ').map(&:to_i)
a = x - t
b = x - t - 1
if a%s == 0
  puts 'YES'
elsif b%s == 0 && (b > 0)
  puts 'YES'
else
  puts 'NO'
end

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

n = (x - t) / s
num = t + n * s

first = true if (n == 0)
oneTime = (n > 0)

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

	oneTime = !oneTime
	first = false

	#puts "Num : #{num}"
end

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


a, b = gets.split(' ').map &:to_i
total = 0
tmp = 0
while a > 0
  total += 1
  tmp += 1
  a -= 1
  if tmp == b
    tmp = 0
    a += 1
  end
end
puts total

a, b = gets.split.map &:to_i
total = a
while a / b > 0
  total += a / b
  if a % b == 0
    a = a / b
  else
    a = a / b + a % b
  end
end
puts total

ARGF.lines do |line|
    n, m = line.split.map(&:to_i)
    sol = n+(n-1)/(m-1)
    puts sol
end

n,m=gets.split.map{|e| e.to_i}
c=1
1.upto(10000000){|i|
	if i%m==0 then
		c=c+1
	end
	if n<=i then
		c=c-1
		if c<=0 then
			puts i
			break
		end
	end
}

n, m = gets.split.map(&:to_i)
i = 0
ans = 0
while n > 0
	i += 1
	if i % m == 0
		ans += 1
		next end
	n -= 1
	ans += 1 end
puts ans

input=gets.split(" ")
n=input[0].to_i
m=input[1].to_i
k=n
days=0
while k>0
  days+=1
  k=k-1
  if days % m == 0
    k=k+1
  end
end
puts days

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

div = n/m
days = n + div

while((div+=1) * m <= days)
    days += 1
end

puts days

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

i = 0
while n > 0
    i += 1
    n -= 1 if i % m != 0
end

p i

n, m = gets.split.map(&:to_i)
puts n + (n - 1) / (m - 1)

a,b = gets.split.map(&:to_i)
sum = a
while a>=b
    tmp = a
    a = tmp/b
    sum+=a
    a += tmp%b
end
p sum

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

i = 0
loop do
  i += 1
  n -= 1
  n += 1 if i % m == 0
  break if n == 0
end

puts i

n, m = gets.split(/\s+/).map(&:to_i)
a = 0

while n > 0
  a += 1
  n -= 1
  if a%m == 0
    n+=1
  end
end

p a

n,m = gets.split.map(&:to_i)
day = 0
while n > 0
  day += 1
  n -= 1 if day % m != 0
end
puts day

s = gets.split(' ')	#Танцы с бубном

n = s[0].to_i
m = s[1].to_i

i = 0
res = 0
until n == 0
	res += 1
	n -= 1
	i += 1
	if i == m
		n += 1
		i = 0
	end
end

puts res

n, m = *(gets.split(' ').collect { |n| n.to_i })
day = 0
until n == 0 do
  day += 1
  n -= 1 unless day % m == 0
end
puts day

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

x=n
while (x<n+x/m) do
    x+=1
end

puts x

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

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

day = 0
while n > 0
  day += 1
  n -= 1
  n += 1 if day % m == 0
end

puts day

input = gets.split.map(&:to_i)
n = input[0]
m = input[1]
days = 0
while n != 0
  days += 1
  if days % m == 0
    n += 1
  end
  n -= 1
end
puts days

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

(1..100000).each do |i|
  #puts "#{i}, #{a}"
  if a == 0
    puts i-1
    break
  end

  a -= 1

  if i % b == 0
    a += 1
  end

end

n, m = gets.chomp.split(' ').map { |i| i.to_i }
ans = 1
until n == 0 do
    n -= 1
    n += 1 if ans % m == 0
    ans += 1
end

puts ans - 1

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

day=0

while n>0
    day+=1
    n-=1
    if day%m==0
        n+=1
    end
end

p day

#!/usr/bin/env ruby

#n = ARGV[0].to_i
#m = ARGV[1].to_i

inp = $stdin.gets.chomp.split
n = inp[0].to_i
m = inp[1].to_i

day = 0

while n > 0
  day += 1
  n -= 1 unless day%m==0
end

puts day

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

res = 0
day = 0

while n > 0
    res += 1
    n -= 1
    day += 1
    if day >= m
        n += 1
        day = 0
    end
end

p res

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

k = n
i = 1
while i<=n
  if i%m == 0
      n+=1
  end
  i+=1
end
puts n

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

n = 0
r = 0
while a>0
    n+= a
    t = r+a
    a = t/b
    r = t%b
end

puts n

a, b = gets.split.map(&:to_i); p (a*b-1)/(b-1)

n, m = gets.split.map(&:to_i)
c = n
c += n = n / m while n >= m
puts c

a,b = gets.split.map(&:to_i)
sum = a
while a!=0
    a = a/b
    sum+=a
end
p sum

n, m = gets.split(/\s+/).map(&:to_i)
a = n

while n >= m
  a += n/m
  n /= m
end

p a

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

x=(m*n)/(m-1)
if (x==n+x/m)
    puts x
else
    puts x-1
end

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

result = n
while n > 0
  n /= m
  result += n
end

puts result

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

result = n
while n > 0
  result += n % m
  n /= m
  result += n
end

puts result - 1

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

(1..100000).each do |i|
  #puts "day #{i} socks #{a}"
  if a == 0
    puts i
    break
  end
  a -= 1
  if (i-1) % b == 0
    a += 1
  end

end

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

(1..100000).each do |i|
  if a == 0
    puts i
    break
  end

  a -= 1

  if (i-1)%b == 0 && i != 1
    a += 1
  end

end

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

if a == 1

  puts "1"

else

  a -= 1
  (1..100000).each do |i|
    #puts "day #{i} socks #{a}"
    if a == 0
      puts i
      break
    end
    a -= 1
    if (i-1) % b == 0
      a += 1
    end

  end

end

#!/usr/bin/env ruby

n = ARGV[0].to_i
m = ARGV[1].to_i

day = 0

while n > 0
  day += 1
  n -= 1 unless day%m==0
end

puts day

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

for i in 1..n
  if i%m == 0
      n+=1
  end
end
puts n+1

a, b = gets.split.map &:to_i
if a < b
  puts a
  exit
end
total = a
while a != 1
  total += a % b == 0 ? a / b : a / b + 1
  a /= b
end
puts total

a, b = gets.split.map &:to_i
total = a
while a / b > 0
  total += a / b
  if a % b == 0
    a = a / b
  else
    a = a / b + 1
  end
end
puts total

a, b = gets.split.map &:to_i
puts a + (a + a / b) / b


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

require 'prime'

n, k = gets.chomp.split.map(&:to_i)
a = n.prime_division
sum = (a.map do | x | x[1] end).inject(0, :+)

if sum < k
  puts -1
else
  for x in a
    for i in 1..x[1]
      if k == 1
        break
      end
      print x[0], ' '
      k -= 1
      n /= x[0]
    end
  end
  puts n
end

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

n,k = ascan

require 'prime'

aa = Prime.prime_division(n).map{|e,c| [e]*c}.inject(:+)

if aa.size < k
    p -1
elsif k==1
    p n
else
    r = aa.shift(k-1)
    r << aa.inject(:*)
    puts r*" "
end

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

data = []
for i in (2..n)
    while n % i == 0
        data.push(i)
        n /= i
    end
end

if data.length < k
    puts -1
else
    data[(k-1..-1)] = data[(k-1..-1)].reduce(:*)
    puts data.map(&:to_s).join(' ')
end

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

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

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

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

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

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


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

n = gets.to_i
t = 1
while n%t==0
    t *= 3
end
ans = n/t+1
puts ans

n = gets.chomp.to_i

k = 1

while (n % 3**k == 0)
    k+=1
end

puts n / 3 ** k + 1

n=gets.to_i
p=3
while p < n
  break if n%p>0
  p*=3
end
if n%p>0
  p n/p+1
else
  p 1
end

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

a = gets.to_i.to_s(3).split('')
a.pop while a.last == '0'
a.pop
puts a.join.to_i(3) + 1

a = gets.to_i
a /= 3 while a%3 == 0
puts a/3+1

n = gets.to_i
t = 1
while n%t==0
    t *= 3
end
ans = n/t+n%t
puts ans

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


#bismillahir rahmanir rahim

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

k = 240-k
c = 0

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

c=n if c>n

puts c

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

nk = gets.chomp
nk = nk.split
problems = nk[0].to_i
minutes = nk[1].to_i

solved = 0
for i in (1..problems)
	if minutes + (i * 5) <= 240
		minutes += (i*5)
		solved += 1
	else
		break
	end
end

print solved

n, k = gets.split.map(&:to_i)
c = 0
t = 60 * 4 - k
while t >= (c + 1) * 5 && c < n
  c += 1
  t -= c * 5
end
puts c

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

result = n

1.upto(n) do |x|
  if (1..x).inject(&:+)*5 > k
    result = x-1
    break
  end
end

puts result

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.chomp.split.map(&:to_i)
t = 240 - k
idx = 0
sum = 0
(1..n).each do |i|
  sum += i * 5
  if sum <= t
    idx = i
  else
    break
  end
end
puts idx

n,k = gets.split().map(&:to_i)
i = 0
t = 0
until t > 240-k
	i += 1
	t += i*5
end

puts [i-1,n].min

n,k = gets.split.map &:to_i
available_time = 240-k
counter = 0
problem = 1
n.times do
  time_taken = problem * 5
  available_time-= time_taken
  if available_time >=0 then
    problem+=1
    counter +=1
  else
    break
  end
end
puts counter

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

rem = 240 - k

ans = 0
(1..n).each do |i|
  rem -= i * 5
  if rem < 0
    break
  end
  ans += 1
end
puts ans

total_time = 4 * 60
n, k = gets.strip.split.map(&:to_i)
total_time -= k
time = 0
ans = 0
(1..n).each do |i|
    curr_time = 5 * i
    break if time + curr_time > total_time
    ans += 1
    time += curr_time
end
puts ans

n,m=gets.split.map{|e| e.to_i}
m=240-m
ans=0
sum=0
n.times{|i|
	sum+=((i+1)*5)
	break if sum>m
	ans+=1
}
puts ans

n, k = gets.split.map(&:to_i)
k = 240 - k
1.upto(n) do |i|
	k -= i * 5
	if k < 0
		puts i - 1
		break
	end
end
if k >= 0
	puts n
end

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

rem = 240 - k

ans = 0
(1..n).each do |i|
  if rem <= 0
    break
  end
  rem -= i * 5
  ans += 1
end
puts ans

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

rem = 240 - k

ans = 0
(1..n).each do |i|
  rem -= i * 5
  if rem <= 0
    break
  end
  ans += 1
end
puts ans

total_time = 4 * 60
n, k = gets.strip.split.map(&:to_i)
total_time -= k
time = 5
ans = 0
(1..n).each do |i|
    curr_time = 5 * i
    ans += 1
    break if time + curr_time > total_time
    time += curr_time
end
puts ans

n, k = gets.split.map(&:to_i)
c = 0
t = 60 * 4 - k
while t > (c + 1) * 5 && c < n
  c += 1
  t -= c * 5
end
puts c


l,r=gets.split.map &:to_i
a=l+l%2
puts a+2<=r ?[a,a+1,a+2].join(" ") :-1

l,r=gets.chomp.split.map(&:to_i)
if l==r
	puts "-1"
	exit
end
while l<r

	if l%2!=0
		l+=1
	end
	if l+2>r
		puts "-1"
		break
	else
		puts l.to_s+' '+(l+1).to_s+' '+(l+2).to_s
		break
	end
end

l, r = gets.chomp.split(/ /).map(&:to_i)
if (l % 2 != 0)
    l += 1
end
if (l + 2 > r)
    puts "-1"
else
    puts "#{l} #{l + 1} #{l + 2}"
end

def test(l,r)
l.upto(r-2){|a|
	(a+1).upto(r-1){|b|
		next if a.gcd(b)!=1
		(b+1).upto(r){|c|
			next if b.gcd(c)!=1
			next if a.gcd(c)==1
			return [a,b,c]
		}
	}
}
return [-1]
end
l,r=gets.split.map{|e| e.to_i}
puts test(l,r)*" "

# coding: utf-8

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

def euclidean(a, b)
    while b > 0
        c = a % b
        a = b
        b = c
    end

    a
end
# counter example
# if (a,b) is coprime and (b, c) is coprime,  then (a,c) is also coprime
# if possible, print a b c
# if not possible, print -1
# l과 r사이 간격은 최대 50
if r - l < 2
    puts "-1"
else
    # 두 수의 관계이므로 유클리드 호제법을 이용한다
    # 나눠 떨어지는 시점이 최대공약수(GCD)인데 1이 나오면 서로소이다.
    printed = false

    catch :found do
        for a in l...r
            for b in (a+1)...r
                for c in (b+1)..r
                    if euclidean(a, b) == 1 && euclidean(b, c) == 1 && euclidean(a, c) != 1
                        puts "#{a} #{b} #{c}"
                        printed = true
                        throw :found
                    end
                end
            end
        end
    end

    if !printed
        puts "-1"
    end
end

def isCoprime(a,b)
  if b != 0
    return isCoprime(b,a%b)
  end
  return (a == 1)
end

def counter(l, r)
  for a in (l..r+1)
    for b in (a+1)..(r)
      for c in (b+1)..(r)
        if isCoprime(a,b)
          if isCoprime(b,c)
            unless isCoprime(a,c)
               return "#{a} #{b} #{c}"
            end
          end
        end
      end
    end
  end
  -1
end

l, r = gets.chomp.split
l = l.to_i
r = r.to_i

print counter(l, r)

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

coprime = false
req_a = 0
req_b = 0
req_c = 0
l,h = gets.split(" ").map(&:to_i)
(l..h).each do |a|
  (a+1..h).each do |b|
    (b+1..h).each do |c|
      # puts "#{a} #{b} #{c}"
      # puts "#{gcd(b,a)} #{gcd(c,b)} #{gcd(c,a)}"
      if gcd(a,b) == 1 &&  gcd(b,c) == 1 && gcd(a,c) != 1
        req_a = a
        req_b = b
        req_c = c
        coprime = true
        break
      end

    end
    break if coprime
  end
  break if coprime
end

puts coprime ? "#{req_a} #{req_b} #{req_c}" :-1

=begin
data = gets.chomp.split(/\s/).map {|n| n.to_i}
#p data
puts data.inject(:+) / data.length
=end

def nod(x,y)
  if x == y
    return x
  end
  if x == 0
    return y
  end
  if y == 0
    return x
  end
  if x > y
    return nod(y, x % y)
  else
    return nod(x, y % x)
  end
end

data = gets.chomp.split(/\s/).map{|n| n.to_i}
l = data[0]
r = data[1]

a = l
b = a + 1
c = b + 1
res = -1

while a < r - 1
  b = a + 1
  while b < r
    c = b + 1
    while c <= r
      if nod(a, b) == 1 && nod(b,c) == 1 && nod(a,c) > 1
        puts "#{a} #{b} #{c}"
        exit
      end
      c += 1
    end
    b += 1
  end
  a += 1
end

puts "#{-1}" if res <= 0

l, r = gets.split.map {|i| i.to_i}
if r - l == 2 && l % 2 == 1 || r - l < 2
  puts -1
else
  if l % 2 == 1
    l += 1
  end
  puts l.to_s + ' ' + (l + 1).to_s + ' ' + (l + 2).to_s
end

l,r=gets.chomp.split.map(&:to_i)
while l<r
	if l%2!=0
		l+=1
	end
	if l+2>r
		puts "-1"
		break
	else
		puts l.to_s+' '+(l+1).to_s+' '+(l+2).to_s
		break
	end
end

# coding: utf-8

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

def euclidean(a, b)
    while b > 0
        c = a % b
        a = b
        b = c
    end

    a
end
# counter example
# if (a,b) is coprime and (b, c) is coprime,  then (a,c) is also coprime
# if possible, print a b c
# if not possible, print -1
# l과 r사이 간격은 최대 50
if r - l < 2
    puts "-1"
else
    # 두 수의 관계이므로 유클리드 호제법을 이용한다
    # 나눠 떨어지는 시점이 최대공약수(GCD)인데 1이 나오면 서로소이다.
    printed = false

    for i in (l+1)...r
        if euclidean(l, i) == 1 && euclidean(i, r) == 1 && euclidean(l, r) != 1
            puts "#{l} #{i} #{r}"
            printed = true
            break
        end
    end

    if !printed
        puts "-1"
    end
end

# coding: utf-8

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

def euclidean(a, b)
    while b > 0
        c = a % b
        a = b
        b = c
    end

    a
end
# counter example
# if (a,b) is coprime and (b, c) is coprime,  then (a,c) is also coprime
# if possible, print a b c
# if not possible, print -1
# l과 r사이 간격은 최대 50
if r - l < 2
    puts "-1"
else
    # 두 수의 관계이므로 유클리드 호제법을 이용한다
    # 나눠 떨어지는 시점이 최대공약수(GCD)인데 1이 나오면 서로소이다.
    printed = false

    catch :found do
        for a in l...r
            for b in (a+1)...r
                for c in (b+1)...r
                    if euclidean(a, b) == 1 && euclidean(b, c) == 1 && euclidean(a, c) != 1
                        puts "#{a} #{b} #{c}"
                        printed = true
                        throw :found
                    end
                end
            end
        end
    end

    if !printed
        puts "-1"
    end
end

=begin
data = gets.chomp.split(/\s/).map {|n| n.to_i}
#p data
puts data.inject(:+) / data.length
=end

def nod(x,y)
  if x == y
    return x
  end
  if x == 0
    return y
  end
  if y == 0
    return x
  end
  if x > y
    return nod(y, x % y)
  else
    return nod(x, y % x)
  end
end

data = gets.chomp.split(/\s/).map{|n| n.to_i}
l = data[0]
r = data[1]

a = l
b = a + 1

res = -1

while res < 0 && b <= r
  if nod(a, b) == 1
    res = 0
  else
    b += 1
  end
end

c = b + 1

while res == 0 && c <=r
  if nod(b,c) == 1 && nod(a,c) > 1
    res = 1
  else
    c += 1
  end
end

if res <= 0
  puts -1
else
  puts "#{a} #{b} #{c}"
end

=begin
data = gets.chomp.split(/\s/).map {|n| n.to_i}
#p data
puts data.inject(:+) / data.length
=end

def nod(x,y)
  if x == y
    return x
  end
  if x == 0
    return y
  end
  if y == 0
    return x
  end
  if x > y
    return nod(y, x % y)
  else
    return nod(x, y % x)
  end
end

data = gets.chomp.split(/\s/).map{|n| n.to_i}
l = data[0]
r = data[1]

a = l
b = a + 1
c = b + 1
res = -1

while res < 0 && b <= r
  if nod(a, b) == 1
    res = 0
  else
    b += 1
  end
end

while res == 0 && c <=r
  if nod(b,c) == 1 && nod(a,c) > 1
    res = 1
  else
    c += 1
  end
end

if res < 0
  puts -1
else
  puts "#{a} #{b} #{c}"
end

=begin
data = gets.chomp.split(/\s/).map {|n| n.to_i}
#p data
puts data.inject(:+) / data.length
=end

def nod(x,y)
  if x == y
    return x
  end
  if x == 0
    return y
  end
  if y == 0
    return x
  end
  if x > y
    return nod(y, x % y)
  else
    return nod(x, y % x)
  end
end

data = gets.chomp.split(/\s/).map{|n| n.to_i}
l = data[0]
r = data[1]

a = l
b = a + 1
c = b + 1
res = -1

while res < 0 && b <= r
  if nod(a, b) == 1
    res = 0
  else
    b += 1
  end
end

while res == 0 && c <=r
  if nod(b,c) == 1 && nod(a,c) > 1
    res = 1
  else
    c += 1
  end
end

if res <= 0
  puts -1
else
  puts "#{a} #{b} #{c}"
end

=begin
data = gets.chomp.split(/\s/).map {|n| n.to_i}
#p data
puts data.inject(:+) / data.length
=end

def nod(x,y)
  if x == y
    return x
  end
  if x == 0
    return y
  end
  if y == 0
    return x
  end
  if x > y
    return nod(y, x % y)
  else
    return nod(x, y % x)
  end
end

data = gets.chomp.split(/\s/).map{|n| n.to_i}
l = data[0]
r = data[1]

a = l
b = a + 1

res = -1

while res < 0 && b <= r
  if nod(a, b) == 1
    res = 0
  else
    b += 1
  end
end

c = b + 1

while res == 0 && c <=r
  if nod(b,c) == 1 && nod(a,c) > 1
    res = 1
  else
    c += 1
  end
end

if res <= 0
  puts "#{-1}"
else
  puts "#{a} #{b} #{c}"
end

=begin
data = gets.chomp.split(/\s/).map {|n| n.to_i}
#p data
puts data.inject(:+) / data.length
=end

def nod(x,y)
  if x == y
    return x
  end
  if x == 0
    return y
  end
  if y == 0
    return x
  end
  if x > y
    return nod(y, x % y)
  else
    return nod(x, y % x)
  end
end

data = gets.chomp.split(/\s/).map{|n| n.to_i}
l = data[0]
r = data[1]

a = l
b = a + 1
c = b + 1
res = -1
while res < 0 && c <=r
  if nod(a,b) == 1 && nod(b,c) == 1 && nod(a,c) > 1
    res = 1
  else
    c += 1
  end
end

if res < 0
  puts -1
else
  puts "#{a} #{b} #{c}"
end

#require 'prime'
def coprime?(a, b)
  a.gcd(b).equal?(1)
end
l, r = gets.chomp.split.map(&:to_i)
for i in l...r
  a = i
  b = 0
  c = 0
  for j in (i + 1)..r
    if coprime?(a, j) && b == 0
      b = j
    elsif coprime?(b, j) && c == 0
      c = j if c == 0
    end
    c = 0 if coprime?(a, c)
  end
  break if coprime?(a, c) == false
end
if a == 0 || b == 0 || c == 0
  puts -1
else
  puts [a, b, c].join(" ")
end
#p Prime.prime?(3)
#p coprime?(3, 4)

#require 'prime'
def coprime?(a, b)
  a.gcd(b).equal?(1)
end
l, r = gets.chomp.split.map(&:to_i)
a = 0
b = 0
c = 0
for i in l...r
  a = i
  b = 0
  c = 0
  for j in (i + 1)..r
    b = j if coprime?(a, j) && b == 0
#    print "a = #{a}, b = #{b}, c = #{c}\n"
    for k in (j + 1)..r
      if coprime?(b, k) && c == 0
        c = k if c == 0
#        print "a = #{a}, b = #{b}, c = #{c}\n"
      end
      c = 0 if coprime?(a, c)
    end
    b = 0 if coprime?(a, c) || c == 0
#    print "a = #{a}, b = #{b}, c = #{c}\n"
  end
  break if coprime?(a, c) == false
end
if a == 0 || b == 0 || c == 0
  puts -1
else
  puts [a, b, c].join(" ")
end
#p Prime.prime?(3)
#p coprime?(3, 4)

#require 'prime'
def coprime?(a, b)
  a.gcd(b).equal?(1)
end
l, r = gets.chomp.split.map(&:to_i)
for i in l...r
  a = i
  b = 0
  c = 0
  for j in (i + 1)..r
    b = j if coprime?(a, j) && b == 0
#    print "a = #{a}, b = #{b}, c = #{c}\n"
    for k in (j + 1)..r
      if coprime?(b, k) && c == 0
        c = k if c == 0
#        print "a = #{a}, b = #{b}, c = #{c}\n"
      end
      c = 0 if coprime?(a, c)
    end
  end
  break if coprime?(a, c) == false
end
if a == 0 || b == 0 || c == 0
  puts -1
else
  puts [a, b, c].join(" ")
end
#p Prime.prime?(3)
#p coprime?(3, 4)

#require 'prime'
def coprime?(a, b)
  a.gcd(b).equal?(1)
end
l, r = gets.chomp.split.map(&:to_i)
for i in l...r
  a = i
  b = 0
  c = 0
  for j in (i + 1)..r
    b = j if coprime?(a, j) && b == 0
#    print "a = #{a}, b = #{b}, c = #{c}\n"
    for k in (j + 1)..r
      if coprime?(b, k) && c == 0
        c = k if c == 0
#        print "a = #{a}, b = #{b}, c = #{c}\n"
      end
      c = 0 if coprime?(a, c)
    end
    b = 0 if coprime?(a, c) || c == 0
  end
  break if coprime?(a, c) == false
end
if a == 0 || b == 0 || c == 0
  puts -1
else
  puts [a, b, c].join(" ")
end
#p Prime.prime?(3)
#p coprime?(3, 4)


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

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

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

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

puts (1..n).bsearch { |day|
		minus, cnt = 0, 0
		(0...n).sum { |i|
			res = [0, a[i] - minus].max
			cnt += 1
			if cnt == day
				minus += 1
				cnt = 0
			end
			res
		} >= m
     } || -1