def convert str, b
  num = 0
  for i in 0..str.length-1
    num += (str[i].to_i(36) * (b**(str.length-1 - i)).to_i)
  end
  return num
end

time = gets
m = /(.*):(.*)/.match(time)
hour = m[1]
minute = m[2]

max = 1
hour.each_char {|c| max = [c.to_i(36), max].max}
minute.each_char {|c| max = [c.to_i(36), max].max}
infinite = false
bases = []
for i in max + 1..60
  if(!(convert(hour,i) < 0 || convert(hour,i) > 23  || convert(minute, i) < 0 || convert(minute, i) > 59))
    bases << i
	if(i < 60 && convert(hour,i) == convert(hour, i + 1) && convert(minute, i) == convert(minute, i + 1))
	  infinite = true
	  break
	end
  end
end

if infinite
  puts -1
elsif bases.length > 0
  puts bases.join(" ")
else
  puts 0
end


n,m = 2.times.map{gets.to_i}
a = n.times.map{gets.to_i}
mx = a.max
p = 0
for i in 0..n-1
  p += mx-a[i]
end
puts "#{([m-p,0].max+n-1)/n+mx} #{mx+m}"


n = gets.to_i
s = gets.chomp.split ''
c = 0
s.each do |x|
  c += x == '+'  ? 1 : -1
  c = [c, 0].max
end
puts c


def dot(x0, y0, x1, y1, x2, y2)
	(x1 - x0) * (x2 - x0) + (y1 - y0) * (y2 - y0)
end

def right(x0, y0, x1, y1, x2, y2)
	[x0, y0] != [x1, y1] and [x0, y0] != [x2, y2] and [x1, y1] != [x2, y2] and
		dot(x0, y0, x1, y1, x2, y2) == 0 ||
		dot(x1, y1, x0, y0, x2, y2) == 0 ||
		dot(x2, y2, x0, y0, x1, y1) == 0
end

def gao(a)
	return "RIGHT" if right(*a)
	a.each_index do |i|
		a[i] += 1
		return "ALMOST" if right(*a)
		a[i] -= 2
		return "ALMOST" if right(*a)
		a[i] += 1
	end
	"NEITHER"
end

puts gao gets.split.map &:to_i


n, a = gets.split.map(&:to_i)
res = 0
if (a.to_i.odd?)
    res = (a+1)/2
else
    res = (n-a)/2+1
end
print res


n = gets.to_i
s = gets.chomp
max = 0
for i in 0..n / 2
	max = s[0..i] == s[i + 1..i * 2 + 1] ? i : max
end
puts n - max


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

numbers.each do |n|
  result = result - [n]
  result.push(n)
end

puts result.size
puts result.join(' ')


W=%W(monday tuesday wednesday thursday friday saturday sunday)
n=(-W.index(gets.chomp)+W.index(gets.chomp))%7
puts [0,2,3].include?(n) ? :YES : :NO


s = gets.to_i
h, m = gets.split.map(&:to_i)
res = 0
ti = h*60+m
until /7/.match("#{ti / 60 % 24}#{ti % 60}")
  ti-=s
  res+=1
end
p res


MOD = 10**9 + 7
def modpow(x,n)
  ret = 1
  while n > 0
    ret = ret*x % MOD if n&1 == 1
    x = x*x % MOD
    n >>= 1
  end
  return ret
end

n,m = gets.split.map(&:to_i)
mx = [n,m].max
dp = Array.new(mx+1){[0,0]}
dp[1] = [2,0]
1.upto(mx-1) do |i|
  dp[i+1][0] = (dp[i][0] + dp[i][1]) % MOD
  dp[i+1][1] = (dp[i][0]) % MOD
end
sumn = (dp[n][0] + dp[n][1]) % MOD
summ = (dp[m][0] + dp[m][1]) % MOD
ans = (sumn + summ - 2) % MOD
p ans


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

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


def gao(n)
    unless $h[n]
        t = 1
        d = 2
        while d * d <= n
            if n % d == 0
                t = [t, gao(d), gao(n / d)].max
            end
            d += 1
        end
        $h[n] = n + t
    end
    return $h[n]
end

$h = {1 => 1}
n = gets.to_i
p gao(n)


t = gets.to_i
ans = []
t.times do
  x = gets.chomp
  len = x.length
  cnt = 0
  cnt += 10 * (x[0].to_i - 1)
  cnt += len * (len + 1) / 2
  ans << cnt
end
puts ans


# http://codeforces.com/contest/588/problem/B
# B. Duff in Love
require 'prime'

n = gets.chomp.to_i
puts n.prime_division.map(&:first).inject(1, :*)


require 'set'

$n = gets.to_i
$a = gets.split.map(&:to_i)
$memo = Set.new
$map = Array.new(300){Array.new(300)}
$delta = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]

def explode(x, y, lv, dir)
  return if lv >= $n
  k = ((x * 301 + y) * 31 + lv + 1) * 8 + dir
  return unless $memo.add?(k)
  dx, dy = $delta[dir]
  $a[lv].times do
    x += dx
    y += dy
    $map[x][y] = true
  end
  explode(x, y, lv + 1, (dir + 1) % 8)
  explode(x, y, lv + 1, (dir - 1) % 8)
end

explode(150, 150, 0, 0)

p $map.inject(0){|sum, x| sum + x.count(true)}


n=gets.chomp.to_i
s=gets.chomp

len, num=0, 0
s.chars.each do |c|
  if c=='x'
    if len==2
      num+=1
    else
      len+=1
    end
  else
    len=0
  end
end

puts num


vp,vd,t,f,c=gets.to_i,gets.to_i,gets.to_i,gets.to_i,gets.to_i
if vp>=vd then
        puts 0
        exit
end
time=Rational(t,1)
for ans in 0..100000
        #t*vp+time*vp=(vd-vp)*t
        #t*(vp-vd)=-vp*time
        tmp=Rational(-vp*time,vp-vd)
        #p [tmp,vp,vd]
        if tmp+time>=Rational(c,vp) then
                puts ans
                exit
        end
        time+=tmp*2+f
end


n = gets.chomp.to_i

l = 0
r = 10**8
while 1 < r - l
  c = (l + r) / 2
  if c * (c + 1) / 2 < n
    l = c
  else
    r = c
  end
end

puts n - l * (l + 1) / 2


n = gets.chomp.to_i

total = 0
$finite = true
a = gets.chomp.split.map(&:to_i)

def points2(a, b)
  case a+b
  when 3 then 3
  when 4 then 4
  when 5 then
    $finite = false
    0
  end
end

def points3(a,b,c)
  if a== 3 && b == 1 && c == 2
    2
  else
    points2(b,c)
  end
end

total += points2(a[0], a[1])
a.each_cons(3) do |a,b,c|
  total += points3(a,b,c)
  break unless $finite
end

if $finite
  puts "Finite"
  puts total
else
  puts "Infinite"
end


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

$n1=input[0]
$n2=input[1]
$k1=input[2]+1
$k2=input[3]+1
$dp=Array.new(101){Array.new(101){Array.new(20){Array.new(3,-1)}}}
def solve(c1,c2,s,t)

    if c1>$n1 || c2>$n2 then
        return 0
    end
    if c1==$n1 && c2==$n2 then
        return 1
    end
    if $dp[c1][c2][s][t]>=0 then
        return $dp[c1][c2][s][t]
    end
    #1を置く
    ans=0
    if t==0 then
        if s+1<$k1 then
            ans+=solve(c1+1,c2,s+1,t)
        end
    else
        ans+=solve(c1+1,c2,1,0)
    end

    #2を置く
    if t==1 then
        if s+1<$k2 then
            ans+=solve(c1,c2+1,s+1,t)
        end
    else
        ans+=solve(c1,c2+1,1,1)
    end
    ans%=100000000
    $dp[c1][c2][s][t]=ans
    return ans
end

p solve(0,0,0,0)


def ptstr(x, y, h)
	p = x.to_s + " " + y.to_s
	if h.key?(p)
		h[p] += 1
	else
		h[p] = 1
	end
end

ret = true
ar = Array.new(4)
h = Hash.new
xlcnt = 0
ylcnt = 0
for i in 0..3
	a = gets.chomp.split(/ /).map!{|x| x.to_i}
	ret = false if a[0] == a[2] && a[1] == a[3]

	if a[0] == a[2]
		xlcnt += 1
	elsif a[1] == a[3]
		ylcnt += 1
	else
		ret = false
	end
	ptstr(a[0],a[1], h)
	ptstr(a[2],a[3], h)
	ar[i] = a
end

ret = false if xlcnt != 2 || ylcnt != 2
ret = false if h.count != 4
h.each{|key, val| ret = false if val != 2 }

if ret
	puts "YES"
else
	puts "NO"
end


a,b = gets.split(/\s+/).map(&:to_i)
mod = 1000000007

    maxa = b - 1

ans = 0
  if maxa > 0
    x1 = (b+1 + a*b+1)*a / 2
    xn = maxa*(b+1 + a*b+1)*a / 2
    ans = (x1 + xn) * maxa / 2
  end

p ans % mod


I =->{gets.split.map &:to_i}

x, y, z = I[]; x1, y1, z1 = I[]; a = I[]
res = 0
res += a[5] if x > x1
res += a[4] if x < 0
res += a[1] if y > y1
res += a[0] if y < 0
res += a[3] if z > z1
res += a[2] if z < 0

puts res


n, q = readline.split(' ').collect(&:to_i)
hash = Hash.new
q.times do
  a, b = readline.split(' ')
  hash[a] = b
end

puts ["a", "b", "c", "d", "e", "f"].repeated_permutation(n).count {|arr|
#  puts arr
  str = arr.join
  while hash.has_key? str[0..1]
    str = hash[str[0..1]] + str[2..-1]
  end
  str == "a"
}


def solution
  x, y, z = read_ints

  a = x
  b = [a + 1, y].min
  c = [b + 1, z].min

  total = c * 3 - 3

  puts total

end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']


n = gets;
a = gets.split.map(&:to_i);
l = a.length;
b_sum = c_sum = 0;
while l > 0 do
  if a[l-1] >= 0
    b_sum += a[l-1];
  else
    c_sum += a[l-1];
  end
  l -= 1
end
puts (b_sum - c_sum);


n, m = gets.split(' ').map &:to_i
if n == 0
  puts m*3
elsif m == 0
  puts n*2
else
  nmax = n*2
  mmax = m*3
  for i in 1...5e6

    if i > nmax || i > mmax
      break
    end

    if i%2 == 0 && i%3 == 0
      if i <= nmax && i <= mmax
        if nmax + 2 < mmax + 3
          nmax += 2
        else
          mmax += 3
        end
      end
    end
  end

  puts [nmax, mmax].max
end


X, Y = gets.split.map &:to_i
x, y = X, Y
2000000.times { |i|
  if i & 1 == 0 # Ciel's turn, more 100
    if x >= 2 and y >= 2
      x -= 2
      y -= 2
    elsif x >= 1 and y >= 12
      x -= 1
      y -= 12
    elsif y >= 22
      y -= 22
    else
      puts "Hanako"
      exit
    end
  else
    if y >= 22
      y -= 22
    elsif y >= 12 and x >= 1
      y -= 12
      x -= 1
    elsif y >= 2 and x >= 2
      y -= 2
      x -= 2
    else
      puts "Ciel"
      exit
    end
  end
}


def modul(num)
  return num < 0 ? -num : num
end

nums_fir = gets.split.map &:to_i
nums_sec = gets.split.map &:to_i
puts ([ modul(nums_sec[0]-nums_fir[0]), modul(nums_sec[1]-nums_fir[1])].max )


n = gets.chomp.to_i
k = gets.chomp.to_i
a = gets.chomp.to_i
b = gets.chomp.to_i
result = 0

if k != 1
  while n != 1
    if(n%k == 0)
      sum = (n - n/k) * a

      if(b < sum)
        result += b
        n = n/k
      else
        result += (n - 1) * a
        break
      end
    else
      res = n%k
      n -= res
      result += res * a
    end
  end
else
  result = (n - 1) * a
end

puts result


a = []
3.times do
  a.push(gets.split.map(&:to_i))
end
a.map!{|b| b.map!{|c| c = c % 2}}
ans = []
5.times do
  ans.push([1,1,1,1,1])
end
3.times do |i|
  3.times do |j|
    if a[i][j] == 1
      ans[i+1][j+1] = 1 - ans[i+1][j+1]
      ans[i][j+1] = 1 - ans[i][j+1]
      ans[i+2][j+1] = 1 - ans[i+2][j+1]
      ans[i+1][j] = 1 - ans[i+1][j]
      ans[i+1][j+2] = 1 - ans[i+1][j+2]
    end
  end
end
3.times do |i|
  3.times do |j|
    print ans[i+1][j+1]
  end
  print "\n"
end


def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end

def scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end

def na(n=0,d=0) Array.new(n,d)end

def na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end

def na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def bit(n) n.to_s(2).split("").map(&:to_i) end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def k2(k,n,m)
  return 1 if k == 0
  return n if(k == 1)
  ans = 1
  if((k%2) == 0)
    ans *= k2(k/2,n,m)**2
  else
    ans *= k2(k-1,n,m)
    ans *= n
  end
  return ans % m
end
mod = 10**9+7
a,b = inp
p k2(a,k2(b,2,mod)-1,mod)


n = gets.to_i

1.upto(n) do |d|
    next if n%d != 0
    a = n/d
    c = 1
    while a%2 == 0
        a/=2
        c += 1
    end
    a+=1
    while a%2 == 0
        a/=2
        c -= 1
    end

    if c == 0 && a == 1
        p n/d
        exit
    end
end
abort


n = gets.to_i
app = gets.split.map(&:to_i)
if n == 1
	print "NO"
else
	wt = Hash.new
	app.each do |a|
		wt[a] = (wt[a] || 0) + 1
	end

	if n % 2 != 0
		print (wt[100] || 1) % 2 == 0 ? "YES" : "NO"
	else
		a100 = (wt[100] || 2) % 2 == 0
		a200 = (wt[200] || 2) % 2 == 0
		print a100 && a200 ? "YES" : "NO"
	end
end


# Input
t = gets.chomp

# Processing
n = t.length
k = (n + 1) / 2 - 1
str = ""
n.times do |i|
  j = ((-1)**(i + 1)) * ((i + 1) / 2)
  # puts j
  str = str + t[k + j]
end

# Output
puts str


# CodeForces 3C MegaShow 2017.2.12

# init
$arr = [[]]

# input
for i in 0..2
    $arr[i] = gets
end

#------------------------------------------------------------------------------
# check win
def check_win(char)
    for i in 0..2
        if $arr[i][0] == char and $arr[i][1] == char and $arr[i][2] == char
            return true
        end
    end
    for i in 0..2
        if $arr[0][i] == char and $arr[1][i] == char and $arr[2][i] == char
            return true
        end
    end
    if $arr[0][0] == char and $arr[1][1] == char and $arr[2][2] == char
        return true
    end
    if $arr[0][2] == char and $arr[1][1] == char and $arr[2][0] == char
        return true
    end
    return false
end

#------------------------------------------------------------------------------
# main
def main
    first_x = second_0 = 0
    first_win = check_win('X')
    second_win = check_win('0')

    for i in 0..2
        for j in 0..2
            if $arr[i][j] == 'X'
                first_x += 1
            elsif $arr[i][j] == '0'
                second_0 += 1
            end
        end
    end

    # judge
    if (first_win and first_x == second_0) or (second_win and first_x == second_0 + 1) or
                        first_x - second_0 > 1 or first_x < second_0
        print "illegal\n"
    elsif first_win
        print "the first player won\n"
    elsif second_win
        print "the second player won\n"
    elsif first_x + second_0 == 9
        print "draw\n"
    elsif first_x == second_0
        print "first\n"
    elsif first_x - second_0 == 1
        print "second\n"
    end
end

#start
main


def power_mod(num, power)
    prod = 1
    num %= MOD
    while power > 0 do
        if power & 1 == 1
            prod = (prod * num) % MOD
        end
        num = (num * num) % MOD
        power >>= 1
    end
    return prod
end

MOD = 10**9+7

X, K = gets.split.map(&:to_i)
if X == 0
    puts 0
    exit
end

low = (X-1) * power_mod(2, K) + 1
high = X * power_mod(2, K)
puts (low + high) % MOD


#!/usr/bin/env ruby

def modpow(a, x, mod)
  return 1 if x == 0
  return a if x == 1
  half = modpow(a, x/2, mod)
  rem = x.odd? ? a : 1
  half * half * rem % mod
end

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


def check(m, c1, c2, x, y)
  c1 -= m / y - m / (x * y)
  c2 -= m / x - m / (x * y)
  c1 = [c1, 0].max
  c2 = [c2, 0].max
  m - m / x - m / y + m / (x * y) >= c1 + c2
end
cnt1, cnt2, x, y = gets.split.map {|i| i.to_i}
l, r = 1, 10 ** 10
while r - l > 1
  m = (r + l) / 2
  if check(m, cnt1, cnt2, x, y)
    r = m
  else
    l = m
  end
end
puts r


n = readline.to_i
arr = readline.split(' ').collect(&:to_i).sort.uniq
found = false
0.upto(arr.length - 3) do |ind|
  if arr[ind] + 1 == arr[ind + 1] and arr[ind] + 2 == arr[ind + 2]
    found = true
    break
  end
end

if found
  puts "YES"
else
  puts "NO"
end