Jack is working on his jumping skills recently. Currently he's located at point zero of the number line. He would like to get to the point x. In order to train, he has decided that he'll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach x.

The input data consists of only one integer x ( - 109 ≤ x ≤ 109).

Output the minimal number of jumps that Jack requires to reach x.

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

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

puts i

j=gets.to_i.abs;i=0;j-=i+=1 while j>0||j&1>0;puts i

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

j=-gets.to_i.abs;i=0;j+=i+=1 while j<0||j&1>0;puts i

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

n=gets.to_i.abs;s=i=0;s+=i+=1 while s<n||(s-n)%2>0;p i

#!/usr/bin/ruby

class Solver
  def solve(x)
    x = x.abs()
    i = 0
    sum = 0

    while sum < x or (x - sum)%2 != 0 do
      i += 1
      sum += i
    end

    puts i
  end
end

x = STDIN.gets.to_i
solver = Solver.new
solver.solve(x)

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

end

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

end

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

end

n=gets.to_i.abs
i=1
k=0
while k<n || k&1>0
    k+=i
    i+=1
end
#p k
puts i-1

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

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


Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger.But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?"Petya knows only 5 integer types:1) byte occupies 1 byte and allows you to store numbers from  - 128 to 1272) short occupies 2 bytes and allows you to store numbers from  - 32768 to 327673) int occupies 4 bytes and allows you to store numbers from  - 2147483648 to 21474836474) long occupies 8 bytes and allows you to store numbers from  - 9223372036854775808 to 92233720368547758075) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower.For all the types given above the boundary values are included in the value range.From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him.

The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).

Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above.

#!/usr/bin/ruby

n = gets.to_i

ret = "BigInteger"
ret = "long" if n <= 9223372036854775807
ret = "int" if n <= 2147483647
ret = "short" if n <= 32767
ret = "byte" if n <= 127
puts ret

#! /usr/bin/ruby
$a = gets.to_i
def f
    if $a < -9223372036854775808 or $a > 9223372036854775807
        return 'BigInteger'
    end

    if $a < -2147483648 or $a > 2147483647
        return 'long'
    end

    if $a < -32768 or $a > 32767
        return 'int'
    end

    if $a < -128 or $a > 127
        return 'short'
    end

    return 'byte'
end

puts f

y = [1 << 7, 1 << 15, 1 << 31, 1 << 63]
name = ['byte', 'short', 'int', 'long', 'BigInteger']

x = gets.to_i

add = 0
if x > 0
  add = -1
else
  add = 0
  x = -x
end

ans = ''
for i in 0...4

  if x <= y[i] + add
    ans = name[i]
    break
  end

end

if ans == ''
  ans = 'BigInteger'
end

puts ans

N = gets.to_i

if N > 9223372036854775807
  puts "BigInteger"
elsif N > 2147483647
  puts "long"
elsif N > 32767
  puts "int"
elsif N > 127
  puts "short"
else
  puts "byte"
end

arr=["byte","short","int","long","BigInteger"]
n=(gets.chomp.to_i).abs
if n > 9223372036854775807
	puts arr[4]
elsif n > 2147483647
	puts arr[3]
elsif n > 32767
	puts arr[2]
elsif n >127
	puts arr[1]
else
	puts arr[0]
end

d=gets.to_i
if  d>=-128 and d<=127 then puts 'byte'
elsif d>=-32768 and d<=32767 then puts 'short'
elsif d>=-2147483648 and d<=2147483647 then puts 'int'
elsif d>=-9223372036854775808 and d<=9223372036854775807 then puts 'long'
else puts 'BigInteger'
end

a = gets.to_i
res = 'BigInteger'
res = 'long' if a <= 9223372036854775807
res = 'int' if a <= 2147483647
res = 'short' if a <= 32767
res = 'byte' if a <= 127
puts res


Mishka started participating in a programming contest. There are $$$n$$$ problems in the contest. Mishka's problem-solving skill is equal to $$$k$$$.Mishka arranges all problems from the contest into a list. Because of his weird principles, Mishka only solves problems from one of the ends of the list. Every time, he chooses which end (left or right) he will solve the next problem from. Thus, each problem Mishka solves is either the leftmost or the rightmost problem in the list.Mishka cannot solve a problem with difficulty greater than $$$k$$$. When Mishka solves the problem, it disappears from the list, so the length of the list decreases by $$$1$$$. Mishka stops when he is unable to solve any problem from any end of the list.How many problems can Mishka solve?

The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 100$$$) — the number of problems in the contest and Mishka's problem-solving skill. The second line of input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$), where $$$a_i$$$ is the difficulty of the $$$i$$$-th problem. The problems are given in order from the leftmost to the rightmost in the list.

Print one integer — the maximum number of problems Mishka can solve.

NoteIn the first example, Mishka can solve problems in the following order: $$$[4, 2, 3, 1, 5, 1, 6, 4] \rightarrow [2, 3, 1, 5, 1, 6, 4] \rightarrow [2, 3, 1, 5, 1, 6] \rightarrow [3, 1, 5, 1, 6] \rightarrow [1, 5, 1, 6] \rightarrow [5, 1, 6]$$$, so the number of solved problems will be equal to $$$5$$$.In the second example, Mishka can't solve any problem because the difficulties of problems from both ends are greater than $$$k$$$.In the third example, Mishka's solving skill is so amazing that he can solve all the problems.

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
pos = a.each_with_index.select { |v, i| v > k }.map { _2 }
if pos.empty?
	puts n
else
	puts pos[0] + (n - 1 - pos[-1])
end

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

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

nums = gets.split.map &:to_i

count = 0

index = 0
for i in 0..n-1
    if nums[i]<=k
        count+=1
    else
        index = i
        break
    end
end

if count!=n
    for i in 0..n-index-1
        if nums[n-i-1]<=k
            count+=1
        else
            break
        end
    end
    puts count
else
    puts count
end

def solution
  _, k = read_ints
  ps = read_ints

  count = 0

  while ps.first && ps.first <= k
    count += 1
    ps.shift
  end

  while ps.last && ps.last <= k
    count += 1
    ps.pop
  end

  puts count
end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

N, K = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
as.shift while as.length > 0 && as[0] <= K
as.pop while as.length > 0 && as[-1] <= K
puts N - as.length

n, k = gets.strip.split.map(&:to_i)
ary = gets.strip.split.map(&:to_i)
ans = 0
si = -1
0.upto(n-1).each do |i|
  if ary[i] <= k
    ans += 1
    si = i
  else
    break
  end
end
(n-1).downto(si+1).each do |i|
  if ary[i] <= k
    ans += 1
  else
    break
  end
end
puts ans

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

ans=0
i=0
while i<n and a[i]<=k
	ans += 1
	i += 1
end
i=n-1
while i>=0 and a[i]<=k
	ans += 1
	i -= 1
end

puts [ans,n].min

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

ans = 0
(0..n-1).each do |i|
  a = as[i]
  if a <= k
    ans += 1
  else
    break
  end
end

(n-1).downto(ans) do |i|
  a = as[i]
  if a <= k
    ans += 1
  else
    break
  end
end

puts ans

# https://codeforces.com/problemset/problem/999/A
def solution(n, k, a)
  count = 0

  i = 0
  while i < n && a[i] <= k do
    count += 1
    i += 1
  end

  return count if count == n

  i = n - 1
  while i > 0 && a[i] <= k do
    count += 1
    i -= 1
  end

  return count
end

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

puts solution(n, k, a)

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

i, j = 0, n - 1

while i < n
  break i if list[i] > k
  i += 1
end

while j >= 0
  break j if list[j] > k
  j -= 1
end

if i == n && j == -1
  puts n
else
  puts (n - (j - i + 1))
end

$n,$k=gets.split
$n,$k,i=$n.to_i,$k.to_i,0
a=gets.split
$ans,$flag=[0,0],[0,0]
def test(c,p)
	if c>$k
	  yield p,0
	else
          yield p,1
	end
end
a.each{|c| test(c.to_i,0){|id,fla|
			     if fla==0
			         $flag[id]=1
			     elsif $flag[id]==0
                                 $ans[id]=$ans[id]+1
			     end
				}}
a.reverse.each{|c| test(c.to_i,1){|id,fla|
			     if fla==0
			         $flag[id]=1
			     elsif $flag[id]==0
                                 $ans[id]=$ans[id]+1
			     end
				}
		}
puts [$ans[0]+$ans[1],$n].min

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

n,k,*a=$<.read.split.map &:to_i
p a.count{|e|e<=k}

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

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


George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample).

The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" — the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59.

In the single line print time p — the time George went to bed in the format similar to the format of the time in the input.

NoteIn the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday.In the third sample, George didn't do to bed at all.

class Tm
  attr_accessor :x
  def initialize(a)
    if a.class == String
      h, m = a.split(":").map(&:to_i)
      @x = h*60 + m
    else
      @x = a
      @x += 1440 if @x < 0
    end
  end
  def -(y)
    Tm.new (@x - y.x)
  end
  def output
    printf "%02d:%02d\n", @x/60, @x%60
  end
end
st = Tm.new gets.chomp
ed = Tm.new gets.chomp
(st-ed).output

require 'date'

$SIZE_DAY_IN_MINUTES = 24 * 60

def convert_to_secunds(minutes)
  minutes * 60
end

def convert_to_minutes(time)
  time.hour * 60 + time.min
end

endTime   = DateTime.parse gets.chomp
startTime = DateTime.parse gets.chomp

endTime_minutes   = convert_to_minutes endTime
startTime_minutes = convert_to_minutes startTime

if startTime_minutes > endTime_minutes
  minutes_yesterday = $SIZE_DAY_IN_MINUTES - startTime_minutes
  sleep = minutes_yesterday + endTime_minutes
else
  sleep = endTime_minutes - startTime_minutes
end

puts Time.at(convert_to_secunds sleep).utc.strftime("%H:%M")

mh,mm = gets.split(":").map(&:to_i)
nh, nn  = gets.split(":").map(&:to_i)

mh=mh-nh
mm= mm-nn

if mm<0
  mm+=60
  mh-=1
end
if mh<0
  mh+=24
end
if mm<10
  mm= "0" + mm.to_s
end
if mh<10
  mh = "0"+mh.to_s
end
puts "#{mh}:#{mm}"

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @now = @dats[0]
    @slept = @dats[1]
  end

  def solve
    slept_h = @slept.split(":")[0].to_i % 24
    sleft_m = @slept.split(":")[1].to_i

    now_h = @now.split(":")[0].to_i
    now_m = @now.split(":")[1].to_i

    m = now_m - sleft_m
    h = now_h - slept_h
    minus = 0
    if m < 0
      m = now_m + 60 - sleft_m
      minus = 1
    end

    h = now_h - slept_h - minus
    h = h < 0 ? h + 24 : h

    h = h < 10 ? "0#{h}" : h
    m = m < 10 ? "0#{m}" : m
    print "#{h}:#{m}"
  end
end

solver = ProblemA.new
solver.solve

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

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

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

fell_asleep = [wake_up[0] - slept[0], wake_up[1] - slept[1]]

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

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

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

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

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

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

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

	return fell_asleep
end

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

fell_asleep = getFellAsleep(wake_up, slept)

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

first = gets.chomp
second = gets.chomp

first = first.split(":").map { |i| i.to_i }
second = second.split(":").map { |i| i.to_i }

first = first[0]*60 + first[1]
second = second[0]*60 + second[1]

x = first - second

if x >= 0 then

	hr = x/60
	mn = x%60

	print "0" if hr < 10
	print hr,":"
	print "0" if mn < 10
	print mn

else

	hr = ((24*60) - (-1*x)) / 60
	mn = ((24*60) - (-1*x)) % 60
	print "0" if hr < 10
	print hr,":"
	print "0" if mn < 10
	print mn

end

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

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

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

sh, sm = gets.chomp.split(":").map(&:to_i)
th, tm = gets.chomp.split(":").map(&:to_i)
if sm < tm
  sh -= 1
  sm += 60
end
ph = [sh - th, (24 + (sh - th)) % 24].max
pm = sm - tm
puts "#{"%02d" % ph}:#{"%02d" % pm}"

def parse_time(time)
  time.split(':').map(&:to_i)
end

def to_str(h, m)
  [h, m].map { |t| "%02d" % t }.join(':')
end

def solve(cur_time, duration)
  hc, mc = parse_time cur_time
  hd, md = parse_time duration
  hg = hc - hd
  mg = mc - md
  if mg < 0
    mg = 60 + mg
    hg -= 1
  end
  hg = (hg + 24) % 24
  to_str(hg, mg)
end

if __FILE__ == $0
  puts solve(gets.chomp, gets.chomp)
end

def parseTime(s)
	[s[0,2].to_i, s[-2, 2].to_i]
end

h1, m1 = parseTime(gets.chomp)
h2, m2 = parseTime(gets.chomp)

t1 = h1*60+m1
t2 = h2*60+m2

if t2 > t1
	t1 += 1440
end

result = t1 - t2

print "%02d:%02d" % [result / 60, result % 60]

mh,mm = gets.split(":").map(&:to_i)
nh, nn  = gets.split(":").map(&:to_i)

mh=mh-nh
mm= mm-nn
if mm<0
  mm-=60
  mh+=1
end
if mh<0
  mh+=24
end
if mm<10
  mm= "0" + mm.to_s
end
if mh<10
  mh = "0"+mh.to_s
end
puts "#{mh}:#{mm}"

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

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

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

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

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


In order to make the "Sea Battle" game more interesting, Boris decided to add a new ship type to it. The ship consists of two rectangles. The first rectangle has a width of $$$w_1$$$ and a height of $$$h_1$$$, while the second rectangle has a width of $$$w_2$$$ and a height of $$$h_2$$$, where $$$w_1 \ge w_2$$$. In this game, exactly one ship is used, made up of two rectangles. There are no other ships on the field.The rectangles are placed on field in the following way:  the second rectangle is on top the first rectangle;  they are aligned to the left, i.e. their left sides are on the same line;  the rectangles are adjacent to each other without a gap. See the pictures in the notes: the first rectangle is colored red, the second rectangle is colored blue.Formally, let's introduce a coordinate system. Then, the leftmost bottom cell of the first rectangle has coordinates $$$(1, 1)$$$, the rightmost top cell of the first rectangle has coordinates $$$(w_1, h_1)$$$, the leftmost bottom cell of the second rectangle has coordinates $$$(1, h_1 + 1)$$$ and the rightmost top cell of the second rectangle has coordinates $$$(w_2, h_1 + h_2)$$$.After the ship is completely destroyed, all cells neighboring by side or a corner with the ship are marked. Of course, only cells, which don't belong to the ship are marked. On the pictures in the notes such cells are colored green.Find out how many cells should be marked after the ship is destroyed. The field of the game is infinite in any direction.

Four lines contain integers $$$w_1, h_1, w_2$$$ and $$$h_2$$$ ($$$1 \leq w_1, h_1, w_2, h_2 \leq 10^8$$$, $$$w_1 \ge w_2$$$) — the width of the first rectangle, the height of the first rectangle, the width of the second rectangle and the height of the second rectangle. You can't rotate the rectangles.

Print exactly one integer — the number of cells, which should be marked after the ship is destroyed.

NoteIn the first example the field looks as follows (the first rectangle is red, the second rectangle is blue, green shows the marked squares):  In the second example the field looks as:

a,b,c,d = gets.split.map(&:to_i)
p a+2*b+c+2*d+(a-c).abs+4

require 'scanf'
w1,h1,w2,h2=scanf("%d%d%d%d")
puts w1*2+h1*2+h2*2+4

DBG = true
w1,h1,w2,h2 = gets.split.map{|z| z.to_i}
puts "#{2*(w1+h1+h2)+4}"

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

w1,h1,w2,h2 = lscan

ans = (w2+1)*2+(h1+h2+1)*2
ans += (w1-w2)*2

p ans

w1, h1, w2, h2 = gets.split.map &:to_i
puts 2*(h1 + h2 + 2 + w1)

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

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

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


InputThe input contains a single integer a (10 ≤ a ≤ 999).OutputOutput 0 or 1.ExamplesInput13Output1Input927Output1Input48Output0

n = gets.to_i

if(n % 2 != 0)
  puts 1
else
  puts 0
end

#!/usr/bin/env ruby

puts gets.chomp.to_i % 2

n = gets.to_i

k = false

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

if (k)
    puts 1
else
    puts 0
end

n = gets.to_i

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


Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?

The first and only line contains two integers x and y (3 ≤ y < x ≤ 100 000) — the starting and ending equilateral triangle side lengths respectively.

Print a single integer — the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.

NoteIn the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do .In the second sample test, Memory can do .In the third sample test, Memory can do: .

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

x, y = gets.split.map(&:to_i)
a = [y] * 3
r = 0
until a[0] == x
	a[0] = [a[1] + a[2] - 1, x].min
	a.sort!
	r += 1
end

puts r

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

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

lol = [y, y, y]
count = 0
while lol[0] != x or lol[1] != x or lol[2] != x do
  asd = lol.dup
  asd.slice!(lol.index(lol.min))
  top = (asd[0] + asd[1])

  if top - 1 > x
    asd << x
  else
    asd << top - 1
  end

  lol = asd
  count += 1
  #printlol(lol)
end

puts count

x, y=gets.split.map &:to_i
a=y, y, y
res=0
while a[0]<x
    a[0]=[a[1]+a[2]-1, x].min
    a.sort!
    res+=1
end
p res

x, y = gets.split.map(&:to_i)
a = [y, y, y]
c = 1
loop do
  mx = a.min
  case mx
  when a[0]
    a[0] = [a[1] + a[2] - 1, x].min
  when a[1]
    a[1] = [a[0] + a[2] - 1, x].min
  when a[2]
    a[2] = [a[0] + a[1] - 1, x].min
  end
  break if a == [x, x, x]
  c += 1
end
puts c

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

s = [m,m,m]

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

puts ans

x, y = gets.chomp.split(' ').map(&:to_i)
sides = [y, y, y]

ans = 0

until sides.count(x) == 3
    sortedSides = Array.new(sides).sort
    power = x - sortedSides[0]

    power = sortedSides[1] + sortedSides[2] - sortedSides[0] - 1 if power >= sortedSides[1] + sortedSides[2] - sortedSides[0]
    sides[sides.index(sortedSides[0])] = sides[sides.index(sortedSides[0])] + power

    #print sides
    #puts ''

    ans += 1
end

puts ans

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

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

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

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

puts count

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

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

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

puts count

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

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

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

puts count

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

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

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

puts count

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

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

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

puts count

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

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

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

puts count

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

s = [m,m,m]

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

puts ans


The girl Taylor has a beautiful calendar for the year y. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.The calendar is so beautiful that she wants to know what is the next year after y when the calendar will be exactly the same. Help Taylor to find that year.Note that leap years has 366 days. The year is leap if it is divisible by 400 or it is divisible by 4, but not by 100 (https://en.wikipedia.org/wiki/Leap_year).

The only line contains integer y (1000 ≤ y < 100'000) — the year of the calendar.

Print the only integer y' — the next year after y when the calendar will be the same. Note that you should find the first year after y with the same calendar.

NoteToday is Monday, the 13th of June, 2016.

#!/usr/bin/ruby
def zeller(_y,m,d)
	m+=1
	if m<4 then _y-=1;m+=12 end
	y=_y/100;z=_y%100
	(5*y+z+y/4+z/4+13*m/5+d-1)%7
end
def leap(y)
	return true if y%400==0
	return false if y%100==0
	y%4==0
end
n=gets.to_i
a=[zeller(n,1,1),leap(n)]
p (n+1..1/0.0).find{|i|a==[zeller(i,1,1),leap(i)]}


Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase and uppercase Latin letters.Let A be a set of positions in the string. Let's call it pretty if following conditions are met:  letters on positions from A in the string are all distinct and lowercase;  there are no uppercase letters in the string which are situated between positions from A (i.e. there is no such j that s[j] is an uppercase letter, and a1 < j < a2 for some a1 and a2 from A). Write a program that will determine the maximum number of elements in a pretty set of positions.

The first line contains a single integer n (1 ≤ n ≤ 200) — length of string s. The second line contains a string s consisting of lowercase and uppercase Latin letters.

Print maximum number of elements in pretty set of positions for string s.

NoteIn the first example the desired positions might be 6 and 8 or 7 and 8. Positions 6 and 7 contain letters 'a', position 8 contains letter 'b'. The pair of positions 1 and 8 is not suitable because there is an uppercase letter 'B' between these position.In the second example desired positions can be 7, 8 and 11. There are other ways to choose pretty set consisting of three elements.In the third example the given string s does not contain any lowercase letters, so the answer is 0.

n = gets.to_i

ans = 0

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

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

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

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

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

gets
p gets.chomp.split(/[A-Z]+/).each.map{|s|s.split(//).uniq.length}.max || 0

n = gets.to_i

ans = 0

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

p ans

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

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

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


Soon a school Olympiad in Informatics will be held in Berland, n schoolchildren will participate there.At a meeting of the jury of the Olympiad it was decided that each of the n participants, depending on the results, will get a diploma of the first, second or third degree. Thus, each student will receive exactly one diploma.They also decided that there must be given at least min1 and at most max1 diplomas of the first degree, at least min2 and at most max2 diplomas of the second degree, and at least min3 and at most max3 diplomas of the third degree.After some discussion it was decided to choose from all the options of distributing diplomas satisfying these limitations the one that maximizes the number of participants who receive diplomas of the first degree. Of all these options they select the one which maximizes the number of the participants who receive diplomas of the second degree. If there are multiple of these options, they select the option that maximizes the number of diplomas of the third degree.Choosing the best option of distributing certificates was entrusted to Ilya, one of the best programmers of Berland. However, he found more important things to do, so it is your task now to choose the best option of distributing of diplomas, based on the described limitations.It is guaranteed that the described limitations are such that there is a way to choose such an option of distributing diplomas that all n participants of the Olympiad will receive a diploma of some degree.

The first line of the input contains a single integer n (3 ≤ n ≤ 3·106) — the number of schoolchildren who will participate in the Olympiad. The next line of the input contains two integers min1 and max1 (1 ≤ min1 ≤ max1 ≤ 106) — the minimum and maximum limits on the number of diplomas of the first degree that can be distributed. The third line of the input contains two integers min2 and max2 (1 ≤ min2 ≤ max2 ≤ 106) — the minimum and maximum limits on the number of diplomas of the second degree that can be distributed.  The next line of the input contains two integers min3 and max3 (1 ≤ min3 ≤ max3 ≤ 106) — the minimum and maximum limits on the number of diplomas of the third degree that can be distributed.  It is guaranteed that min1 + min2 + min3 ≤ n ≤ max1 + max2 + max3.

In the first line of the output print three numbers, showing how many diplomas of the first, second and third degree will be given to students in the optimal variant of distributing diplomas. The optimal variant of distributing diplomas is the one that maximizes the number of students who receive diplomas of the first degree. Of all the suitable options, the best one is the one which maximizes the number of participants who receive diplomas of the second degree. If there are several of these options, the best one is the one that maximizes the number of diplomas of the third degree.

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

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

# http://codeforces.com/contest/557/problem/A
n = gets.chomp.to_i
first, second, third = [].tap do |arr|
  3.times { arr << gets.chomp.split.map(&:to_i) }
end

a = [first[1], n - second[0] - third[0]].min
b = [second[1], n - a - third[0]].min
c = n - a - b

puts [a, b, c].join(' ')

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

@rez = [min1,min2,min3]

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

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

puts @rez.join(' ')

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

d1, d2, d3 = 0, 0, 0

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

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

print n

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

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

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

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


Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of n stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take). Your task is to determine by the given a, b and n who wins the game.

The only string contains space-separated integers a, b and n (1 ≤ a, b, n ≤ 100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile.

If Simon wins, print "0" (without the quotes), otherwise print "1" (without the quotes).

NoteThe greatest common divisor of two non-negative integers a and b is such maximum positive integer k, that a is divisible by k without remainder and similarly, b is divisible by k without remainder. Let gcd(a, b) represent the operation of calculating the greatest common divisor of numbers a and b. Specifically, gcd(x, 0) = gcd(0, x) = x.In the first sample the game will go like that: Simon should take gcd(3, 9) = 3 stones from the heap. After his move the heap has 6 stones left. Antisimon should take gcd(5, 6) = 1 stone from the heap. After his move the heap has 5 stones left. Simon should take gcd(3, 5) = 1 stone from the heap. After his move the heap has 4 stones left. Antisimon should take gcd(5, 4) = 1 stone from the heap. After his move the heap has 3 stones left. Simon should take gcd(3, 3) = 3 stones from the heap. After his move the heap has 0 stones left. Antisimon should take gcd(5, 0) = 5 stones from the heap. As 0 < 5, it is impossible and Antisimon loses.In the second sample each player during each move takes one stone from the heap. As n is even, Antisimon takes the last stone and Simon can't make a move after that.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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
}

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

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

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

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

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

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

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)


Vasily the bear has a favorite rectangle, it has one vertex at point (0, 0), and the opposite vertex at point (x, y). Of course, the sides of Vasya's favorite rectangle are parallel to the coordinate axes. Vasya also loves triangles, if the triangles have one vertex at point B = (0, 0). That's why today he asks you to find two points A = (x1, y1) and C = (x2, y2), such that the following conditions hold:  the coordinates of points: x1, x2, y1, y2 are integers. Besides, the following inequation holds: x1 < x2;  the triangle formed by point A, B and C is rectangular and isosceles ( is right);  all points of the favorite rectangle are located inside or on the border of triangle ABC;  the area of triangle ABC is as small as possible. Help the bear, find the required points. It is not so hard to proof that these points are unique.

The first line contains two integers x, y ( - 109 ≤ x, y ≤ 109, x ≠ 0, y ≠ 0).

Print in the single line four integers x1, y1, x2, y2 — the coordinates of the required points.

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


Once again Tavas started eating coffee mix without water! Keione told him that it smells awful, but he didn't stop doing that. That's why Keione told his smart friend, SaDDas to punish him! SaDDas took Tavas' headphones and told him: "If you solve the following problem, I'll return it to you."  The problem is: You are given a lucky number n. Lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.If we sort all lucky numbers in increasing order, what's the 1-based index of n? Tavas is not as smart as SaDDas, so he asked you to do him a favor and solve this problem so he can have his headphones back.

The first and only line of input contains a lucky number n (1 ≤ n ≤ 109).

Print the index of n among all lucky numbers.

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

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

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

# 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

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


You are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted like this: "BWBW...BW".Some cells of the board are occupied by the chess pieces. Each cell contains no more than one chess piece. It is known that the total number of pieces equals to .In one step you can move one of the pieces one cell to the left or to the right. You cannot move pieces beyond the borders of the board. You also cannot move pieces to the cells that are already occupied.Your task is to place all the pieces in the cells of the same color using the minimum number of moves (all the pieces must occupy only the black cells or only the white cells after all the moves are made).

The first line of the input contains one integer n (2 ≤ n ≤ 100, n is even) — the size of the chessboard.  The second line of the input contains  integer numbers  (1 ≤ pi ≤ n) — initial positions of the pieces. It is guaranteed that all the positions are distinct.

Print one integer — the minimum number of moves you have to make to place all the pieces in the cells of the same color.

NoteIn the first example the only possible strategy is to move the piece at the position 6 to the position 5 and move the piece at the position 2 to the position 3. Notice that if you decide to place the pieces in the white cells the minimum number of moves will be 3.In the second example the possible strategy is to move  in 4 moves, then  in 3 moves,  in 2 moves and  in 1 move.

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