Mahmoud and Ehab play a game called the even-odd game. Ehab chooses his favorite integer n and then they take turns, starting from Mahmoud. In each player's turn, he has to choose an integer a and subtract it from n such that:  1 ≤ a ≤ n.  If it's Mahmoud's turn, a has to be even, but if it's Ehab's turn, a has to be odd. If the current player can't choose any number satisfying the conditions, he loses. Can you determine the winner if they both play optimally?

The only line contains an integer n (1 ≤ n ≤ 109), the number at the beginning of the game.

Output "Mahmoud" (without quotes) if Mahmoud wins and "Ehab" (without quotes) otherwise.

NoteIn the first sample, Mahmoud can't choose any integer a initially because there is no positive even integer less than or equal to 1 so Ehab wins.In the second sample, Mahmoud has to choose a = 2 and subtract it from n. It's Ehab's turn and n = 0. There is no positive odd integer less than or equal to 0 so Mahmoud wins.

n = gets.to_i
if n % 2 == 1
  puts "Ehab"
else
  puts "Mahmoud"
end

#bismillahir rahmanir rahim

n = gets.to_i

if n%2==0
    puts "Mahmoud"
else
    puts "Ehab"
end

n = gets.chomp.to_i

if n % 2 == 0
  print "Mahmoud"
else
  print "Ehab"
end

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

number = gets.to_i

if number % 2 == 0
  puts "Mahmoud"
else
  puts "Ehab"
end

t = gets.chomp.to_i

case t%2==0
    when true
        puts "Mahmoud"
    when false
        puts "Ehab"
end

n = gets.chomp.to_i

if n.odd?
  puts "Ehab"
else
  puts "Mahmoud"
end

puts gets.to_i.odd? ? "Ehab" : "Mahmoud"

n = gets.to_i
puts n.odd? ? 'Ehab' : 'Mahmoud'

##Please tell me this problmem can't be solved with this please

def winner(n)
	return "Ehab" if n % 2 != 0
	return "Mahmoud" if n % 2 == 0
end

n = gets.chomp.to_i
puts(winner(n))

n = gets.to_i
puts [:Mahmoud, :Ehab][n & 1]

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i

# picking -> Mahmoud picks 'a' and it is even.
#         -> Ehab picks 'a' and it is odd.
#         -> repeat until someone wins

# optimal play
# no fun

puts "Ehab" if n.odd?
puts "Mahmoud" if n.even?

puts gets.chomp.to_i.even? ? "Mahmoud" : "Ehab"

n = Integer(gets.chomp)
if n%2 == 0
    puts "Mahmoud"
else
    puts "Ehab"
end

n = gets.to_i
puts(n%2 == 0 ? 'Mahmoud' : 'Ehab')

puts gets.to_i.even? ? 'Mahmoud' : 'Ehab'

n = gets.to_i
puts n == 1 ? "Ehab" : "Mahmoud"

n = gets.chomp
if n%2 == 0
    puts "Mahmoud"
else
    puts "Ehab"
end


Let's denote d(n) as the number of divisors of a positive integer n. You are given three integers a, b and c. Your task is to calculate the following sum:Find the sum modulo 1073741824 (230).

The first line contains three space-separated integers a, b and c (1 ≤ a, b, c ≤ 2000).

Print a single integer — the required sum modulo 1073741824 (230).

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

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

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

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

solve a, b ,c


Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to play a game. They put multiple cards and on each one they wrote a letter, either 'A', or the letter 'B'. Then they began to remove the cards one by one from left to right, every time they removed a card with the letter 'A', Alice gave Bob all the fruits she had at that moment and took out of the bag as many apples and as many oranges as she had before. Thus the number of oranges and apples Alice had, did not change. If the card had written letter 'B', then Bob did the same, that is, he gave Alice all the fruit that he had, and took from the bag the same set of fruit. After the last card way removed, all the fruit in the bag were over.You know how many oranges and apples was in the bag at first. Your task is to find any sequence of cards that Alice and Bob could have played with.

The first line of the input contains two integers, x, y (1 ≤ x, y ≤ 1018, xy > 1) — the number of oranges and apples that were initially in the bag.

Print any sequence of cards that would meet the problem conditions as a compressed string of characters 'A' and 'B. That means that you need to replace the segments of identical consecutive characters by the number of repetitions of the characters and the actual character. For example, string AAABAABBB should be replaced by string 3A1B2A3B, but cannot be replaced by 2A1A1B2A3B or by 3AB2A3B. See the samples for clarifications of the output format. The string that you print should consist of at most 106 characters. It is guaranteed that if the answer exists, its compressed representation exists, consisting of at most 106 characters. If there are several possible answers, you are allowed to print any of them. If the sequence of cards that meet the problem statement does not not exist, print a single word Impossible.

NoteIn the first sample, if the row contained three cards with letter 'B', then Bob should give one apple to Alice three times. So, in the end of the game Alice has one orange and three apples, and Bob has one apple, in total it is one orange and four apples.In second sample, there is no answer since one card is not enough for game to finish, and two cards will produce at least three apples or three oranges.In the third sample, cards contain letters 'AB', so after removing the first card Bob has one orange and one apple, and after removal of second card Alice has two oranges and one apple. So, in total it is three oranges and two apples.

def run target, left, right, result
  if target[0] == 1
    print (target[1] - 1), 'B'
    puts
    return true
  end
  if target[1] == 1
    print (target[0] - 1), 'A'
    puts
    return true
  end
  if target[0].gcd(target[1]) > 1
    puts "Impossible"
    return false
  end
  if (target[0] - target[1]).abs == 1
    result = []
    num  = target[0] > target[1] ? target[1] : target[0]
    char = target[0] > target[1] ? 'B' : 'A'
    print 1, (char == 'A' ? 'B' : 'A')
    if num > 1
      print (num - 1), char
    end
    puts
    return true
  end
  while true
    median = [left[0] + right[0], left[1] + right[1]]

    if fract_compare(median, target) == 0
      if median[0] != target[0]
        puts "Impossible"
        return false
      end
      puts string_compress result
      return true
    end

    if median[0] > target[0] || median[1] > target[1]
      puts "Impossible"
      return false
    end

    if fract_compare(median, target) > 0
      result << 'B'
      right[0] = median[0]
      right[1] = median[1]
    else
      result << 'A'
      left[0] = median[0]
      left[1] = median[1]
    end
  end
end

def fract_compare a, b
  return a[0] * b[1] - a[1] * b[0]
end

def string_compress s
  current_total = 0
  prev = ""
  size = s.size
  result = []
  s.each_with_index do |cur, i|
    if prev == cur
      current_total += 1
    else
      if prev != ""
        result << current_total
        result << prev
      end
      current_total = 1
      prev = cur
    end
    if i == (size - 1)
      result << current_total
      result << prev
    end
  end
  return result.join
end

target = gets.chomp.split(" ").map{ |i| i.to_i }
left = [0, 1]
right = [1, 0]
result = []
run target, left, right, result

def run target, left, right, result
  median = [left[0] + right[0], left[1] + right[1]]

  if fract_compare(median, target) == 0
    if median[0] != target[0]
      puts "Impossible"
      return false
    end
    puts string_compress result
    return true
  end

  if median[0] > target[0] || median[1] > target[1]
    puts "Impossible"
    return false
  end

  if fract_compare(median, target) > 0
    result << 'B'
    right[0] = median[0]
    right[1] = median[1]
  else
    result << 'A'
    left[0] = median[0]
    left[1] = median[1]
  end

  run target, left, right, result
end

def fract_compare a, b
  return (a[0].to_f / a[1]) - (b[0].to_f / b[1])
end

def string_compress s
  current_total = 0
  prev = ""
  size = s.size
  result = []
  s.each_with_index do |cur, i|
    if prev == cur
      current_total += 1
    else
      if prev != ""
        result << current_total
        result << prev
      end
      current_total = 1
      prev = cur
    end
    if i == (size - 1)
      result << current_total
      result << prev
    end
  end
  return result.join
end

target = gets.chomp.split(" ").map{ |i| i.to_i }
left = [0, 1]
right = [1, 0]
result = []
run target, left, right, result

def run target, left, right, result
  if target[0] == 1
    print (target[1] - 1), 'B'
    puts
    return true
  end
  if target[1] == 1
    print (target[0] - 1), 'A'
    puts
    return true
  end
  if target[0].gcd(target[1]) > 1
    puts "Imposible"
    return false
  end
  if (target[0] - target[1]).abs == 1
    result = []
    num  = target[0] > target[1] ? target[1] : target[0]
    char = target[0] > target[1] ? 'B' : 'A'
    print 1, (char == 'A' ? 'B' : 'A')
    if num > 1
      print (num - 1), char
    end
    puts
    return true
  end
  while true
    median = [left[0] + right[0], left[1] + right[1]]

    if fract_compare(median, target) == 0
      if median[0] != target[0]
        puts "Impossible"
        return false
      end
      puts string_compress result
      return true
    end

    if median[0] > target[0] || median[1] > target[1]
      puts "Impossible"
      return false
    end

    if fract_compare(median, target) > 0
      result << 'B'
      right[0] = median[0]
      right[1] = median[1]
    else
      result << 'A'
      left[0] = median[0]
      left[1] = median[1]
    end
  end
end

def fract_compare a, b
  return a[0] * b[1] - a[1] * b[0]
end

def string_compress s
  current_total = 0
  prev = ""
  size = s.size
  result = []
  s.each_with_index do |cur, i|
    if prev == cur
      current_total += 1
    else
      if prev != ""
        result << current_total
        result << prev
      end
      current_total = 1
      prev = cur
    end
    if i == (size - 1)
      result << current_total
      result << prev
    end
  end
  return result.join
end

target = gets.chomp.split(" ").map{ |i| i.to_i }
left = [0, 1]
right = [1, 0]
result = []
run target, left, right, result

def run target, left, right, result
  if (target[0] - target[1]).abs == 1
    result = []
    num  = target[0] > target[1] ? target[1] : target[0]
    char = target[0] > target[1] ? 'B' : 'A'
    print 1, (char == 'A' ? 'B' : 'A'), (num - 1)
    (num - 1).times do |i|
      print char
    end
    puts
    return true
  end
  while true
    median = [left[0] + right[0], left[1] + right[1]]

    if fract_compare(median, target) == 0
      if median[0] != target[0]
        puts "Impossible"
        return false
      end
      puts string_compress result
      return true
    end

    if median[0] > target[0] || median[1] > target[1]
      puts "Impossible"
      return false
    end

    if fract_compare(median, target) > 0
      result << 'B'
      right[0] = median[0]
      right[1] = median[1]
    else
      result << 'A'
      left[0] = median[0]
      left[1] = median[1]
    end
  end
end

def fract_compare a, b
  return a[0] * b[1] - a[1] * b[0]
end

def string_compress s
  current_total = 0
  prev = ""
  size = s.size
  result = []
  s.each_with_index do |cur, i|
    if prev == cur
      current_total += 1
    else
      if prev != ""
        result << current_total
        result << prev
      end
      current_total = 1
      prev = cur
    end
    if i == (size - 1)
      result << current_total
      result << prev
    end
  end
  return result.join
end

target = gets.chomp.split(" ").map{ |i| i.to_i }
left = [0, 1]
right = [1, 0]
result = []
run target, left, right, result


HAII HAS A TUXGIMMEH TUXI HAS A FOO ITS 0I HAS A BAR ITS 0I HAS A BAZ ITS 0I HAS A QUZ ITS 1TUX IS NOW A NUMBRIM IN YR LOOP NERFIN YR TUX TIL BOTH SAEM TUX AN 0I HAS A PURGIMMEH PURPUR IS NOW A NUMBRFOO R SUM OF FOO AN PURBAR R SUM OF BAR AN 1BOTH SAEM BIGGR OF PRODUKT OF FOO AN QUZ AN PRODUKT OF BAR BAZ AN PRODUKT OF FOO AN QUZO RLY?YA RLYBAZ R FOOQUZ R BAROICIM OUTTA YR LOOPBAZ IS NOW A NUMBARVISIBLE SMOOSH QUOSHUNT OF BAZ QUZKTHXBYE

The input contains between 1 and 10 lines, i-th line contains an integer number xi (0 ≤ xi ≤ 9).

Output a single real number. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 4.

nlines = gets.to_i

sum = 0
maxavg = 0
nlines.times do |x|
  sum = sum + gets.to_i
  maxavg = sum.to_f / (x+1) if sum.to_f / (x+1) > maxavg
end

puts maxavg

foo,bar,baz,quz=0,0,0,1
gets.to_i.times do
    foo+=gets.to_i
    bar+=1
    if foo*quz>baz*bar
       baz=foo
       quz=bar
    end
end
puts '%.6f'%(baz*1.0/quz)

tux=gets
foo=0
bar=0
baz=0
quz=1
tux=tux.to_i
while tux>0
  tux-=1
  pur=gets
  pur=pur.to_i
  foo=foo+pur
  bar=bar+1
  if [foo * quz, bar*baz].max == foo*quz then
    baz=foo
    quz=bar
  end
end
puts 1.0*baz/quz

#!/usr/bin/ruby
r=w=0.0
gets.to_i.times{|i|
	w+=1 if gets.to_i==1
	r=[r,w/-~i].max
}
p r

#!/usr/bin/ruby

tux = gets.to_i
foo = 0
bar = 0
baz = 0
quz = 1
it = false
tux.times do
  pur = gets.to_i
  foo = foo + pur
  bar = bar + 1
  it = [foo*quz, bar*baz].max == foo*quz
  if it
    baz = foo
    quz = bar
  end
end

printf("%.10f\n", baz/quz.to_f)

tux = gets.to_i
foo = 0
bar = 0
baz = 0
quz = 1

tux.downto(1) do
  pur = gets.to_i
  foo = foo+pur
  bar = bar+1
  if foo*quz >= bar*baz
    baz = foo
	quz = bar
  end
end

baz = baz.to_f

puts baz/quz

#!/usr/bin/ruby

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

tux = $stdin.gets.chomp.to_i

foo = 0
bar = 0
baz = 0
quz = 1

nums = []

tux.times do
  pur = $stdin.gets.chomp.to_i
  foo += pur
  bar += 1

  if foo*quz >= bar*baz
    baz = foo
    quz = bar
  end
end

puts sprintf("%0.6f", baz/quz.to_f)

tux = gets
foo = 0
bar = 0
baz = 0
quz = 1
tux = tux.to_i
for i in (tux.downto 1) do
	pur = gets.to_f
	foo = foo+pur
	bar = bar+1
	if([foo*quz, bar*baz].max == (foo*quz))
		baz = foo
		quz = bar
	end
end
baz = baz.to_f
puts baz/quz

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

TUX = gets.to_i
FOO = 0
BAR = 0
BAZ = 0
QUZ = 1
TUX.times {
  PUR = gets.to_i
  FOO = FOO + PUR
  BAR = BAR + 1
  if max(FOO * QUZ, BAR * BAZ) == FOO * QUZ then
    BAZ = FOO
    QUZ = BAR
  end
}
p BAZ * 1.0 / QUZ

foo = 0
bar = 0
baz = 0
quz = 1
tux = gets.to_i
until tux == 0
  pur = gets.to_i
  foo = foo + pur
  bar += 1
  it = [foo * quz, bar * baz].max == foo * quz
  if it
    baz = foo
    quz = bar
  end
  tux -= 1
end
puts baz.to_f/quz

foo = 0
bar = 0
baz = 0
quz = 1
tux = gets.to_i
while tux!=0
pur =gets.to_i
foo = foo + pur
bar = bar + 1
if bar*baz<foo*quz
 baz = foo
 quz = bar
end
tux -=1
end

puts baz*1.0/quz

foo = 0
bar = 0
baz = 0
quz = 1
tux = gets.to_i
while tux!=0
pur =gets.to_i
foo = foo + pur
bar = bar + 1
if bar*baz*foo*quz!=foo*quz
 baz = foo
 quz = bar
end
tux -=1
end

puts baz*1.0/quz

nlines = gets.to_i

sum = 0
nlines.times do
  sum = sum + gets.to_i
end

puts sum.to_f / nlines

n=gets.to_i
sum=0.0
n.times do
  sum+=gets.to_i
end
puts sum/n

#!/usr/bin/ruby

tux = gets.to_i
foo = 0
bar = 0
baz = 0
quz = 1
it = false
tux.times do
  pur = gets.to_i
  foo = foo + pur
  bar = bar + 1
  it = [foo*quz, bar*baz].max == foo*quz
end

if it
  baz = foo
  quz = bar
end
printf("%.10f\n", baz/quz.to_f)

tux = $stdin.gets.chomp.to_i

foo = 0
bar = 0
baz = 0
quz = 0

nums = []

tux.times do
  pur = $stdin.gets.chomp.to_i
  foo += pur
  bar += 1
end
if foo * quz  >= bar * baz && bar * baz >= foo * quz
  baz = foo
  quz = bar
end

puts sprintf("%0.6f", baz/quz.to_f)

n = gets.to_i
sum = 0
n.times {
  sum += gets.to_f
}
p sum / n

foo = 0
bar = 0
baz = 0
quz = 0
tux = gets.to_i
while tux!=0
pur =gets.to_i
foo = foo + pur
bar = bar + 1
#if bar*baz==foo*quz
 baz = foo
 quz = bar
#end
tux -=1
end

puts baz*1.0/quz


Ksenia has ordinary pan scales and several weights of an equal mass. Ksenia has already put some weights on the scales, while other weights are untouched. Ksenia is now wondering whether it is possible to put all the remaining weights on the scales so that the scales were in equilibrium. The scales is in equilibrium if the total sum of weights on the left pan is equal to the total sum of weights on the right pan.

The first line has a non-empty sequence of characters describing the scales. In this sequence, an uppercase English letter indicates a weight, and the symbol "|" indicates the delimiter (the character occurs in the sequence exactly once). All weights that are recorded in the sequence before the delimiter are initially on the left pan of the scale. All weights that are recorded in the sequence after the delimiter are initially on the right pan of the scale.  The second line contains a non-empty sequence containing uppercase English letters. Each letter indicates a weight which is not used yet.  It is guaranteed that all the English letters in the input data are different. It is guaranteed that the input does not contain any extra characters.

If you cannot put all the weights on the scales so that the scales were in equilibrium, print string "Impossible". Otherwise, print the description of the resulting scales, copy the format of the input. If there are multiple answers, print any of them.

dx = gets.chomp.split("|")
dx[0] ||= ""
dx[1] ||= ""
f = false

if dx[0].length > dx[1].length
  f = true
  dx[0], dx[1] = dx[1], dx[0]
end
dx[0] += gets.chomp
if dx[0].length < dx[1].length or (dx[0].length - dx[1].length) % 2 != 0
  puts "Impossible"
else
  tmp = dx[0].length - dx[1].length
  dx = [dx[0][0...(dx[1].length + (tmp / 2))], dx[1] + dx[0][(dx[1].length + (tmp / 2))..-1]]
  if f
    puts dx[1] + "|" + dx[0]
  else
    puts dx[0] + "|" + dx[1]
  end
end

s = gets.strip
a, b = s.split("|")

a = a.to_s
b = b.to_s

c = gets.strip
c = c.to_s

if a.size > b.size then
  diff = a.size - b.size
  if diff <= c.size then
    b += c[0...diff]
    c = c[diff..-1]
  end
end
if a.size < b.size then
  diff = b.size - a.size
  if diff <= c.size then
    a += c[0...diff]
    c = c[diff..-1]
  end
end

if a.size == b.size
  if c.size.even?
    puts "#{a}#{c[0...c.size/2]}|#{b}#{c[c.size/2..-1]}"
  else
    puts "Impossible"
  end
else
  puts "Impossible"
end

s=gets.chomp+'|x'
t=gets.chomp
l,r=s.split '|'
t.each_char{|c|l.size>r.size ? r<<c :l<<c}
puts l.size!=r.size ? 'Impossible':"#{l}|#{r}"

s=gets.chomp
t=gets.chomp
l,r=s.split '|'
l||=""
r||=""
m=l.size+r.size+t.size
if m%2==0
  m/=2
  p=m-l.size
  q=m-r.size
  if p>=0 && q>=0
    l<<t[0...p]
    r<<t[p...t.size]
    puts "#{l}|#{r}"
    exit
  end
end
puts 'Impossible'

a, b = gets.chomp.split '|'
a = '' if not a
b = '' if not b
c = gets
(c ? c.chomp : '').each_char { |ch|
  if a.size < b.size then a += ch
  else b += ch end
}
puts a.size == b.size ? a + '|' + b : 'Impossible'


There exists an island called Arpa’s land, some beautiful girls live there, as ugly ones do.Mehrdad wants to become minister of Arpa’s land. Arpa has prepared an exam. Exam has only one question, given n, print the last digit of 1378n.   Mehrdad has become quite confused and wants you to help him. Please help, although it's a naive cheat.

The single line of input contains one integer n (0  ≤  n  ≤  109).

Print single integer — the last digit of 1378n.

NoteIn the first example, last digit of 13781 = 1378 is 8.In the second example, last digit of 13782 = 1378·1378 = 1898884 is 4.

def modPow( x, n, m )
    answer = 1
    while n > 0
        if n % 2 != 0
            answer = ( answer * x ) % m
        end
        x = (x*x) % m;
        n /= 2
    end
    return answer
end

exponent = gets.chomp.to_i
puts modPow(1378, exponent, 10)

n = gets.to_i

if n == 0
  puts "1"
else
  case n%4
  when 1
    puts "8"
  when 2
    puts "4"
  when 3
    puts "2"
  when 0
    puts "6"
  end
end

a = gets.chomp.to_i
if a==0
    p 1
elsif a%4==1
    p 8
elsif a%4==2
    p 4
elsif a%4==3
    p 2
else
    p 6
end

n = gets.chomp.to_i
if n == 0
     puts 1
     exit
end
case n%4
when 0
    puts 6
when 1
    puts 8
when 2
    puts 4
when 3
    puts 2
end

n=gets.to_i
m=1
r=8
while n>0
	if n%2==1 then
		m=(m*r)%10
	end
	r=(r*r)%10
	n/=2
end
puts m

n = gets.chomp.to_i
if n == 0
	puts 1
else
	puts [8,4,2,6][(n-1)%4]
end

n=gets.to_i
x=1378
r=1
while n>0
    r=r*x%10 if n.odd?
    x=x*x%10
    n>>=1
end
p r

n = gets.chomp.to_i
if n == 0
	print 1
else
	if n % 4 == 2
		print 4
	elsif n % 4 == 3
		print 2
	elsif n % 4 == 0
		print 6
	elsif n % 4 == 1
		print 8
	end
end

n = gets.strip.to_i
if n.zero?
  puts 1
  exit
end
a = [8, 4, 2, 6]
n -= 1
puts a[n % 4]

n=gets.to_i
if n==0
    puts 1
else
    puts [8,4,2,6][(n-1)%4]
end

n=gets.to_i
if n == 0
    puts 1
else
    puts "8426"[(n+3)%4]
end

n = gets.to_i

if n.zero?
  puts 1
else
  puts [6, 8, 4, 2][n%4]
end

n = gets.to_i

if n == 0
  puts "1"
else
  case n%4
  when 1
    puts "8"
  when 2
    puts "4"
  when 3
    puts "2"
  when 4
    puts "6"
  end
end

n = gets.chomp.to_i
case n%4
when 0
    puts 6
when 1
    puts 8
when 2
    puts 4
when 3
    puts 2
end

# coding: utf-8

# if __FILE__ == $0
# end

# power of 1378, and last digit
# and cyclic.
# 1 -> 8, 2 -> 6, 3  -> 2, 4 -> 6, 5 -> 8
n = gets.to_i
base = 8
((n-1)%6).times {
    base *= 8
    base = base % 10
}

puts base

n = gets.strip.to_i
a = [8, 4, 2, 6]
n -= 1
puts a[n % 4]


One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got access to the memory of his not less mean sister Hexadecimal. He loaded there a huge amount of n different natural numbers from 1 to n to obtain total control over her energy.But his plan failed. The reason for this was very simple: Hexadecimal didn't perceive any information, apart from numbers written in binary format. This means that if a number in a decimal representation contained characters apart from 0 and 1, it was not stored in the memory. Now Megabyte wants to know, how many numbers were loaded successfully.

Input data contains the only number n (1 ≤ n ≤ 109).

Output the only number — answer to the problem.

NoteFor n = 10 the answer includes numbers 1 and 10.

ns = gets.chomp
s2 = ""
for i in 0..ns.length-1
	wk = ns[i].to_i
	if wk > 1
		for j in i..ns.length-1
			s2 << "1"
		end
		break
	else
		s2 << ns[i]
	end
end
puts s2.to_i(2)

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

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

n=gets.to_i
ans=0
(1<<12).times{|i|
        v=0
        for j in 0..11
                v*=10
                if ((i>>j)&1)==1 then
                        v+=1
                end
        end
        if v<=n then
                ans+=1
        end
}
p ans-1

$n = gets.to_i

$counter = 0
$len = Math.log10($n).to_i + 1

def solve(num, depth)
	if depth == $len
		$counter += 1 if num > 0 and num <= $n
		return
	end

	[0, 1].each do |i|
		solve(num*10 + i, depth+1)
	end
end

solve(0, 0)

puts $counter

def bin2dec (b)
  ans = 0
  base = 1
  while b > 0 do
    ans += (b % 2) * base
    b = b >> 1
    base = base * 10
  end
  return ans
end
n = gets.to_i;
m = n
len = 0
while m != 0 do
  len += 1
  m /= 10
end
cand = (1 << len) - 1
#p cand
ans = 0
for i in 1..cand do
  v = bin2dec(i)
  if v >= 1 && v <= n then ans += 1 end
#  print i, " " , v
#  puts
end
print ans
puts

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

n = gets.to_i
i = 1
a = 1
c = 0
while a <= n
	c += 1
	i += 1
	a = i.to_s(2).to_i
end
puts c

n = gets.to_i
m = 0
loop do
  break if (m.to_s(2)).to_i > n
  m += 1
end
p m - 1

s = gets.strip

one = false

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

n = s.to_i(2)
puts n

$n = 0
$res = 0
def go(x)
  if x <= $n then
    $res += 1
    go(10 * x + 0)
    go(10 * x + 1)
  end
end
$n = gets.chomp.to_i
go(1)
p $res

n = gets.to_i

z = 1
res=0
while z.to_s(2).to_i <= n
  z+=1
  res+=1
end

puts res

ns = gets.chomp
s2 = ""
for i in 0..ns.length-1
	wk = ns[i].to_i
	wk = 1 if wk > 1
	s2 << wk.to_s
end
puts s2.to_i(2)

$n = gets.to_i

$counter = 0
$len = Math.log10($n).to_i + 1

def solve(num, depth)
	if depth == $len
		$counter += 1 if num < $n
		return
	end

	[0, 1].each do |i|
		solve(num*10 + i, depth+1)
	end
end

solve(0, 0)

puts $counter


It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends.Today he invented one simple game to play with Lena, with whom he shares a desk. The rules are simple. Sasha draws n sticks in a row. After that the players take turns crossing out exactly k sticks from left or right in each turn. Sasha moves first, because he is the inventor of the game. If there are less than k sticks on the paper before some turn, the game ends. Sasha wins if he makes strictly more moves than Lena. Sasha wants to know the result of the game before playing, you are to help him.

The first line contains two integers n and k (1 ≤ n, k ≤ 1018, k ≤ n) — the number of sticks drawn by Sasha and the number k — the number of sticks to be crossed out on each turn.

If Sasha wins, print "YES" (without quotes), otherwise print "NO" (without quotes). You can print each letter in arbitrary case (upper of lower).

NoteIn the first example Sasha crosses out 1 stick, and then there are no sticks. So Lena can't make a move, and Sasha wins.In the second example Sasha crosses out 4 sticks, then Lena crosses out 4 sticks, and after that there are only 2 sticks left. Sasha can't make a move. The players make equal number of moves, so Sasha doesn't win.

# Codeforces My Practice
# author: Leonardone @ NEETSDKASU

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

k2 = k * 2
v = n % k2

if v < k
    puts :NO
else
    puts :YES
end

# Try Codeforces
# author: Leonardone @ NEETSDKASU

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

v = n / k

if v.even?
    puts :NO
else
    puts :YES
end

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

puts (n / k).odd? ? 'YES' : 'NO'

n, k = gets.strip.split.map(&:to_i)
ans = "YES"
div = n / k
ans = "NO" if div.even?
puts ans

n,k=gets.split.map{|e| e.to_i}
t=n/k
if t%2==1 then
	puts "YES"
else
	puts "NO"
end

n, k=gets.split.map &:to_i
puts (n/k).odd? ? "YES" : "NO"

n,m = gets.split
n = n.to_i
m = m.to_i
k = Integer(n / m)
if k % 2 == 1 then
	puts "YES"
else
	puts "NO"
end

# coding: utf-8

# if __FILE__ == $0
# end

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

if (n/k).odd?
    puts "YES"
else
    puts "NO"
end

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

if (n/k)%2 == 0
	print "NO"
else
	print "YES"
end

n,k = gets.split.map(&:to_i)
n/=k
puts (n&1!=0 ? "YES" : "NO")

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

def kek(n,k)
  (n/k).even? ? 'NO' : 'YES'
end
puts kek(n,k)

a=gets.split.map &:to_i
puts (a[0]/a[1])%2==0 ?"NO":"YES"

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

# https://codeforces.com/problemset/problem/832/A
s = gets.chomp
arr = s.split(' ')

n = arr[0].to_i
k = arr[1].to_i
if ((n % k == 0) && (n / k % 2 != 0)) || ((n % k != 0) && (n / k % 2 != 0))
  puts 'YES'
else
  puts 'NO'
end

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

if (n/k)%2==1
  puts 'YES'
else
  puts 'NO'
end

# Try Codeforces
# author: Leonardone @ NEETSDKASU

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

k2 = k * 2
v = n % k2

if v < n
    puts :NO
else
    puts :YES
end

# Try Codeforces
# author: Leonardone @ NEETSDKASU

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

k2 = k * 2
v = n % k2

if v <= n
    puts :NO
else
    puts :YES
end

# Try Codeforces
# author: Leonardone @ NEETSDKASU

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

v = n / k

if v.odd?
    puts :NO
else
    puts :YES
end

n, k = gets.strip.split.map(&:to_i)
ans = "YES"
mod = n % k
div = n / k
ans = "NO" if div.even? && mod > 0
puts ans

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

if n%k == 0
	print "YES"
else
	if (n/k)%2 == 0
		print "NO"
	else
		print "YES"
	end
end

a=gets.split.map &:to_i
puts (a[1]/a[0])%2==0 ?"NO":"YES"


You are given the current time in 24-hour format hh:mm. Find and print the time after a minutes.Note that you should find only the time after a minutes, see the examples to clarify the problem statement.You can read more about 24-hour format here https://en.wikipedia.org/wiki/24-hour_clock.

The first line contains the current time in the format hh:mm (0 ≤ hh < 24, 0 ≤ mm < 60). The hours and the minutes are given with two digits (the hours or the minutes less than 10 are given with the leading zeroes). The second line contains integer a (0 ≤ a ≤ 104) — the number of the minutes passed.

The only line should contain the time after a minutes in the format described in the input. Note that you should print exactly two digits for the hours and the minutes (add leading zeroes to the numbers if needed). See the examples to check the input/output format.

h, m = gets.split( ":" ).map( &:to_i )
a = gets.to_i
t = Time.local( 2016, 1, 1, h, m, 0 )
puts ( t + a * 60 ).strftime( "%H:%M" )

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

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

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

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

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


Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one.In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8.To graduate with «A» certificate, Noora has to have mark k.Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.

The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack.

Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.

NoteConsider the first example testcase.Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to . Consequently, new final mark is 10. Less number of marks won't fix the situation.In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate.

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

n, k = gis
a = gis

sum = a.inject(:+) * 10

lim = (k - 1) * 10 + 5

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

puts (i - n)

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


A long time ago (probably even in the first book), Nicholas Flamel, a great alchemist and the creator of the Philosopher's Stone, taught Harry Potter three useful spells. The first one allows you to convert a grams of sand into b grams of lead, the second one allows you to convert c grams of lead into d grams of gold and third one allows you to convert e grams of gold into f grams of sand. When Harry told his friends about these spells, Ron Weasley was amazed. After all, if they succeed in turning sand into lead, lead into gold, and then turning part of the gold into sand again and so on, then it will be possible to start with a small amount of sand and get huge amounts of gold! Even an infinite amount of gold! Hermione Granger, by contrast, was skeptical about that idea. She argues that according to the law of conservation of matter getting an infinite amount of matter, even using magic, is impossible. On the contrary, the amount of matter may even decrease during transformation, being converted to magical energy. Though Hermione's theory seems convincing, Ron won't believe her. As far as Ron is concerned, Hermione made up her law of conservation of matter to stop Harry and Ron wasting their time with this nonsense, and to make them go and do homework instead. That's why Ron has already collected a certain amount of sand for the experiments. A quarrel between the friends seems unavoidable...Help Harry to determine which one of his friends is right, and avoid the quarrel after all. To do this you have to figure out whether it is possible to get the amount of gold greater than any preassigned number from some finite amount of sand.

The first line contains 6 integers a, b, c, d, e, f (0 ≤ a, b, c, d, e, f ≤ 1000).

Print "Ron", if it is possible to get an infinitely large amount of gold having a certain finite amount of sand (and not having any gold and lead at all), i.e., Ron is right. Otherwise, print "Hermione".

Ron Hermione Hermione Hermione Ron Hermione Ron

NoteConsider the first sample. Let's start with the 500 grams of sand. Apply the first spell 5 times and turn the sand into 1000 grams of lead. Then apply the second spell 4 times to get 600 grams of gold. Let’s take 400 grams from the resulting amount of gold turn them back into sand. We get 500 grams of sand and 200 grams of gold. If we apply the same operations to 500 grams of sand again, we can get extra 200 grams of gold every time. Thus, you can get 200, 400, 600 etc. grams of gold, i.e., starting with a finite amount of sand (500 grams), you can get the amount of gold which is greater than any preassigned number.In the forth sample it is impossible to get sand, or lead, or gold, applying the spells.In the fifth sample an infinitely large amount of gold can be obtained by using only the second spell, which allows you to receive 1 gram of gold out of nothing. Note that if such a second spell is available, then the first and the third one do not affect the answer at all.The seventh sample is more interesting. We can also start with a zero amount of sand there. With the aid of the third spell you can get sand out of nothing. We get 10000 grams of sand in this manner. Let's get 100 grams of lead using the first spell 100 times. Then make 1 gram of gold from them. We managed to receive 1 gram of gold, starting with a zero amount of sand! Clearly, in this manner you can get an infinitely large amount of gold.

a, b, c, d, e, f = gets.split.map {|n| n.to_f }
conv_ratio = (a/b)*(c/d)*(e/f)

if d == 0
  puts "Hermione"
elsif c == 0 or (a == 0 and b > 0)
  puts "Ron"
elsif conv_ratio < 1
  puts "Ron"
else
  puts "Hermione"
end

a, b, c, d, e, f = gets.split.map {|n| n.to_f }
conv_ratio = (a/b)*(c/d)*(e/f)

if d == 0
  puts "Hermione"
elsif c == 0
  puts "Ron"
elsif b == 0
  puts "Hermione"
elsif a == 0
  puts "Ron"
elsif f == 0
  puts "Hermione"
elsif conv_ratio < 1
  puts "Ron"
else
  puts "Hermione"
end

a, b, c, d, e, f = gets.chomp.split.map{|e| e.to_i}

if (a * b * c * d * e * f != 0)
  if (a * c * e < b * d * f)
    puts "Ron"
  else
    puts "Hermione"
  end
else
  if (d == 0)
    puts "Hermione"
  elsif (c == 0)
    puts "Ron"
  else # d, c > 0
    if (b == 0)
      puts "Hermione"
    elsif (a == 0)
      puts "Ron"
    else # a, b > 0
      if (f == 0)
        puts "Hermione"
      else
        puts "Ron"
      end
    end
  end
end

a,b,c,d,e,f = gets.split(' ').map(&:to_i)
if (c==0 && d !=0) || (a==0 &&  b !=0 && c*d !=0)
  puts "Ron"
else
  if (a*c*e) < (b*d*f)
    puts "Ron"
  else
    puts "Hermione"
  end
end

a, b, c, d, e, f = gets.split(' ').map{|s| s.to_i}
ab, cd, ef = [[a, b], [c, d], [e, f]].map{|aa, bb|bb/aa.to_f}
g = ab*cd*ef
m = [cd.infinite?, ab.infinite?&&cd>0, ef.infinite?&&ab>0&&cd>0]
puts (m.any?||g>1.0) ? 'Ron' : 'Hermione'

def foo(a)
  if a[3] == 0
    return false
  end

  if a[2] == 0 && a[3] > 0
    return true
  end

  if a[1]*a[3] == 0
    return false
  end

  if a[0]*a[2] == 0 && a[1]*a[3] > 0
    return true
  end

  if a[1]*a[3]*a[5] == 0
    return false
  end

  if a[0]*a[2]*a[4] == 0 && a[1]*a[3]*a[5] > 0
    return true
  end

  pr = 1.0
  0.step(4, 2) do |i|
    pr = pr / a[i] * a[i-1]
  end
  if pr > 1
    return true
  else
    return false
  end
end

a = gets.split.map(&:to_i)
puts(foo(a) ? "Ron" :  "Hermione")

a = gets.split.map(&:to_i)
if a[2] == 0 and a[3] != 0
  puts "Ron"
else
  puts(a[0]*a[2]*a[4] < a[1]*a[3]*a[5] ? "Ron" : "Hermione")
end

a, b, c, d, e, f = gets.split.map {|n| n.to_f }
conv_ratio = (a/b)*(c/d)*(e/f)

if d == 0
  puts "Hermione"
elsif c == 0
  puts "Ron"
elsif b == 0 or f == 0
  puts "Hermione"
elsif conv_ratio < 1
  puts "Ron"
else
  puts "Hermione"
end

a, b, c, d, e, f = gets.split(' ').map{|s| s.to_i}
ab, cd, ef = [[a, b], [c, d], [e, f]].map{|aa, bb|bb/aa.to_f}
g = ab*cd*ef
puts (cd.infinite?||g>1.0) ? 'Ron' : 'Hermione'

a, b, c, d, e, f = gets.split(' ').map{|s| s.to_i}
g = (b/a.to_f) * (d/c.to_f) * (f/e.to_f)
puts (g>1.0) ? 'Ron' : 'Hermione'

a, b, c, d, e, f = gets.split(' ').map{|s| s.to_i}
ab, cd, ef = [[a, b], [c, d], [e, f]].map{|aa, bb|bb/aa.to_f}
g = ab*cd*ef
m = [cd.infinite?, ab.infinite?&&!cd.nan?, ef.infinite?&&!ab.nan?&&!cd.nan?]
puts (m.any?||g>1.0) ? 'Ron' : 'Hermione'


The last stage of Football World Cup is played using the play-off system.There are n teams left in this stage, they are enumerated from 1 to n. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third — with the fourth, the fifth — with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over.Arkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids a and b can meet.

The only line contains three integers n, a and b (2 ≤ n ≤ 256, 1 ≤ a, b ≤ n) — the total number of teams, and the ids of the teams that Arkady is interested in.  It is guaranteed that n is such that in each round an even number of team advance, and that a and b are not equal.

In the only line print "Final!" (without quotes), if teams a and b can meet in the Final. Otherwise, print a single integer — the number of the round in which teams a and b can meet. The round are enumerated from 1.

NoteIn the first example teams 1 and 2 meet in the first round.In the second example teams 2 and 6 can only meet in the third round, which is the Final, if they win all their opponents in earlier rounds.In the third example the teams with ids 7 and 5 can meet in the second round, if they win their opponents in the first round.

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

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

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

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

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

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


Little Artem got n stones on his birthday and now wants to give some of them to Masha. He knows that Masha cares more about the fact of receiving the present, rather than the value of that present, so he wants to give her stones as many times as possible. However, Masha remembers the last present she received, so Artem can't give her the same number of stones twice in a row. For example, he can give her 3 stones, then 1 stone, then again 3 stones, but he can't give her 3 stones and then again 3 stones right after that.How many times can Artem give presents to Masha?

The only line of the input contains a single integer n (1 ≤ n ≤ 109) — number of stones Artem received on his birthday.

Print the maximum possible number of times Artem can give presents to Masha.

NoteIn the first sample, Artem can only give 1 stone to Masha.In the second sample, Atrem can give Masha 1 or 2 stones, though he can't give her 1 stone two times.In the third sample, Atrem can first give Masha 2 stones, a then 1 more stone.In the fourth sample, Atrem can first give Masha 1 stone, then 2 stones, and finally 1 stone again.

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

	remaining = stones - (threes * 3) # 2

	count = threes * 2

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

	if remaining == 1
		count += 1
	end

	count = 1 if count == 0

	count
end

stones = gets.chomp.to_i

puts calc(stones)

n = gets.to_i
begin
  puts n%3 == 0 ? n/3 * 2 : n/3 * 2 + 1
rescue
end

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

n = gi

if n < 3
    puts 1
    exit
end

=begin

n = 3
1 2

n = 4
1 2 1

n = 5
1 2 1

n = 6
1 2 1 2

n = 7
1 2 1 2 1

n = 8
1 2 1 2 1

n = 9
1 2 1 2 1 2

n = 10
1 2 1 2 1 2 1

n = 11
1 2 1 2 1 2 1

a n
3 4 5
4 6
5 7 8
6 9
7 10 11
8 12

=end

if n % 3 == 0
    n /= 3
    puts 2 * n
else
    n /= 3
    puts 2 * n + 1
end

n = readline.to_i
ct = n / 3
ct *= 2
if n % 3 != 0
  ct += 1
end
puts ct

a=gets.chomp.to_i
ans=((a/3.0).ceil)*2
if a%3==0
puts "#{ans}"
else
puts "#{ans-1}"
end

n = gets.to_i
ans = n / 3 * 2
if (n % 3 == 0)
	p ans

else
	p ans + 1
end

n = gets.to_i

ans = 0

ans += n/3 * 2
n %= 3

ans += 1 if n > 0

puts ans

n = gets.chomp.to_i

a = n / 3
b = n % 3
if b != 0 then
  puts a * 2 + 1
else
  puts a * 2
end

n = gets.to_i
cnt = (n / 3) * 2
res = n % 3
cnt += 1 if res >= 1
puts cnt

n = gets.to_i

ans = n / 3 * 2
ans += 1 if n % 3 != 0
puts ans

n=gets.to_i
if n%3==0 #then
  p n/3*2
else
  p n/3*2+1
end

stones = gets.chomp.to_i

threes = (stones / 3).to_i

remaining = stones - (threes * 3)

count = threes * 2

count += 1 if remaining == 1

count = 1 if count == 0

puts count

n = gets.to_i
begin
  puts n%3 == 0 ? n/3 : n/3 + 1
rescue
end

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

n = gi

if n < 3
    puts 1
    exit
end

=begin

n = 3
1 2

n = 4
1 2 1

n = 5
1 2 1

n = 6
1 2 1 2

n = 7
1 2 1 2 1

n = 8
1 2 1 2 1

n = 9
1 2 1 2 1 2

n = 10
1 2 1 2 1 2 1

a n
3 4 5
4 6
5 7 8
6 9
7 10 11
8 12

=end

if n % 3 == 0
    puts 2 * n / 3
else
    puts 2 * n / 3 + 1
end

n = readline.to_i
ct = n / 3
if n % 3 != 0
  ct += 1
end
puts ct


In the city of Ultima Thule job applicants are often offered an IQ test. The test is as follows: the person gets a piece of squared paper with a 4 × 4 square painted on it. Some of the square's cells are painted black and others are painted white. Your task is to repaint at most one cell the other color so that the picture has a 2 × 2 square, completely consisting of cells of the same color. If the initial picture already has such a square, the person should just say so and the test will be completed. Your task is to write a program that determines whether it is possible to pass the test. You cannot pass the test if either repainting any cell or no action doesn't result in a 2 × 2 square, consisting of cells of the same color.

Four lines contain four characters each: the j-th character of the i-th line equals "." if the cell in the i-th row and the j-th column of the square is painted white, and "#", if the cell is black.

Print "YES" (without the quotes), if the test can be passed and "NO" (without the quotes) otherwise.

NoteIn the first test sample it is enough to repaint the first cell in the second row. After such repainting the required 2 × 2 square is on the intersection of the 1-st and 2-nd row with the 1-st and 2-nd column.

res = "NO"
data = []

4.times do
    s = gets.split("")
    data << s
end

3.times do |i|
    if data[1][i] == data[1][i+1] then
        if data[0][i] == data[1][i] || data[0][i+1] == data[1][i] then
            res = "YES"
        end
        if data[2][i] == data[1][i] || data[2][i+1] == data[1][i] then
            res = "YES"
        end
    end
end

if res == "NO" then
    3.times do |i|
        if data[-1][i] == data[-1][i+1] then
            if data[2][i] == data[1][i] || data[2][i+1] == data[1][i] then
                res = "YES"
            end
        end
    end
end

puts res

s1 = gets.split("")
s2 = gets.split("")
s3 = gets.split("")
s4 = gets.split("")
s= s1.zip(s2,s3,s4)

yes=false
i=0
3.times do
  j=0
  3.times do
    count=0
    if s[i][j]=='#'
      count+=1
    end
    if s[i+1][j]=='#'
      count+=1
    end
    if s[i+1][j+1]=='#'
      count+=1
    end
    if s[i][j+1]=='#'
       count+=1
    end
    if count!=2
      yes=true
      break
    end
    j+=1
  end
  i+=1
end

if yes
  puts "YES"
else
  puts "NO"
end

matrix = []
4.times { matrix << gets.chomp.chars.to_a }

tiles = []
(0..2).each do |i|
  (0..2).each do |j|
    tiles << [matrix[i][j], matrix[i][j+1], matrix[i+1][j], matrix[i+1][j+1]]
  end
end

freq = tiles.map { |t| [t.count('.'), t.count('#')].max }

print freq.max >= 3 ? "YES" : "NO"

#!/usr/bin/ruby1.9

grid = []

4.times do
	grid.push(STDIN.readline.strip)
end

3.times do |r|
	3.times do |c|
		count = 0
		count += 1 if grid[r][c] == '.'
		count += 1 if grid[r+1][c] == '.'
		count += 1 if grid[r][c+1] == '.'
		count += 1 if grid[r+1][c+1] == '.'
		next if count == 2
		puts 'YES'
		exit
	end
end

puts 'NO'

def run
  ary = []
  $stdin.readlines.each do |line|
    ary << line.split(//)
  end

  3.times do |x|
    3.times do |y|
      if [ary[x][y], ary[x+1][y], ary[x][y+1], ary[x+1][y+1]].select {|x| x == '#'}.length != 2
        $stdout.puts "YES"
        return
      end
    end
  end
  $stdout.puts "NO"
end

run if $0 == __FILE__

class A
  def initialize
    @cells = []
    @list = [[1,1],[1,2],[2,1],[2,2]]
    @pos1 = [[-1,0],[-1,1],[0,1]]
    @pos2 = [[-1,0],[-1,-1],[0,-1]]
    @pos3 = [[0,-1],[1,-1],[1,0]]
    @pos4 = [[0,1],[1,1],[1,0]]
    4.times do
      @cells << $stdin.gets.chomp.split('')
    end

    check
    puts "NO"
  end

  def check
    flag = false
    @list.each do |elem|
      y = elem[0]
      x = elem[1]

      flag ||= right_up(y, x)
      flag ||= left_up(y, x)
      flag ||= left_up(y, x)
      flag ||= right_down(y, x)

      if flag
        puts "YES"
        exit
      end
    end
  end

  def right_up(ypos,xpos)
    white = 0
    black = 0
    if @cells[ypos][xpos] == "#"
      black += 1
    else
      white += 1
    end

    @pos1.each do |pos|
      y = pos[0]
      x = pos[1]

      if @cells[ypos+y][xpos+x] == "#"
        black += 1
      else
        white += 1
      end

    end
    if black >= 3 || white >= 3
      true
    else
      false
    end
  end

  def left_up(ypos,xpos)
    white = 0
    black = 0
    if @cells[ypos][xpos] == "#"
      black += 1
    else
      white += 1
    end

    @pos2.each do |pos|
      y = pos[0]
      x = pos[1]

      if @cells[ypos+y][xpos+x] == "#"
        black += 1
      else
        white += 1
      end

    end
    if black >= 3 || white >= 3
      true
    else
      false
    end
  end
  def left_down(ypos,xpos)
    white = 0
    black = 0
    if @cells[ypos][xpos] == "#"
      black += 1
    else
      white += 1
    end

    @pos3.each do |pos|
      y = pos[0]
      x = pos[1]

      if @cells[ypos+y][xpos+x] == "#"
        black += 1
      else
        white += 1
      end

    end
    if black >= 3 || white >= 3
      true
    else
      false
    end
  end

  def right_down(ypos,xpos)
    white = 0
    black = 0
    if @cells[ypos][xpos] == "#"
      black += 1
    else
      white += 1
    end

    @pos4.each do |pos|
      y = pos[0]
      x = pos[1]

      if @cells[ypos+y][xpos+x] == "#"
        black += 1
      else
        white += 1
      end
    end
    if black >= 3 || white >= 3
      true
    else
      false
    end
  end
end

a = A.new

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

A = [gets, gets, gets, gets]
def rr(t)
  if (t[0] == t[1] and t[1] == t[2] and t[2] == t[3]) then return true end
  t = t.chars.partition { |x| x == '.' }
  if t[0].length == 1 or t[0].length == 3 then return true end
  return false
end
def solve()
  i, j = 0, 0
  while i != 3
    if rr(A[i][j] + A[i][j+1] + A[i+1][j] + A[i+1][j+1]) then return "YES" end
    j = j+1
    if j == 3 then i, j = i+1, 0 end
  end
  return "NO"
end
puts solve()

a = 4.times.map { gets.chop }
3.times do |i|
  3.times do |j|
    if [a[i][j], a[i][j+1], a[i+1][j], a[i+1][j+1]].count('#') != 2
      puts 'YES'
      exit
    end
  end
end
puts 'NO'

lines = [""]*4
lines[0] = gets.chomp.split("")
lines[1] = gets.chomp.split("")
lines[2] = gets.chomp.split("")
lines[3] = gets.chomp.split("")

s = ""
t = ""
ok = false
(0..2).each {|l|
	(0..2).each {|c|
		s = "#{lines[l][c]}#{lines[l][c+1]}#{lines[l+1][c]}#{lines[l+1][c+1]}"
		t = s.gsub(".","")
		if t.length==4 or t.length==3 or t.length==1 or t.length==0
			ok=true
		end
	}
}

if ok
	puts "YES"
else
	puts "NO"
end

m = Array.new

4.times do
  m << gets.chomp
end

3.times do |y|
  3.times do |x|
    c = (m[y][x] + m[y+1][x] + m[y][x+1] + m[y+1][x+1]).delete(".").length
    unless c == 2
      puts 'YES'
      exit 0
    end
  end
end

puts 'NO'

A = [gets, gets, gets, gets]
def rr(t)
  t = t.chars.sort {|x,y| y <=> x}.join
  puts t
  if (t[0] == t[1] and t[1] == t[2] and t[2] == t[3]) then return true end
  if t[0] != t[1] then return true end
  return false
end
def solve()
  i, j = 0, 0
  while i != 3
    if rr(A[i][j] + A[i][j+1] + A[i+1][j] + A[i+1][j+1]) then return "YES" end
    j = j+1
    if j == 3 then i, j = i+1, 0 end
  end
  return "NO"
end
puts solve()

A = [gets, gets, gets, gets]
def rr(t)
  t = t.chars.sort {|x,y| y <=> x}.join
  if (t[0] == t[1] and t[1] == t[2] and t[2] == t[3]) then return true end
  if t[0] != t[1] then return true end
  return false
end
def solve()
  i, j = 0, 0
  while i != 3
    if rr(A[i][j] + A[i][j+1] + A[i+1][j] + A[i+1][j+1]) then return "YES" end
    j = j+1
    if j == 3 then i, j = i+1, 0 end
  end
  return "NO"
end
puts solve()

m = Array.new

4.times do
  m << gets.chomp
end

3.times do |y|
  3.times do |x|
    if (m[y][x] + m[y+1][x] + m[y][x+1] + m[y+1][x+1]).delete(".").length >= 3
      puts 'YES'
      exit 0
    end
  end
end

puts 'NO'


PolandBall is a young, clever Ball. He is interested in prime numbers. He has stated a following hypothesis: "There exists such a positive integer n that for each positive integer m number n·m + 1 is a prime number".Unfortunately, PolandBall is not experienced yet and doesn't know that his hypothesis is incorrect. Could you prove it wrong? Write a program that finds a counterexample for any n.

The only number in the input is n (1 ≤ n ≤ 1000) — number from the PolandBall's hypothesis.

Output such m that n·m + 1 is not a prime number. Your answer will be considered correct if you output any suitable m such that 1 ≤ m ≤ 103. It is guaranteed the the answer exists.

NoteA prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.For the first sample testcase, 3·1 + 1 = 4. We can output 1.In the second sample testcase, 4·1 + 1 = 5. We cannot output 1 because 5 is prime. However, m = 2 is okay since 4·2 + 1 = 9, which is not a prime number.

n = gets.chomp.to_i
case n
when 1
    puts 3
when 2
    puts 4
else
    puts (n - 2)
end

n = gets.to_i
puts n <= 2 ? n + 2 : n - 2

def prime(n)
	return false if n == 0 || n == 1

	i = 2
	while i * i <= n
		return false if n % i == 0
		i += 1
	end

	return true
end

def main
	n = gets.to_i

	for m in (1..1000)
		if not prime(n * m + 1)
			puts m
			break
		end
	end
end

main

require 'prime'
n = gets.strip.to_i
ans = 1
if n > 2
    (1..n).each do |i|
        if !Prime.prime?(n * i + 1)
            ans = i
            break
        end
    end
else
    ans = n + 2
end
puts ans

require 'prime'
n=gets.to_i
m=1
while Prime.prime?(n*m+1)
	m+=1
end
puts m

require 'prime'
n= gets.to_i

for i in 1..1000
  if !(Prime.prime?((i*n) + 1))
    print i
    break
  end
end

require 'prime'
n=gets.to_i
for m in 1..999
    unless (n*m+1).prime?
        p m
        break
    end
end

require 'prime'
N = gets.to_i
(1..1000).each { |i| next if Prime.prime? N * i + 1
   p i
   exit
}

require'prime'
N=gets.to_i
p (1..1000).find{|i|!Prime.prime?N*i+1}

n=gets.chomp.to_i
def is_prime(a)
    i=2
    while i*i<=a do
        return 0 if a % i == 0
        #ruby不支持++运算！
        i += 1
    end
    return 1
end
i=1
while true do
    #print "#{i} "
    #puts i*n+1
    #if i==5 break end
    #get_character
    #puts is_prime(i*n+1)

    if is_prime(i*n+1) == 1
        i+=1
    else
        puts i
        break
    end
end

#!/usr/bin/env ruby

require 'prime'

n = gets.chop.to_i

m = 1
while m < 10**3 do
  result = m * n + 1
  break unless result.prime?
  m += 1
end

puts m

def prime(n)
	return false if n == 0 || n == 1

	i = 2
	while i * i <= n
		return false if n % i == 0
		i += 1
	end

	return true
end

def main
	n = gets.to_i

	for m in (1..1000)
		if prime(n * m + 1)
			puts m
			break
		end
	end
end

main

require 'prime'

n = gets.strip.to_i
ans = 0
(1..n).each do |i|
    if !Prime.prime?(n * i + 1)
        ans = i
        break
    end
end
puts ans

require 'prime'

n = gets.strip.to_i
ans = 1
(1..n).each do |i|
    if !Prime.prime?(n * i + 1)
        ans = i
        break
    end
end
puts ans

require 'prime'
N = gets.to_i
(1..1000).each { |i| next if Prime.prime? N * i + 1
  at_exit { p i }
}

n=gets.chomp.to_i
def is_prime(a)
    i=2
    while i*i<=a do
        return 0 if a % i == 0
        #ruby不支持++运算！
        i += 1
    end
    return 1
end
i=1
while true do
    #print "#{i} "
    #puts i*n+1
    if i==5
        break
    end
    #get_character
    #puts is_prime(i*n+1)

    if is_prime(i*n+1) == 1
        i+=1
    else
        puts i
        break
    end
end


You know that Japan is the country with almost the largest 'electronic devices per person' ratio. So you might be quite surprised to find out that the primary school in Japan teaches to count using a Soroban — an abacus developed in Japan. This phenomenon has its reasons, of course, but we are not going to speak about them. Let's have a look at the Soroban's construction.  Soroban consists of some number of rods, each rod contains five beads. We will assume that the rods are horizontal lines. One bead on each rod (the leftmost one) is divided from the others by a bar (the reckoning bar). This single bead is called go-dama and four others are ichi-damas. Each rod is responsible for representing a single digit from 0 to 9. We can obtain the value of a digit by following simple algorithm:  Set the value of a digit equal to 0.  If the go-dama is shifted to the right, add 5.  Add the number of ichi-damas shifted to the left. Thus, the upper rod on the picture shows digit 0, the middle one shows digit 2 and the lower one shows 7. We will consider the top rod to represent the last decimal digit of a number, so the picture shows number 720.Write the program that prints the way Soroban shows the given number n.

The first line contains a single integer n (0 ≤ n < 109).

Print the description of the decimal digits of number n from the last one to the first one (as mentioned on the picture in the statement), one per line. Print the beads as large English letters 'O', rod pieces as character '-' and the reckoning bar as '|'. Print as many rods, as many digits are in the decimal representation of number n without leading zeroes. We can assume that number 0 has no leading zeroes.

#!/usr/bin/ruby
T=[
	'O-|-OOOO',
	'O-|O-OOO',
	'O-|OO-OO',
	'O-|OOO-O',
	'O-|OOOO-',
	'-O|-OOOO',
	'-O|O-OOO',
	'-O|OO-OO',
	'-O|OOO-O',
	'-O|OOOO-',
]
gets.chomp.reverse.chars{|e|puts T[e.to_i]}

str = gets.chomp
str.reverse.split(//).each do |c|
  if c.to_i >= 5
    print "-O|"
  else
    print "O-|"
  end
  tmp = (c.to_i - 5 < 0 ? c.to_i : c.to_i - 5)
  case tmp
  when 0 then
    puts "-OOOO"
  when 1 then
    puts "O-OOO"
  when 2 then
    puts "OO-OO"
  when 3 then
    puts "OOO-O"
  when 4 then
    puts "OOOO-"
  else
    puts "-OOOO"
  end
end

n = gets.chomp.to_i
digits = ["O-|-OOOO", "O-|O-OOO", "O-|OO-OO", "O-|OOO-O", "O-|OOOO-", "-O|-OOOO", "-O|O-OOO",
            "-O|OO-OO", "-O|OOO-O", "-O|OOOO-"]
ans = []
if n == 0
    puts digits[0]
else
    while n > 0
        last = n % 10
        ans << digits[last]
        n -= last
        n /= 10
    end
    ans.each do |a|
        puts a
    end
end

n = gets.chomp.to_i

puts "O-|-OOOO" if n == 0
while n != 0 do
  i = n%10
  row = ""
  if i < 5
    row += "O-|"
  else
    i = i - 5
    row +=  "-O|"
  end
  case i
  when 0
    row += "-OOOO"
  when 1
    row += "O-OOO"
  when 2
    row += "OO-OO"
  when 3
    row += "OOO-O"
  when 4
    row += "OOOO-"
  end
  puts row
  n = n/10
end

n = gets.chomp
rep = {0 => 'O-|-OOOO',
 1 => 'O-|O-OOO',
 2 => 'O-|OO-OO',
 3 => 'O-|OOO-O',
 4 => 'O-|OOOO-',
 5 => '-O|-OOOO',
 6 => '-O|O-OOO',
 7 => '-O|OO-OO',
 8 => '-O|OOO-O',
 9 => '-O|OOOO-'
}

arr = []
n.each_char do |c|
	arr << rep[c.to_i]
end
arr.reverse!
arr.each do |a|
	puts a
end

def rod(d)
    if d < 5
        print "O-"
    else
        print "-O"
        d -= 5
    end
    print "|"
    remain = 4-d
    while d > 0
        print "O"
        d -= 1
    end
    print "-"
    while remain > 0
        print "O"
        remain -= 1
    end
    puts ""
end

gets.strip.reverse.each_char do |c|
    d = c.to_i
    rod(d)
end

gets.chomp.split('').map { |x| x.to_i }.reverse.each { |x|
  left = x < 5 ? 'O-' : '-O'
  m = x % 5
  right = 'O' * m + '-' + 'O' * (4 - m)
  puts left + '|' + right
}

n = gets.chomp.to_i

def draw(i)
  str = ""
  if i >= 5
    str = "-O|"
  else
    str = "O-|"
  end

  (i % 5).times do
    str += "O"
  end

  str += "-"
  (4 - (i % 5)).times do
    str += "O"
  end
  puts str
end

if n == 0
  draw(n)
end

while n != 0
  draw(n % 10)
  n /= 10
end

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

gets.chop.reverse.soroban

# encoding: utf-8

str = gets.chomp
str.reverse.split(//).each do |c|
  if c.to_i >= 5
    print "-0|"
  else
    print "0-|"
  end
  tmp = (c.to_i - 5 < 0 ? c.to_i : c.to_i - 5)
  case tmp
  when 0 then
    puts "-0000"
  when 1 then
    puts "0-000"
  when 2 then
    puts "00-00"
  when 3 then
    puts "000-0"
  when 4 then
    puts "0000-"
  else
    puts "-0000"
  end
end

n = gets.chomp.to_i
digits = ["O-|-OOOO", "O-|O-OOO", "O-|OO-OO", "O-|OOO-O", "O-|OOOO-", "-O|-OOOO", "-O|O-OOO",
            "-O|OO-OO", "-O|OOO-O", "-O|OOOO-"]
ans = []
while n > 0
    last = n % 10
    ans << digits[last]
    n -= last
    n /= 10
end
ans.each do |a|
    puts a
end

n = gets.chomp.to_i
while n != 0 do
  i = n%10
  row = ""
  if i < 5
    row += "O-|"
  else
    i = i - 5
    row +=  "-O|"
  end
  case i
  when 0
    row += "-OOOO"
  when 1
    row += "O-OOO"
  when 2
    row += "OO-OO"
  when 3
    row += "OOO-O"
  when 4
    row += "OOOO-"
  end
  puts row
  n = n/10
end

gets.chomp.split('').map { |x| x.to_i }.reverse.each { |x|
  left = x < 5 ? '0-' : '-0'
  m = x % 5
  right = '0' * m + '-' + '0' * (4 - m)
  puts left + '|' + right
}

n = gets.chomp.to_i

def draw(i)
  str = ""
  if i >= 5
    str = "-0|"
  else
    str = "0-|"
  end

  (i % 5).times do
    str += "0"
  end

  str += "-"
  (4 - (i % 5)).times do
    str += "0"
  end
  puts str
end

while n != 0
  draw(n % 10)
  n /= 10
end

n = gets.chomp.to_i

def draw(i)
  str = ""
  if i >= 5
    str = "-O|"
  else
    str = "O-|"
  end

  (i % 5).times do
    str += "O"
  end

  str += "-"
  (4 - (i % 5)).times do
    str += "O"
  end
  puts str
end

while n != 0
  draw(n % 10)
  n /= 10
end


Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).

The single line contains three space-separated integers n, m and k (2 ≤ k ≤ n ≤ 109; 0 ≤ m ≤ n).

Print a single integer — the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).

NoteSample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.

require 'openssl'

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

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

while r < 0
	r += d
end

puts r

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

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

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

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

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

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

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

def release
  solve STDIN
end

release

require 'openssl'

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

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

while r < 0
	r += d
end

puts r

require 'openssl'

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

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

puts r

MOD = 10 ** 9 + 9
n, m, k = gets.chop.split.map(&:to_i)
extra = n - m
total = 0
if(extra * k >= n)
	print m % mod
else
	st = n - extra * k
	1.upto(st) do |i|
		total += 1
		if(i % k == 0)
			total *= 2
		end
	end
	total += extra * (k - 1)
	print total
end

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

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

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

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

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

module Containers
  class Stack

  end
end

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

def solve
  n, m, k = gets.split.map(&:to_i)
  t = (n / k) * (k - 1) + n % k
  if m <= t
    puts m
  else
    a = m - t
    puts k * ((1 << (a + 1)) - 2) + m - a * k
  end
end

solve


Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. Winnie has got three best friends: Rabbit, Owl and Eeyore, each of them lives in his own house. There are winding paths between each pair of houses. The length of a path between Rabbit's and Owl's houses is a meters, between Rabbit's and Eeyore's house is b meters, between Owl's and Eeyore's house is c meters.For enjoying his life and singing merry songs Winnie-the-Pooh should have a meal n times a day. Now he is in the Rabbit's house and has a meal for the first time. Each time when in the friend's house where Winnie is now the supply of honey is about to end, Winnie leaves that house. If Winnie has not had a meal the required amount of times, he comes out from the house and goes to someone else of his two friends. For this he chooses one of two adjacent paths, arrives to the house on the other end and visits his friend. You may assume that when Winnie is eating in one of his friend's house, the supply of honey in other friend's houses recover (most probably, they go to the supply store).Winnie-the-Pooh does not like physical activity. He wants to have a meal n times, traveling minimum possible distance. Help him to find this distance.

First line contains an integer n (1 ≤ n ≤ 100) — number of visits. Second line contains an integer a (1 ≤ a ≤ 100) — distance between Rabbit's and Owl's houses. Third line contains an integer b (1 ≤ b ≤ 100) — distance between Rabbit's and Eeyore's houses. Fourth line contains an integer c (1 ≤ c ≤ 100) — distance between Owl's and Eeyore's houses.

Output one number — minimum distance in meters Winnie must go through to have a meal n times.

NoteIn the first test case the optimal path for Winnie is the following: first have a meal in Rabbit's house, then in Owl's house, then in Eeyore's house. Thus he will pass the distance 2 + 1 = 3.In the second test case Winnie has a meal in Rabbit's house and that is for him. So he doesn't have to walk anywhere at all.

n = gets.strip.to_i
a = gets.strip.to_i
b = gets.strip.to_i
c = gets.strip.to_i
curr = 1
ans = 0
if n > 1
    i = 1
    while i < n
        dist = 0
        case curr
        when 1
            if a <= b
                dist = a
                curr = 2
            else
                dist = b
                curr = 3
            end
        when 2
            if a <= c
                dist = a
                curr = 1
            else
                dist = c
                curr = 2
            end
        when 3
            if c <= b
                dist = c
                curr = 2
            else
                dist = b
                curr = 1
            end
        end
        ans += dist
        i += 1
    end
end
puts ans

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

if n==1
    p 0;exit
end
len = 0

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

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

p len

#! /usr/bin/env ruby

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

n -= 1

ans = 0

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

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

puts ans

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

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

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

if n==1
    p 0
end
len = 0

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

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

p len

#! /usr/bin/env ruby

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

n -= 1

ans = 0

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

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

puts ans

n = gets.to_i
a = gets.to_i
b = gets.to_i
c = gets.to_i
n -= 1
x = [0, a, b, c].sort
tot = (n / 3) * (a + b + c)
for i in 1 .. 3
  x[i] += x[i - 1]
end
tot += x[n % 3]
puts tot

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


You want to arrange n integers a1, a2, ..., an in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers.More formally, let's denote some arrangement as a sequence of integers x1, x2, ..., xn, where sequence x is a permutation of sequence a. The value of such an arrangement is (x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn).Find the largest possible value of an arrangement. Then, output the lexicographically smallest sequence x that corresponds to an arrangement of the largest possible value.

The first line of the input contains integer n (2 ≤ n ≤ 100). The second line contains n space-separated integers a1, a2, ..., an (|ai| ≤ 1000).

Print the required sequence x1, x2, ..., xn. Sequence x should be the lexicographically smallest permutation of a that corresponds to an arrangement of the largest possible value.

NoteIn the sample test case, the value of the output arrangement is (100 - ( - 50)) + (( - 50) - 0) + (0 - 50) + (50 - ( - 100)) = 200. No other arrangement has a larger value, and among all arrangements with the value of 200, the output arrangement is the lexicographically smallest one.Sequence x1, x2, ... , xp is lexicographically smaller than sequence y1, y2, ... , yp if there exists an integer r (0 ≤ r < p) such that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1.

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

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

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

run if $0 == __FILE__

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

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

    puts answer.join(' ')
  end
end

a = A.new


The only difference between the easy and the hard versions is constraints.A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters.In one move you can take any subsequence $$$t$$$ of the given string and add it to the set $$$S$$$. The set $$$S$$$ can't contain duplicates. This move costs $$$n - |t|$$$, where $$$|t|$$$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).Your task is to find out the minimum possible total cost to obtain a set $$$S$$$ of size $$$k$$$ or report that it is impossible to do so.

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 100$$$) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters.

Print one integer — if it is impossible to obtain the set $$$S$$$ of size $$$k$$$, print -1. Otherwise, print the minimum possible total cost to do it.

NoteIn the first example we can generate $$$S$$$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $$$S$$$ is $$$0$$$ and the cost of the others is $$$1$$$. So the total cost of $$$S$$$ is $$$4$$$.

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

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

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
n,k=gsmi
s=gs
l0=s.length

cnt=0
l=l0
k-=1
prev=[s]
while k>0 do
  ar=[]
  prev.each do |pr|
    l.times do |i|
      st=pr[0,i]+pr[(i+1)..-1]
      ar<<st
    end
  end
  ar.uniq!
  if k<=ar.size
    cnt+=k*(l0-(l-1))
    break
  end
  cnt+=ar.size*(l0-(l-1))
  k-=ar.size
  l-=1
  prev=ar
  putsend -1 if l==0&&k>=1
end

puts cnt


You've got a 5 × 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix:  Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≤ i < 5).  Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≤ j < 5). You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful.

The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one.

Print a single integer — the minimum number of moves needed to make the matrix beautiful.

r1 = gets.chomp
row1 = r1.split(" ")
r2 = gets.chomp
row2 = r2.split(" ")
r3 = gets.chomp
row3 = r3.split(" ")
r4 = gets.chomp
row4 = r4.split(" ")
r5 = gets.chomp
row5 = r5.split(" ")

spaces = 0
if row1.include? "1"
	if row1[2] == "1"
		spaces = 0
	elsif row1[0] == "1" || row1[4] == "1"
		spaces += 2
	else
		spaces += 1
	end
	spaces += 2
elsif row2.include? "1"
	if row2[2] == "1"
		spaces = 0
	elsif row2[0] == "1" || row2[4] == "1"
		spaces += 2
	else
		spaces += 1
	end
	spaces += 1
elsif row3.include? "1"
	if row3[2] == "1"
		spaces = 0
	elsif row3[0] == "1" || row3[4] == "1"
		spaces += 2
	else
		spaces += 1
	end
elsif row4.include? "1"
	if row4[2] == "1"
		spaces = 0
	elsif row4[0] == "1" || row4[4] == "1"
		spaces += 2
	else
		spaces += 1
	end
	spaces += 1
else
	if row5[2] == "1"
		spaces = 0
	elsif row5[0] == "1" || row5[4] == "1"
		spaces += 2
	else
		spaces += 1
	end
	spaces += 2
end

puts spaces

mat = []
5.times{
	mat << STDIN.readline.split
}

(0..4).each{|i|
	(0..4).each{|j|
		if mat[i][j] == '1'
			p (i-2).abs+(j-2).abs
		end
	}
}

arr = []
i = 0
j = 0
5.times {
  arr.push(gets.split(" ").map(&:to_i))
}
arr.each_with_index do |row,i_ind|
  val = row.index(1)
  i = i_ind and j = val if val
end
puts (i - 2).abs + (2 - j ).abs

5.times { |row|
  c = gets.chomp.gsub(" ", "").index("1")
  puts (2-row).abs + (2-c).abs if c
}

# https://codeforces.com/problemset/problem/263/A
0.upto(4) do |i|
  line = gets.chomp
  chars = line.split(' ')
  chars.each_with_index do |c, j|
    if c == '1'
      p (2 - i).abs + (2 - j).abs
    end
  end
end

l = []
(1..5).each { l << gets.split.map(&:to_i) }

x = 0
y = 0

l.each_with_index { |i, in1|
	i.each_with_index { |j, in2|
			if j == 1
				x = in2
				y = in1
				break
			end
		}
	}

puts (x - 2).abs + (y - 2).abs

i = 1
row_num = 0
output = 0
while i <= 5
  input_row = gets.chomp.split
  if input_row.include?("1") then
    input_row = input_row.map{|i| i.to_i}
    colum_num = input_row.index{|i| i == 1}
    row_num = i
    colum_num = colum_num +1
  end
  i = i+1
end
if 3 > row_num then
  output = 3 - row_num
elsif 3 < row_num then
    output = row_num -3
end
if 3 > colum_num then
  output = 3 - colum_num + output
elsif 3 < colum_num then
  output = colum_num - 3 + output
end
print output

line1 = gets.chomp.split(" ")
line2 = gets.chomp.split(" ")
line3 = gets.chomp.split(" ")
line4 = gets.chomp.split(" ")
line5 = gets.chomp.split(" ")

i = 0
j = 0
if line1.index("1")
	j = line1.index("1")
	i = 0
end

if line2.index("1")
	j = line2.index("1")
	i = 1
end

if line3.index("1")
	j = line3.index("1")
	i = 2
end

if line4.index("1")
	j = line4.index("1")
	i = 3
end

if line5.index("1")
	j = line5.index("1")
	i = 4
end

puts "#{(i-2).abs+(j-2).abs}"

5.times do |i|
  j=gets.split.find_index(?1)
  puts (i-2).abs+(j-2).abs if j
end

5.times {|i|
  j=gets.split.index(?1)
  puts (i-2).abs+(j-2).abs if j
}

a = Array.new(5)
5.times do |i|
  a[i] = gets.split.map(&:to_i)
end
xi = 0
xj = 0
5.times do |i|
  5.times do |j|
    if a[i][j] == 1
      xi = i + 1
      xj = j + 1
      break
    end
  end
end
puts (xi - 3).abs + (xj - 3).abs

total = 0
5.times{|i|
	j = gets.split(' ').index(?1)
	puts (i-2).abs + (j-2).abs if j
}

5.times do |i|
    j = gets.split.index("1")
    if j
        puts (2 - i).abs + (2 - j).abs
        break
    end
end

r=0
c=0
1.upto(5) do |i|
a=gets.chomp
if a.count("1")==1
r=i
a.each_char do |k|
if k=="0" || k=="1"
c+=1
end
break if k== "1"
end
end
end
ans= (r-3).abs+(c-3).abs

puts "#{ans}"

def check (n,r)
	if n.include?(1)
		puts (3-r).abs + (3-(n.index(1)+1)).abs
	end
end

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

y = []
y = gets.split.map(&:to_i)

z = []
z = gets.split.map(&:to_i)

a = []
a = gets.split.map(&:to_i)

b = []
b = gets.split.map(&:to_i)

check(x,1)
check(y,2)
check(z,3)
check(a,4)
check(b,5)

arr_input = []
i, j = -1
5.times do |n|
 	arr = gets.chomp
 	arr_input << arr
end

arr_input.each.with_index do |row, index|
	row.split(' ').each.with_index do |col, idx|
		if col.to_i == 1
			i = index
			j = idx
		end
	end
end
min_distance = (2 - i).abs + (2 - j).abs

puts min_distance

arr_input = []
i, j = -1
5.times do |n|
 	arr = gets.chomp
 	arr_input << arr
 	arr.split(' ').each.with_index do |col, idx|
		if col.to_i == 1
			i = n
			j = idx
		end
	end
end
min_distance = (2 - i).abs + (2 - j).abs

puts min_distance

5.times do |i|
    a = gets.split.map(&:to_i)
    5.times do |j|
        if a[j] == 1
            puts (2 - i).abs() + (2 - j).abs()
            exit 0
        end
    end
end

#!/usr/bin/env ruby

5.times do |i|
	gets.split.each_with_index do |v, j|
		if v == "1"
			puts (i - 2).abs + (j - 2).abs
			exit 0
		end
	end
end

n = 0
5.times.each_with_index do |row, ind|
  line = gets.chomp.match(/1/)
  if line
    n = (ind - 2).abs + (line.begin(0) / 2 - 2).abs
  end
end
puts n

include Math
BEGIN{
	def gint()
		return gets.split.map(&:to_i)
	end
	def main()
		a = []
		x = y = 0
		5.times do |i|
			a[i] = gint
			a[i].each_with_index do |ta,w|
				if ta == 1 then
					x, y = i, w
				end
			end
		end
		puts (x-2).abs + (y-2).abs
	end
	Test_case = 1
	Test_case.times do
		main()
	end
}

line = 0
pos = 0
while true
  line += 1
  pos = gets.split.map(&:to_i).index(1)
  if pos != nil
    break
  end
end
pos += 1
puts (3-line).abs + (3-pos).abs

_2d = []
5.times{ _2d = _2d + [gets.chomp.split.map(&:to_i)]}
for i in (0...5) do
  for j in (0...5) do
    unless _2d[i][j].zero?
      puts (3-(i+1)).abs + (3-(j+1)).abs
    end
  end
end

t = 5

t.times do |i|
	s = gets.split.collect{|x| x.to_i}

	s.each_index{|j|
		if s[j] == 1
			puts (i-2).abs + (j-2).abs
		end
	}
end

x=nil
y=nil

5.times do |i|
  a=gets.split.map{|e|e.to_i}
  n=a.index 1
  if n
    x=n
    y=i
    break
  end
end

puts (2-x).abs+(2-y).abs

matrix = []
row, column = 0, 0

(0...5).each do |iterNum|
	matrix << gets.chomp.split(' ').map {|item| item.to_i}
	row, column = iterNum, matrix[iterNum].index(1) unless matrix[iterNum].index(1).nil?
end

puts (2 - row).abs + (2 - column).abs

ar = []
5.times do
  ar << gets.split(' ').map(&:to_i)
end
row = ar.map{|x| x.inject(:+)}.index(1)
col = ar[row].index(1)
puts (row-2).abs+(col-2).abs

def abs(x)
    if x < 0 then
        return -x
    else
        return x
    end
end

n = 0
m = 0
matrix = []
5.times do |i|
    matrix[i] = gets.split
    if q = matrix[i].index("1") then
        n = i + 1
        m = q + 1
    end
end

result = abs(n-3) + abs(m-3)
puts result

5.times do |i|
  j = gets.split.index(?1)
  puts (i-2).abs + (j-2).abs if j
end

matrix = [] ; 5.times{ matrix << gets.chomp.split(" ").map(&:to_i) }
matrix.each_with_index do |line, i|
  if line.include?(1)
    count_heigth = (3 - (i+1)).abs
    line.each_with_index do |element, index|
      if element == 1
        count_element = (3 - (index+1)).abs
        puts count_heigth + count_element
      end
    end
  end
end

def read_tokens
  return gets.chomp.to_s.strip.split(' ')
end

def read_ints
  data = []
  read_tokens.each { |s|
    data.push(s.to_i)
  }
  return data
end

r_1, c_1 = 0, 0
(0...5).each { |r|
  row = read_ints
  c = row.find_index(1)
  if c != nil
    r_1, c_1 = r, c
  end
}

def solve(row, col)
  dest_r, dest_c = 2, 2
  return (dest_r - row).abs + (dest_c - col).abs
end

print(solve(r_1, c_1))

f = false
m = 0
0.upto 4 do |t|
	if !f
		a = gets.chomp.split(" ")
		if a.include? "1"
			m += (2 - t).abs
			m += (2 - a.index("1")).abs
			f = true
		end
	else
		gets
	end
end
puts m

a=Array.new
s=1
5.times do
j=0
a=gets.split.map(&:to_i)
for i in 0..4 do
if a[i]==1 then
puts (s-3).abs+(i+1-3).abs
j=1
end
end
break if j== 1
s+=1
end

x, y = 0, 0
5.times do |i|
  a = gets.split(" ").map{|x| x.to_i}
  x, y  = i, a.index(1) if a.include?(1)
end
puts (2 - x).abs + (2 - y).abs

# -*- encoding: utf-8 -*-

x, y = 0, 0

for i in 0 .. 4 do
  cols = gets.chomp.split(" ").map{|n| n.to_i}

  for j in 0 .. 4 do
    if cols[j] == 1
      x = j
      y = i
      break
    end
  end
end
puts (2 - x).abs + (2 - y).abs

(1..5).each do |i|
  m = gets.split(/\s+/).map(&:to_i)
  i_th = m.index(1)
  unless i_th.nil?
    movements = (3-(i_th+1)).abs + (3-i).abs
    puts movements
    break
  end
end

m = 0
n = 0
for i in 0...5
    x = gets.split.map(&:to_i)
    for j in 0...5
        if x[j]==1
            m = j
            n = i
            break
        end
    end

end

puts (n-2).abs + (m-2).abs

#!/usr/bin/ruby1.9

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

x = 0
y = 0
(0...5).each do |i|
  a = []
  a = gets.split.map &:to_i
  (0...5).each do |j|
    if a[j] == 1
      x = i + 1
      y = j + 1
    end
  end
end

puts (3 - x).abs + (3 - y).abs

for i in 1..5
	str = gets.chomp
	if str.include? "1"
		y = i
		x = (((" " + str).index "1") + 1) / 2
	end
end
puts (x - 3).abs + (y - 3).abs

matrix = []
5.times do
  row_string = gets.strip
  row = row_string.split(' ').map(&:to_i)
  matrix.push(row)
end

row = nil
column = nil
5.times do |row_index|
  5.times do |column_index|
    next unless matrix[row_index][column_index].eql?(1)

    row = row_index
    column = column_index
    break
  end
end

diff_row = (row - 2).abs
diff_column = (column - 2).abs
result = diff_row + diff_column
puts result

a=[]
i=0
x=0
y=0
5.times do
	row=gets.chomp.split(' ')
	#print row
	#puts
	row.each_index do |char|
		#puts row[char]
		if row[char]=='1'
			x=i
			y=char
		end
	end
	a.push(row)
	i+=1
end
#puts '('+x.to_s+','+y.to_s+')'
puts (x-2).abs+(y-2).abs

arr = []
x = nil
y = nil

5.times do |tx|
  arr << gets.chomp.split(' ')
  y ||= arr.last.index '1'
  (x ||= tx) if y
end

puts (x-2).abs + (y-2).abs

coco = 0
moco = 0
5.times do |x|
    a = gets.split.map(&:to_i)
    if a.include? 1
        coco = x
        moco = a.index(1)
    end
end

puts (coco - 2).abs + (moco - 2).abs

i,j = nil, nil
(0..4).each do |k|
    a = $stdin.readline.split.map(&:to_i)
    t = a.find_index(1)
    if !t.nil?
        i,j=k,t
    end
end

di = (2-i).abs
dj = (2-j).abs

puts di+dj

a = Array.new(5, Array.new(5))
ii, jj = 0, 0;
for i in 0...5
  a[i] = gets.split(" ").map(&:to_i)
  for j in 0...5
    if a[i][j] == 1
      ii ,jj = i, j;
    end
  end
end
puts (ii - 2).abs + (jj  - 2).abs

def run
  ARGF.readlines.each_with_index do |line, i|
    line.chomp.split(" ").each_with_index do |c, j|
      if c == "1"
        puts (i - 2).abs + (j - 2).abs
        return
      end
    end
  end
end

run if $0 == __FILE__

# cook your code here
arr = Array.new(5,Array.new(5))
one_row =0
one_col=0
(0..4).each do |row|
    val = gets.split(" ").map(&:to_i)
    (0..4).each do |col|
        arr[row][col] = val[col]
        if val[col] == 1
            one_row = row
            one_col = col
        end
    end
end
puts (one_row - 2).abs  + (one_col-2).abs

inp = Array.new
5.times do
  inp << gets.chomp
end
inp.each do |str|
  if str =~ /^.*1.*$/
    str.scan(/1/) { |ch| puts (inp.index(str) - 2).abs + (str.delete(" ").index(ch) - 2).abs }
  end
end

#!/usr/bin/env ruby -w

v = (1..5).map do
  gets.split(/\s+/).map(&:to_i)
end

found = false
(0..4).each do |i|
  break if found
  (0..4).each do |j|
    if v[i][j] == 1
      puts (i - 2).abs + (j - 2).abs
      found = true
      break
    end
  end
end

grids = []
one_id = nil
5.times do |i|
  line = gets.split.map(&:to_i)
  one_id = line.index(1)
  if one_id
    puts (i - 2).abs + (one_id - 2).abs
  end
end

matrix = []
pos_one = [-1, -1]
beautiful_pos = [3, 3]
(1..5).each do |index|
    row = gets.chomp.split(/ /).map(&:to_i)
    matrix << row
    pos_one = [index, row.index( 1 ) + 1] if row.include? 1
end
puts (pos_one[0] - beautiful_pos[0]).abs + (pos_one[1] - beautiful_pos[1]).abs

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

matrix = []
5.times do
  matrix << gets.strip.split.map(&:to_i)
end

x, y = 0, 0
catch (:done) do
  for i in 0...5 do
    for j in 0...5 do
      if matrix[i][j] == 1
        x, y = i, j
        throw :done
      end
    end
  end
end

moves = (x - 2).abs + (y - 2).abs
puts moves

5.times do|i|
j=gets.split.index(?1)and puts (i-2).abs+(j-2).abs
end

1.upto(5) { |i| gets.chomp.split.each_with_index { |x,j| puts (i-3).abs + (j-2).abs if x == '1'} }

$<.each_with_index{|x,r|c=x.split.index('1');(puts (r-2).abs+(c-2).abs; break) if c}

# http://codeforces.com/problemset/problem/263/A

rows = []

one_row_index = -1
one_col_index = -1

5.times do |t|
  row = gets.chomp.split
  if row.include? "1"
    one_row_index = t
    one_col_index = row.find_index("1")
  end
  rows << row
end

puts "#{(2 - one_row_index).abs + (2 - one_col_index).abs}"

runner = 0
height = 0
width = 0
while line = gets.to_s.strip do
  break if line == ''
  columns = line.split(' ')
  if columns.include? '1'
    width = columns.index '1'
    height = runner
  end
  runner += 1
end

needed_height = 2
needed_width = 2

puts (needed_width - width).abs + (needed_height - height).abs

n = Array.new
for i in 0...5
    n[i] = Array.new
end
for i in 0...5
    input = gets.chomp
    for j in 0...5
        nums = input.split
        n[i][j] = Integer(nums[j])
    end
end

a = 0
b = 0
for i in 0...5
    for j in 0...5
        if n[i][j] == 1
            a = i
            b = j
        end
    end
end

if a >= 2 && b >= 2
    puts a - 2 + b - 2
elsif a < 2 && b >= 2
    puts 2 - a + b - 2
elsif a < 2 && b < 2
    puts 2 - a + 2 - b
else  a >= 2 && b < 2
    puts  a - 2 + 2 - b
end

l = []
(1..5).each { l << gets.split.map(&:to_i) }

x = 0
y = 0

l.each_with_index { |i, in1|
	i.each_with_index { |j, in2|
			if j == 1
				x = in2
				y = in1
				break
			end
		}
	}

puts (x - 3).abs + (y - 3).abs

_2d = []
5.times{ _2d = _2d + gets.chomp.split.map(&:to_i)}
_2d.each_with_index{|x, index| puts ((index+1)-13).abs unless x.zero?}

5.times do |i|
  j = gets.split.find_index(1)
  puts (i-2).abs + (j-2).abs if j
end

x = 0
y = 0
(0...5).each do |i|
  a = []
  a = gets.split.map &:to_i
  (0...5).each do |j|
    if a[j] == 1
      x = i
      y = j
    end
  end
end
puts (3 - x).abs + (3 - y).abs

a=[]
i=0
x=0
y=0
5.times do
	row=gets.chomp.split(' ').map(&:to_i)
	row.each_index do |char|
		if char==1
			x=i
			y=char
		end
	end
	a.push(row)
	i+=1
end
puts (x+y-4).abs

a=[]
i=0
x=0
y=0
5.times do
	row=gets.chomp.split(' ').map(&:to_i)
	row.each_index do |char|
		if char==1
			x=i
			y=char
		end
	end
	a.push(row)
	i+=1
end
puts (x-2).abs+(y-2).abs

arr = []
x = nil
y = nil

5.times do |tx|
  arr << gets.chomp.split(' ')
  y ||= arr.last.index '1'
  (x ||= tx) if y
end

puts "COORDS: " + x.to_s + " " + y.to_s
puts (x-2).abs + (y-2).abs

i,j = nil, nil
(0..4).each do |k|
    a = $stdin.readline.split.map(&:to_i)
    t = a.find_index(1)
    if !t.nil?
        i,j=k,t
    end
end

di = (2-i).abs
dj = (2-i).abs

puts 1+di+dj

a=gets.chomp.split(" ").map {|i| i.to_i}
b=gets.chomp.split(" ").map {|i| i.to_i}
b.sort!
if a[0]-a[1]>=0
c=b[a[0]-a[1]]
puts "#{c} #{c-1}"
else
puts "-1"
end

n = Array.new
for i in 0...5
    n[i] = Array.new
end
for i in 0...5
    input = gets.chomp
    for j in 0...5
        nums = input.split
        n[i][j] = Integer(nums[j])
    end
end

a = 0
b = 0
for i in 0...5
    for j in 0...5
        if n[i][j] == 1
            a = i
            b = j
        end
    end
end

if a > b
    puts a - b
elsif a == 0 && b == 0
    puts 4
elsif a == 1 && b == 1
    puts 2
elsif a == 2 && b == 2
    puts 0
elsif a == 3 && b == 3
    puts 2
elsif a == 4 && b == 4
    puts 4

else
    a < b
    puts  b - a
end


Amr loves Geometry. One day he came up with a very interesting problem.Amr has a circle of radius r and center in point (x, y). He wants the circle center to be in new position (x', y').In one step Amr can put a pin to the border of the circle in a certain point, then rotate the circle around that pin by any angle and finally remove the pin.Help Amr to achieve his goal in minimum number of steps.

Input consists of 5 space-separated integers r, x, y, x' y' (1 ≤ r ≤ 105,  - 105 ≤ x, y, x', y' ≤ 105), circle radius, coordinates of original center of the circle and coordinates of destination center of the circle respectively.

Output a single integer — minimum number of steps required to move the center of the circle to the destination point.

NoteIn the first sample test the optimal way is to put a pin at point (0, 2) and rotate the circle by 180 degrees counter-clockwise (or clockwise, no matter).

r, x, y, x_, y_ = gets.split.map &:to_i

d = Math.sqrt(((x_ - x) ** 2) + ((y_ - y) ** 2))

puts (d / (2 * r).to_f).ceil

r,x,y,x1,y1 = gets.chomp.split

r = r.to_f
x = x.to_f
y = y.to_f
x1= x1.to_f
y1= y1.to_f

l = Math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1))
l = l/(2*r)
l = l.ceil

puts l.to_i

#!/usr/bin/env ruby
r,*_=gets.split.map(&:to_f)
d=Math.hypot(*_.each_slice(2).to_a.transpose.map{|e|e.reduce(:-)})
p (d/r/2).ceil

def run
  r, x, y, xp, yp = $stdin.gets.split.map(&:to_i)

  dx = xp - x
  dy = yp - y

  n = (Math.sqrt(dx * dx + dy * dy) / (r * 2)).ceil
  puts n
end

run if $0 == __FILE__

r,x1,y1,x2,y2=gets.split.map{|e| e.to_f}
r2=Math.hypot(x1-x2,y1-y2)
r4=r*2
if r2==0 then
	puts 0
else
	r3=r2.to_i
	ans=(r2/r4).floor
	#p [r2,r4,ans]
	d=r2-r4*ans
	if d>0 && d<r4 then
		ans+=1
	end
	puts ans.to_i
end

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

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

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

def stop
	puts "stop"
	sleep(2)
end
def readarr
	gets.split.map{|i| i.to_i}
end
def write(foo)
	if foo.is_a?(Array)
		puts foo.map{|i| i.to_s}.join(" ")
	else
		puts foo
	end
end
r, x, y, x1, y1 = readarr
N = ((x1-x)**2+(y1-y)**2)
l = (4*r*r)
ans =(N/l)**0.5
if(ans*ans*l==N)
	puts ans.to_i
else
	puts ans.to_i+1
end
#sleep(300)

r, x, y, xd, yd = gets.split(" ").map &:to_f

len = Math.sqrt((xd - x)**2 + (yd - y)**2)

puts (len / (2 * r)).ceil

#!ruby

r, x0, y0, x1, y1 = gets.split.map{|e| e.to_i}

distance = Math.sqrt((x1-x0)**2 + (y1-y0)**2)

count = 0

while ( distance > 0 )
  count += 1
  distance -= r*2
end

puts count

r,x1,y1,x2,y2=gets.split.map{|e| e.to_f}
r2=Math.hypot(x1-x2,y1-y2)
r4=r*2
if r2==0 then
	puts 0
else
	r3=r2.to_i
	ans=(r2/r4).floor
	#p [r2,r4,ans]
	d=r2-r4*ans
	if ans==0 then
		ans=2
	elsif d>0 && d<r4 then
		ans+=1
	end
	puts ans.to_i
end

# coding: utf-8

# if __FILE__ == $0
# end

r, x1, y1, x2, y2 = gets.split.map(&:to_i)
step = 2*r

puts (x1 - x2).abs/step + (y1 - y2).abs/step

def stop
	puts "stop"
	sleep(2)
end
def readarr
	gets.split.map{|i| i.to_i}
end
def write(foo)
	if foo.is_a?(Array)
		puts foo.map{|i| i.to_s}.join(" ")
	else
		puts foo
	end
end
r, x, y, x1, y1 = readarr
ans = ((x1-x)**2+(y1-y)**2)**(0.5)/(2*r)
if ans-ans.to_i<1e-9
	puts ans.to_i
else
	puts ans.to_i+1
end
#sleep(300)