n,m,a = gets.split(' ').map(&:to_i)
w = n+a-1)/a
h = (m+a-1)/a
puts w*h


n = gets.chomp
    m = gets.chomp
    a = gets.chomp

    x = n/a;
    y = m/a;
    mod_n = n % a;
    if (mod_n > 0)
        x = x+1

    mod_m = m % a;
    if (mod_m > 0)
        y = y+1

    print("#{x*y}")


    n = gets.chomp
    m = gets.chomp
    a = gets.chomp

    x = n/a;
    y = m/a;
    mod_n = n % a;
    if (mod_n > 0)
        x = x+1;

    mod_m = m % a;
    if (mod_m > 0)
        y = y+1;

    puts "#{x*y}";


    n = gets.chomp
    m = gets.chomp
    a = gets.chomp

    x = n/a;
    y = m/a;
    mod_n = n % a;
    if (mod_n > 0){
        x = x+1;
    }
    mod_m = m % a;
    if (mod_m > 0){
        y = y+1;
    }

    puts "#{x*y}";
}


#         ... LETS HACK THE WORLD ...
#... KEEP CALM AND SAY LE ROI EST MORT IS HERE ...
#       ... EL-BADRY BECOME A LEGEND ...
n, m, a = gets.chomp.split.map(&:to_i)

 if m%a==0 and n%a==0

            puts (m/a)*(n/a)

        if m%a!=0 and n%a!=0

            puts ((m/a)+1)*((n/a)+1)

        if m%a==0 and n%a!=0

            puts (m/a)*((n/a)+1)

        if m%a!=0 and n%a==0

            puts ((m/a)+1)*(n/a)


class A1
  def m(n, m, a)
      (n / a.to_f).ceil * (m / a.to_f).ceil
    end
  end
end

n, m, a = gets.chomp.split("\s").map { |x| x.to_i }
a = A1.new
puts a.m(n,m,a)


x = raw_input()
n = "NO"
for i in x:
	if i=='H' or i== 'Q' or i== '9' or i== '+':
		n = "YES"
print n


hq = gets.strip
l = hq.length
s = false

for i in 0...l
  if hq[i] == "H" || hq[i] == "Q" || hq[i] == "9" then s = true end
end

puts if s then "Yes" else "No" end


input = gets.chomp
if input == (input[0].downcase + input.reverse.chop.reverse.upcase)
  puts input.capitalize
elsif input.upcase == input ||
  puts input.downcase
else
  puts input
end


word = raw_input()
print (word[0].swapcase() + word[1:].lower()) if (word[1:] == "" or word[1:].isupper()) else word


input_word = gets.chomp
initial = input_word.split("")

if input_word == input_word.upcase
  p input_word.downcase
elsif input_word[1,input_word.length-1]==input_word.upcase[1,input_word.length-1] && initial == input_word.at(0) ==initial
  p input_word.capitalize
end


str=''; 2.times { str << gets.chomp }; puts gets.chomp.sort==str.sort?"YES":"NO"


w = gets.split.map &:to_i
if w % 2 == 0 {puts "YES"}
else {puts "NO"}


n = Integer(gets())
if n.odd?:
    puts "NO"
    exit 0
end

a = b = n/2

if a.even? && b.even?:
    puts "YES"
    exit 0
end
a--
b++
if a <= 0:
    puts "NO"
    exit 0
end
if a.even? && b.even?:
    puts "YES"
    exit 0
end
puts "NO"


w = gets.to_i
if w >= 4 and w % 2 == 0 do
puts "YES"
else
puts "NO"
end


w = gets.chomp.to_i
if w.odd? || w == 2
	puts "NO"
else
	(puts "YES") : (puts "NO")
end


a = gets.to_i

if (a % 4 == 0)
	p "YES"
else
	p "NO"


w = gets.to_i
(w%2 == 0 and w > 0) puts "YES" : puts "NO"


#! ruby -Ku
# -*- coding: utf-8 -*-

n = gets.to_i
if n % 2 == 0
puts YES
else
puts NO


if gets.chomp =~ /1{7}|0{7}/
  puts "YES"
else
  puts "NO"


include Math

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

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


string1 = gets.chomp.downcase
string2 = gets.chomp.downcase

if string1 < string2
  puts "-1"
elsif string1 > string2
  puts "1"
else
  puts "0"


import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class B {
	public static void main(String[] args) {
		doIt2();
	}
	static void doIt(){
		Calendar gc1, gc2;
		int fyear, fmonth, fday, tyear, tmonth, tday;
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		String[] ss = s.split(":");
		fyear = Integer.parseInt(ss[0]);
		fmonth = Integer.parseInt(ss[1]);
		fday = Integer.parseInt(ss[2]);
		s = sc.next();
		ss = s.split(":");
		tyear = Integer.parseInt(ss[0]);
		tmonth = Integer.parseInt(ss[1]);
		tday = Integer.parseInt(ss[2]);
		Calendar calBefore = Calendar.getInstance();
		Calendar calAfter = Calendar.getInstance();

//		// 2011.06.01
//		calBefore.set(1996, Calendar.MARCH, 9, 0, 0, 0);
//
//		// 2011.06.30
//		calAfter.set(1991, Calendar.NOVEMBER, 12, 0, 0, 0);
		// 2011.06.01
				calBefore.set(1900, Calendar.FEBRUARY, 1, 0, 0, 0);

				// 2011.06.30
		calAfter.set(2038, Calendar.DECEMBER, 31, 0, 0, 0);
		// 2つの日付の差を求める(ミリ秒)
		long result = Math.abs(calAfter.getTime().getTime() - calBefore.getTime().getTime());
		result /= 1000 * 60 * 60 * 24;

		System.out.println(result);
	}
	static void doIt2(){
		GregorianCalendar gc1, gc2;
		int fyear, fmonth, fday, tyear, tmonth, tday;
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		String[] ss = s.split(":");
		fyear = Integer.parseInt(ss[0]);
		fmonth = Integer.parseInt(ss[1]);
		fday = Integer.parseInt(ss[2]);
		s = sc.next();
		ss = s.split(":");
		tyear = Integer.parseInt(ss[0]);
		tmonth = Integer.parseInt(ss[1]);
		tday = Integer.parseInt(ss[2]);
		gc1 = new GregorianCalendar(fyear-1, fmonth, fday);
		gc2 = new GregorianCalendar(tyear-1, tmonth, tday);
		long result = Math.abs(gc1.getTimeInMillis() - gc2.getTimeInMillis());
		long div = 24 * 60 * 60 * 1000;
		result /= div + ((0 < result % div)? 1 : 0);
		System.out.println(result);
	}
}


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

p a.count {|x| if x > a[k-1]}


n,k  = gets.chomp.split(' ').map!{ |o| o.to_i }
nums = gets.chomp.split(' ').map!{ |o| o.to_i }

puts k + nums[k..n].count n[k]


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>

using namespace std;

typedef unsigned long long ll;

int dig(ll a)
{
  stringstream ss;
  ss << a;
  return ss.str().size();
}

int main(void)
{
  ll p, d;
  cin >> p >> d;
  if( d == 0 ) {
    if( p%10 == 9 ) {
      cout << p << endl;
    } else {
      cout << 0 << endl;
    }
    return 0;
  }
  --p;
  if( p < 9 ) {
    cout << 0 << endl;
    return 0;
  }
  --d;
  int s = dig(p);
  ll mod = 10;
  ll now = 9;
  ll res = 0;
  for( int i = 0; i < s; i++ ) {
    ll q = p;
    ll r = q/*%(mod*10)*/;
    if( r >= now && d >= (r-now)%mod && q >= (r-now)%mod) {
      res = q-(r-now)%mod;
    }
    mod *= 10;
    now *= 10;
    now += 9;
  }
  cout << res << endl;
}


k = $stdin.readline.to_i
l = $stdin.readline.to_i
m = $stdin.readline.to_i
n = $stdin.readline.to_i
d = $stdin.readline.to_i

a = Array.new(d)
i = 1
while i<=d
    a[i] = 1 if i%k==0 || i%l==0 || i%m==0 || i%n==0
    i+=1
end

s = a.compact.inject(:+)
puts s.nil? : 0 : s


word = gets.gsub!(/[aoyeui]/i,"").downcase.to_a.map {|i| i = ". + #{i}") }
puts "#{word.join}"


i = gets.chomp.to_i

i.times do
word = gets.chomp
vowel = ['a','e','i','o','u','y']
str = ''
word.split("").each do |char|
unless vowel.include? char.to_downcase
str = '.' + char.to_downcase
end
end


vowel = ['a','e','i','o','u','y']
word = gets.chomp
str = ''
word.split("").each do |c|
unless vowel.include? c.downcase
str = str + '.' + c.downcase
end
end
print str
end


#include <cstdio>
#include <vector>
using namespace std;

int main() {
    int t;
    scanf(" %d", &t);
    for (int i = 0; i < t; i++) {
        int n, m;
        scanf(" %d %d", &n, &m);
        vector<int> c(n);
        int cmax = 0, winner;
        for (int j = 0; j < m; j++)
            for (int k = 0; k < n; k++) {
                int v;
                scanf(" %d", &v);
                c[k] += v;
                if (c[k] > cmax) {
                    cmax = c[k];
                    winner = k + 1;
                }
            }
            printf("%d\n", winner);
    }
    return 0;
}


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

if l == r puts 0
else
x = r^l

puts 2**(Math.log2(x).ceil) -1
end


j=gets.chomp
le=j.length
le.times do |i|
if j[i]=='W' && j[i+1]=='U' && j[i+2]=='B'
j[i]='*'
j[i+1]='*'
j[i+2]='*'
end
end
le.times do |i|
if j[i]!='*'
print j[i]
end


#include<stdio.h>

main(){
	char s[101], t[101], i, lens;

	gets(s); gets(t);
	lens=strlen(s);

	if (lens!=strlen(t))
	{
		puts("NO");
		exit(0);
	}

	for(i=0; i<lens; i++) if (s[i]!= t[lens-i-1])
		{
			puts("NO");
			exit(0);
		}
	puts("YES");
};


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


word = gets

i = 0
up = 0
low = 0

while i < word.length ;
	if word[i] <= 90
		up++
	else
		low++

if up > low
	word.upcase
else
	word.lowcase

return word


loop = STDIN.gets.to_i
a = STDIN.gets.split(" ")
total = [0, 0, 0]
loop.times{|i| total[i % 3] += a[i].to_i}
case total.index(total.max)
when 0 puts "chest"
when 1 puts "biceps"
when 2 puts "back"
end


n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
ans = []
(1..n).each { |x|
  mn = 0
  (1..(n-1)).each { |i| mn = i if a[i] < a[mn] }
  break if a[mn] == 101 || m < a[mn]
  ans.push(mn+1) m -= a[mn] a[mn] = 101
}
p ans.size
ans.each { |x| p x}


s=gets.chomp.to_s
s=s.split(" ")
r=s[0].to_i
c=s[1].to_i
row_straw=[]
col_strar=[]
rows= Array.new(r)
r.times{|iter|
	rows[iter]=gets.chomp.to_s
	c.times{|iter2|
		if (rows[iter][iter2]=='S')
			#record
			col_strar.unshift(iter2)
			row_straw.unshift(iter)
	}
}
puts((r-row_straw.size)*c+(c-col_straw.count)*(row_straw.count))


n, k = gets.split.map(&:to_i)
puts max*3(n-k, 0)


#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>

using namespace std;

const int mxn = 1002;

int N;
char s[mxn][mxn];
bitset<mxn> a[mxn], b[mxn];

bool check() {
	for (int i = 0; i < N; ++i)
		for (int j = i + 1; j < N; ++j)
			if (!b[i][j] && (b[i] & b[j] & (a[i] ^ a[j])).none()) {
				//a[i][j] = !a[i][j];
				return false;
			}
	return true;
}

void EXEC() {
	scanf("%d", &N);
	for (int i = 0; i < N; ++i) {
		scanf("%s", s[i]);
		for (int j = 0; j < N; ++j) {
			a[i][j] = rand() & 1;
			b[i][j] = int(s[i][j] == '+');
		}
	}
	while (!check()) {
		//random_shuffle(a, a + N);
		for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j)
			a[i][j] = rand() & 1;
		//for (int i = 0; i < N * 5; ++i)
		//	a[rand() % N][rand() % N] = rand() & 1;
	}
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j)
			if (s[i][j] == '-')
				printf("-");
			else
				printf("%d", int(a[i][j]) + 1);
		printf("\n");
	}
}

int main() {
	srand(time(0));
	freopen("avia.in", "r", stdin);
	freopen("avia.out", "w", stdout);
	EXEC();
	return 0;
}


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

k = read_int
a = read_ints
a.sort!.reverse!

if k == 0 do
  puts 0
  exit
end

count = 0
sum = 0

a.each do |i|
  count += 1
  sum += i
  if k <= sum
    puts count
    exit
  end
end

puts -1


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

a.sort!.reverse!

i = -1
s = 0
while s < n and i <a.size - 1
  i += 1
  s += a

i = -1 if s < n
puts i


def fill_primes(max)
  primes = [2,3]
  now = primes[-1]
  loop{
    now+=2
    break if now>max
    prime, check = true, 0
    while primes[check]**2 <= now:
      prime = false if now%primes[check]==0
      break if not prime
      check+=1
    end
    primes << now if prime
  }
  return primes
end

def is_almost_prime(num, primes)
  check = 0
  divisors = [1]
  while num>1
    while num % primes[check] == 0
      num /= primes[check]
      divisors << primes[check] if divisors[-1] != primes[check]
    end
    check+=1
  end
  if divisors.length == 3
    1
  else
    0
  end
end

primes = fill_primes(3000)

primes, n, ans = fill_primes(3000), gets.chomp.to_i, 0
2.upto(n){|i| ans+=is_almost_prime(i, primes) }
p ans


#!/usr/bin/ruby

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

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

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

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


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

if a > b
    puts "First"
else
    puts "Second"
end


arr = $stdin.readline.split(" ")
n1 = arr[0].to_i
n2 = arr[1].to_i
n2 + 1 > n1 ? puts "Second" : puts "First"


n, m = gets.split.map &:to_i
if n == m
  puts "Second"
else
  if [n, m].max == n
    puts "First"
  else
    puts "Second"
  end
e


s = gets
v,f,a=s.count'7',s.count'4',-1
a=4 if f>0
a=7 if v>f
puts a


s = gets
ans =nil
ans_count =0
m = {}
0.upto s.length do |i|
    1.upto s.length-i do |j|
        ss = s[i, j]
        b = false
        ss.each_char {|c| b ||= (c!='7' && c!='4')}
        if !b do
            count=m[ss]+1
            m[ss] = count
            if count > ans_count) do
                ans = ss
            elsif count== ans_count do
                ans = ss if ans==nil
                ans = min ans, ss
            end
        end
    end
end

puts ans


n = gets.chomp.to_i
if n%4==0 or n%7==0 or n%44==0 or n%47==0 or n%74==0 or n%77==0
    puts "YES"
else
    puts "NO"


m=[0,0,0]
m[0],m[1],m[2],k=gets.split.map(&:to_i)
ans=[1,1,1]
t=0
tk=k*3
tmp=0
loop {
        break unless k>0
        break unless tk>t
        if ans[t%3]<m[t%3] then
                tmp=0
                k-=1
                ans[t%3+=1]
        else
                tmp+=1
        end
        if tmp>4 then break end
        t+=1
}
p ans[0]*ans[1]*ans[2]


# -*- coding: utf-8 -*-
def f(alpha)
  while alpha < 0
    alpha += 2 * Math::PI
  end
  while alpha > 2 * Math::PI
    alpha -= 2 * Math::PI
  end
  alpha
end
x1,y1,x2,y2,x3,y3 = readlines.join("").split(/\s/).map &:to_f

a = Math::sqrt((x2-x3)**2 + (y2-y3)**2)
b = Math::sqrt((x3-x1)**2 + (y3-y1)**2)
c = Math::sqrt((x2-x1)**2 + (y2-y1)**2)

r = a*b*c/Math::sqrt((a+b+c)*(-a+b+c)*(a-b+c)*(a+b-c))

argA = Math::asin(a/(2*r))
argB = Math::asin(b/(2*r))
argC = Math::asin(c/(2*r))

x = (x1 * Math::sin(2*argA) + x2 * Math::sin(2*argB) + x3 * Math::sin(2*argC))/(Math::sin(2*argA) + Math::sin(2*argB) + Math::sin(2*argC))
y = (y1 * Math::sin(2*argA) + y2 * Math::sin(2*argB) + y3 * Math::sin(2*argC))/(Math::sin(2*argA) + Math::sin(2*argB) + Math::sin(2*argC))

x1 -= x
y1 -= y
x2 -= x
y2 -= y
x3 -= x
y3 -= y

arg1 = (Math::atan2(y2,x2))
arg2 = (Math::atan2(y1,x1))
arg3 = (Math::atan2(y3,x3))
alpha = arg2 - arg1
beta  = arg3 - arg1
alpha = f(alpha)
beta  = f(beta)

for n in 3..100
  arg = 2*Math::PI/n
  fa , fb = false, false
  cur = 0
  for i in 0 .. n
    cur += arg
    fa = true if ((cur - alpha).abs < 1e-2)
    fb = true if ((cur - beta ).abs < 1e-2
  end
  #puts "#{n}: #{alpha} #{beta} #{arg}, #{(alpha/arg).round} #{(alpha/arg)} #{(beta/arg).round} #{beta/arg}"
#  if ((alpha/arg).round - (alpha/arg)).abs < 1e-5 && ((beta/arg).round - (beta/arg)).abs < 1e-5
  if (fa && fb)
    printf "%.10f\n", n * r * r * Math::sin(arg)/2
    break
  end
end


B82;20710;0crequire "mathn"

array = gets.split(/ /)

flag = false
flag2 = false

Prime.new.each{|x|
  if flag2 == true
    if array[1].to_i == x
      puts "YES"
      break
    else
      puts "NO"
      break
    end
  end

  if array[0].to_i == x
    flag = true
  end
  if flag == true
    flag2 = true
  end

}


import sys
import math
import itertools

def to_numeric(c, r):
  ret = 0

  array = [i for i in c]
  array.reverse()
  for i in range(len(array)):
    tmp = 0
    if array[i].isalpha():
      tmp = ord(array[i]) - ord('A') + 10
    else:
      tmp = int(array[i])

    ret += tmp * r ** i

  return ret

if __name__ == '__main__':
  print to_numeric("2A", 2)
  """
  s = raw_input().rstrip()
  a = s.split(":")[0]
  b = s.split(":")[1]

  r = 3
  radixes = []
  for i in range(2, 61):
    a2 = to_numeric(a, i)
    b2 = to_numeric(b, i)

    if a2 < 24 and b2 < 60:
      radixes.append(i)

  if len(radixes) == 0:
    print 0
  else:
    if len(radixes) == 59:
      print -1
    else:
      print " ".join([str(i) for i in radixes])
  """


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

if a < 0
  a = -a
  s.tr! 'LR', 'RL'
end

if b < 0
  b = -b
  s.tr! 'UD', 'DU'
end

minx = maxx = miny = maxy = x = y = 0

def step(x, y, c)
  if c == 'U' then
    y += 1
  elsif c == 'D' then
    y -= 1
  elsif c == 'R' then
    x += 1
  elsif c == 'L' then
    x -= 1
  end
  [x, y]
end

s = "0#{s}"
s.each_char do |c|
  x, y = step x, y, c
  maxx = x if x > maxx
  minx = x if x < minx
  maxy = y if y > maxy
  miny = y if y < miny
  if x == a and y == b
    puts 'Yes'
    exit
  end
end

dx = x
dy = y

if dx < 0 or dy < 0 or (dx == 0 and dy == 0)
  puts 'No'
  exit
end

startx = a - maxx - 1
endx = a - minx + 1
starty = b - maxy - 1
endy = b - miny + 1

iter = [dx > 0? startx / dx: 10**9+10, dy > 0 ? starty / dy: 10**9 + 10].min
x = iter * dx
y = iter * dy

begin
  s.each_char do |c|
    x, y = step x, y, c
    if x == a and y == b
      puts 'Yes'
      exit
    end
  end
end while x <= endx and y <= endy

puts 'No'


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


s = gets.chomp.delete("?!., *^%@#$()_-=+")
b = s[-1].delete("aeioyu")
if b == "" then
    puts "YES"
else
    puts "NO"
end


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


teams,c = [],0
gets.chomp.to_i.times do |variable|
	h,a = gets.chomp.split(" ").map(&:to_i)
	teams << {"h": h, "a": a}
end
teams.each_with_index do |t,i|
	teams.each_with_index do |t2,j|
		next if i == j
		c += 1 if t2[:h] == t[:a]
	end
end
puts c


class Card
	def initialize(card)
		@card = card
	end

	def suit
		@card[1]
	end

	def number
		@card[0]
	end

	def to_s
		@card
	end

	def ==(other)
		self.number == other.number && self.suit == other.suit
	end
end

def check(arr, row, col)
	return false if row < 0 || col < 0 || row+3 > arr.size || col+3 > arr[0].size

	numbers = []
	suits = []
	3.times do |r|
		3.times do |c|
			numbers << arr[row+r][col+c].number
			suits << arr[row+r][col+c].suit
		end
	end

	!numbers.index("J") && (numbers.uniq.size == 9 || suits.uniq.size == 1)
end

def wrap(rect1, rect2)
	(rect1[0]-rect2[0]).abs < 3 && (rect1[1]-rect2[1]).abs < 3
end

cards = []
"A23456789TJQK".each_char do |num|
	"CDHS".each_char do |suit|
		cards << Card.new(num + suit)
	end
end

n,m = gets.split.map(&:to_i)
arr = []
joker = []
n.times do |row|
	a = gets.split.map{|cd| Card.new(cd)}
	a.each_with_index do |card,col|
		if card.number == "J"
			joker << [row, col, card.suit]
		else
			cards.delete(card)
		end
	end
	arr << a
end

rects = []
(0..n-3).each do |row|
	(0..m-3).each do |col|
		rects << [row, col] if check(arr, row, col)
	end
end

ans = nil
rects.combination(2) do |cb|
	if !wrap(cb[0], cb[1])
		ans = cb
		joker.each_with_index do |jk,idx|
			arr[jk[0]][jk[1]] = cards[idx]
		end
		break
	end
end

if !ans && joker.size > 0
	catch(:check_loop) {
		cards.combination(joker.size).each do |cb|
			cb.permutation do |perm|
				joker.each_with_index do |jk,idx|
					arr[jk[0]][jk[1]] = perm[idx]
				end
				nr = []
				joker.each_with_index do |jk|
					(-2..0).each do |dr|
						row = jk[0]+dr
						(-2..0).each do |dc|
							col = jk[1]+dc
							if check(arr, row, col)
								nr << [row, col]
							end
						end
					end
				end
				(nr+rects).each do |r1|
					nr.each do |r2|
					if !wrap(r1, r2)
						ans = [r1, r2]
						throw :check_loop
					end
				end
			end
		end
	}
end

if !ans
	puts "No solution."
else
	puts "Solution exists."
	joker.sort_by!{|a| a[2]}
	if joker.size == 0
		puts "There are no jokers."
	elsif joker.size == 1
		puts "Replace J#{joker[0][2]} with #{arr[joker[0][0]][joker[0][1]]}."
	else
		puts "Replace J1 with #{arr[joker[0][0]][joker[0][1]]} and J2 with #{arr[joker[1][0]][joker[1][1]]}."
	end
	puts "Put the first square to (#{ans[0][0]+1}, #{ans[0][1]+1})."
	puts "Put the second square to (#{ans[1][0]+1}, #{ans[1][1]+1})."
end


t="
"+$<.read;puts t==t.reverse ?"YES":"NO"


def pow(a, b)
    if (b==1) return a;
    if (b==0) return 1;
    c = pow(a,b/2)
    c = (c * c) % $mod
    c = (c * a) % $mod if (b % 2==1)
    c
end

n, m = *gets.split.map {|x|x.to_i}
$mod = m
puts (pow(3, n) - 1 + m) % m


n = gets.to_i
a = gets.split.map(&:to_i)
a.sort!
(n-1).times do |i|
    if a[i + 1] < a[i] * 2 - 1 the
        puts "YES"
        exit
    end
end

puts "NO"


#! /usr/bin/ruby

$n = gets.to_i
$cards = gets.split(" ")

def card_compare( a, b )
  a[0] == b[0] or a[1] == b[1]
end

def ff( a, i )
  a[i] = a[0]
  a.shift
  return a
end

def f( a )
  if a.length == 1
    return true
  #####################
  if card_compare( a[0], a[1] )
    b = a.clone
    b = ff( b, 1 )
    if f( b )
      return true
    end
  end
  #####################
  if a.length > 3
    if card_compare( a[0], a[3] )
      b = a.clone
      b = ff( b, 3 )
      if f( b )
	return true
      end
    end
  end
  #####################
  return false
end

if f( $cards.reverse! )
  puts "YES"
else
  puts "NO"
end


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

if n < m
    puts "Impossible"
else
    puts n + m - [n,m].min, n + m - [1,m].min


n,m=gets.split.map(&:to_i)
a=n<m?m:n
if n==0
  puts "Impossible"
else
  puts "#{a} #{n+m-1}"
end


n,m=gets.split.map(&:to_i)
if n==0
  puts "Impossible"
else
  puts "#{n<m?m:n} #{n+m-1}"
end


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li        long long int
#define rep(i,to) for(li i=0;i<((li)(to));++i)
#define pb        push_back
#define sz(v)     ((li)(v).size())
#define bit(n)    (1ll<<(li)(n))
#define all(vec)  (vec).begin(),(vec).end()
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define MP        make_pair
#define F         first
#define S         second

int main(){
	for(li pos=2;pos<120;pos+=2){ cout<<pos<<" "; } cout<<endl;
}


n=gets.to_i
s=gets.to_s.downcase!

s.split('').uniq.size == 26 ? puts "YES" : puts "NO"


n = input()
arr = list(map(int,input().split()))
lum = arr[0]
del(arr[0])
count=0
arr = sorted(arr)[::-1]
'''print(arr)'''
while lum<=arr[0]:
    lum+=1
    arr[0]-=1
    count+=1
    arr=sorted(arr)[::-1]
print(count)


#include <cstdio>
#include <queue>

int main() {
  int tol = 0;
  scanf("%d", &tol);
  for (int o=0; o<tol; o++) {
    int n, x, first, ans = 0;
    std::priority_queue<int> q;
    scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
      scanf("%d", &x);

      if (i > 0) {
        q.push(x);
      } else {
        first = x;
      }
    }

    while (q.top() >= first) {
      int max = q.top();
      q.pop();
      q.push(max - 1);
      ++first, ++ans;
    }

    printf("%d\n", ans);
  }
  return 0;
}


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


count = 0
winner = 0
wagons, sp, cp = gets.chomp.split.map{|s| s.to_i}
h = gets[3] == 'h' ? 0 : 1
gets.chomp.each_char do |c|
  count += 1
  if c.to_i == 0
    if sp < cp
      if sp > 1
        sp -= 1
      else
        sp += 1
      end
    else
      if sp < wagons
        sp += 1
      else
        sp -= 1
      end
    end
    if sp == cp
      winner = 1
      break
    end
    if h
      if cp > 1
        cp -= 1
      else
        cp += 1
        h = !h
      end
    else
      if cp < wagons
        cp += 1
      else
        cp -= 1
        h = !h
      end
    end
    if sp == cp
      winner = 1
      break
    end
  else
    if h
      if cp > 0
        cp -= 1
      else
        cp += 1
        h = !h
      end
    else
      if cp < wagons
        cp += 1
      else
        cp -= 1
        h = !h
      end
    end
    sp = cp <= wagons / 2 > ? cp + 1 : cp - 1
  end
  #puts "#{sp} #{cp}"
end
if winner == 0
  puts "Stowaway"
else
  puts "Controller #{count}"
end


import sys
import math
import itertools

if __name__ == '__main__':
  points = []

  s = [int(i) for i in raw_input().rstrip().split(" ")]
  x1 = s[0]
  y1 = s[1]
  x2 = s[2]
  y2 = s[3]

  min_x = min([x1, x2])
  max_x = max([x1, x2])
  min_y = min([y1, y2])
  max_y = max([y1, y2])

  for x in range(min_x, max_x + 1):
    points.append((min_y, x))
    points.append((max_y, x))

  for y in range(min_y, max_y + 1):
    points.append((y, min_x))
    points.append((y, max_x))

  points = list(set(points))

  n = int(raw_input().rstrip())

  while n > 0:
    s = [float(i) for i in raw_input().rstrip().split(" ")]
    x = s[0]
    y = s[1]
    r = s[2]

    tmp = []
    for p in points:
      if math.sqrt((float(p[0]) - y) ** 2 + (float(p[1]) - x) ** 2) > r:
        tmp.append(p)
    points = tmp

    n -= 1

  print len(points)


n = gets().to_i
res = case n
  when 1..2: n
  when 3: 6
  when n % 2 == 0 : ( n - 1 ) * ( n - 2 ) * ( n - 3 )
  else n * (n - 1) * (n - 2)
end

puts res


n = $stdin.gets.to_i
s = $stdin.gets.chomp

if s.gsub(/[47]/, '') == ""
  first = 0
  second = 0

  (n / 2).times do |i|
    first += s[i,1].to_i
    second += s[-i,1].to_i
  end

  if first == second
    puts "YES"
  else
    puts "NO"
else
  puts "NO"
end


// Problem site: http://codeforces.com/contest/585/problem/C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT_MAX_LENGTH 16

int max(int a, int b);
void string_compress(char *string);
int fract_compare(char *a[2], char *b[2]);
char *big_sum(char *a, char *b);
char *big_multiply(char *a, char *b);
int big_compare(char *a, char *b);
char *string_reverse(char *a);
char *string_append_zero(char *a, int total);
void try(char *left[2], char *right[2], char *targets[2], char *result);

int main() {
  char *targets[2];
  targets[0] = malloc(INPUT_MAX_LENGTH * sizeof(char));
  targets[1] = malloc(INPUT_MAX_LENGTH * sizeof(char));

  scanf("%s", targets[0]);
  scanf("%s", targets[1]);

  char *left[2];
  left[0] = malloc(INPUT_MAX_LENGTH * sizeof(char));
  left[1] = malloc(INPUT_MAX_LENGTH * sizeof(char));
  strcpy(left[0], "0");
  strcpy(left[1], "1");

  char *right[2];
  right[0] = malloc(INPUT_MAX_LENGTH * sizeof(char));
  right[1] = malloc(INPUT_MAX_LENGTH * sizeof(char));
  strcpy(right[0], "1");
  strcpy(right[1], "0");

  char *result = malloc(max(strlen(targets[0]), strlen(targets[1])) * sizeof(char));
  strcpy(result, "");

  try(left, right, targets, result);

  return 0;
}

char *big_sum(char *a, char *b) {
  int length_a = strlen(a), length_b = strlen(b);
  int i = 0, over = 0, result_length = 0;
  char *result = malloc((length_a + length_b) * sizeof(char));

  while (i < length_a || i < length_b) {
    int current_a = 0, current_b = 0;
    if (i < length_a) {
      current_a = a[length_a - 1 - i] - '0';
    }
    if (i < length_b) {
      current_b = b[length_b - 1 - i] - '0';
    }
    int current = current_a + current_b + over;
    over = current / 10;
    current = current % 10;
    result[result_length] = current + '0';
    result_length++;
    i++;
  }
  if (over > 0) {
    result[result_length] = over + '0';
    result_length++;
  }
  result[result_length] = '\0';

  return string_reverse(result);
}

char *big_multiply(char *a, char *b) {
  int length_a = strlen(a), length_b = strlen(b);
  int i;
  char *total = "0";
  for (i = 0; i < length_b; i++) {
    // Multiply a with b[length_b - 1 - i]
    int current_b = b[length_b - 1 - i] - '0';
    int j, over = 0, sum_length = 0;
    char *sum = malloc((length_a + 1) * sizeof(char));
    for (j = 0; j < length_a; j++) {
      int current_a = a[length_a - 1 - j] - '0';
      int current = current_a * current_b + over;
      over = current / 10;
      current = current % 10;
      sum[sum_length] = current + '0';
      sum_length++;
    }
    if (over > 0) {
      sum[sum_length] = over + '0';
      sum_length++;
    }
    sum[sum_length] = '\0';
    sum = string_reverse(sum);
    sum = string_append_zero(sum, i);
    total = big_sum(total, sum);
  }
  return total;
}

int big_compare(char *a, char *b) {
  if (strlen(a) == strlen(b)) {
    return strcmp(a, b);
  }
  return (strlen(a) > strlen(b)) ? 1 : -1;
}

char *string_append_zero(char *a, int total) {
  int length_a = strlen(a);
  char *result = malloc((length_a + total) * sizeof(char));
  int i;
  for (i = 0; i < length_a; i++) {
    result[i] = a[i];
  }
  for (; i < length_a + total; i++) {
    result[i] = '0';
  }
  return result;
}

char *string_reverse(char *a) {
  int length = strlen(a);
  int middle = length / 2;
  int i;
  char temp;

  for (i = 0; i < middle; i++) {
    temp = a[i];
    a[i] = a[length - 1 - i];
    a[length - 1 - i] = temp;
  }

  return a;
}

void try(char *left[2], char *right[2], char *targets[2], char *result) {
  while (1) {
    char *sum[2] = {big_sum(left[0], right[0]), big_sum(left[1], right[1])};

    if (fract_compare(sum, targets) == 0) {
      if (big_compare(sum[0], targets[0]) != 0) {
        printf("Impossible\n");
      } else {
        string_compress(result);
      }

      return;
    }

    if (big_compare(sum[0], targets[0]) > 0 || big_compare(sum[1], targets[1]) > 0) {
      printf("Impossible\n");

      return;
    }

    long long length = strlen(result);
    if (fract_compare(sum, targets) > 0) {
      result[length] = 'B';
      result[length + 1] = '\0';
      strcpy(right[0], sum[0]);
      strcpy(right[1], sum[1]);
    } else {
      result[length] = 'A';
      result[length + 1] = '\0';
      strcpy(left[0], sum[0]);
      strcpy(left[1], sum[1]);
    }
    result[length + 1] = '\0';
  }
}

int max(int a, int b) {
  return (a > b) ? a : b;
}

void string_compress(char *string) {
  long long length = strlen(string);
  long long i, current_total = 1;
  char prev = string[0], current;
  for (i = 1; i < length; i++) {
    current = string[i];
    if (current == prev) {
      current_total++;
    } else {
      printf("%llu%c", current_total, prev);
      prev = current;
      current_total = 1;
    }
  }
  printf("%llu%c", current_total, prev);
  printf("\n");
}

int fract_compare(char *a[2], char *b[2]) {
  return big_compare(big_multiply(a[0], b[1]), big_multiply(a[1], b[0]));
}


#!/usr/bin/env ruby
point,x, y = [], [], []
8.times do
  point << gets.split(' ').map(&:to_i)
  x << point[-1][0] unless x.include? point[-1][0]
  y << point[-1][1] unless y.include? point[-1][1]
end
if x.size == 3 && y.size == 3
  ok = true
  for i in 0...3
    for j in 0...3
      next if i == 1 && j == 1
      ok = false unless point.include?([x[i], y[j]])
    end
  end
  puts ok? "respectable" : "ugly"
else
  puts "ugly"
end


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

float n1, n2

n1=(a[0]+a[1]+a[2])/5
n2=(b[0]+b[1]+b[2])/10

if(n1.ceil+n2.ceil<=n)
    puts "YES"
else
    puts "NO"


l,r = gets.split

k=0

l1 = l.clone
r1 = r.clone

l1[-1] = l1[0]
if l1.to_i<l.to_i then
  l1=(l1.to_i+10).to_s
  l1[-1]=l1[0]
end

if r1=='10' then r1='9'
r1[-1] = r1[0]
if r1.to_i>r.to_i then
  r1=(r1.to_i-10).to_s
  r1[-1]=r1[0]
end

def count(b,e,x)
  (e-b+1)*(10**([x-2 ,0].max))
end

def ans(s)
  z=0
  (1..s.length-1).each do |por|
    z+=count(1,9,por)
  end
  z+=count(1,s[0].to_i-1,s.length)+s[1..-2].to_i+1
end
puts r1
k+=ans(r1)
k-=ans(l1)
k+=1
puts k


#!/usr/bin/ruby

require 'scanf'

r1, c1, r2, c2 = scanf("%d %d %d %d")
rookLine = ""
bLine = ""
kLine = ""
if(r1 == r2 && c1 == c2) then
  rookLine = "0"
  bLine = "0"
  kLine = "0"
elsif (r1 == r2 || c1 == c2) then
  rookLine = "1"
else rookLine = "2"
end
if((c1 + r1)%2 != (c2 + r2) % 2) then
  bLine = "0"
elsif (c1 - r1) == (c2 - r2) then
  bLine = "1"
else bLine = "2"

kLine = "#{ max((r1 - r2).abs, (c1 - c2).abs)}"
print(rookLine + " " + bLine + " " + kLine)


n,a,b,c = gets.chomp.split(/ /).map!{|x| x.to_i}
maxa = a / 2
maxab = maxa + b
minc = [(n - maxab+1)/2, 0].max
maxc = [n/2, c].min

ret = 0
for i in minc..maxc
	forb = n - i*2
	minb = [forb - maxa, 0].max
	maxb = [forb, b].min
	ret += maxb - minb + 1
end
puts ret


v, w = gets.split.map &:to_i
t, d = gets.split.map &:to_i
sum = v + w
3.upto(t) do (v < w)? ( v += d; sum += v) : (w += d; sum += w) end
puts sum


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

# b.rb

def main()
    v1, v2 = gets.chomp.split(" ").map{|s| next s.to_i }
    t, d = gets.chomp.split(" ").map{|s| next s.to_i }

    r = 0
    t.times do |tt|
        t1 = tt
        t2 = t - 1 - tt
        r += [v1 + d * t1, v2 + d * t2].min
    end
    puts r

end

if __FILE__ == $0 then
    main()
end


st, fin = gets.split(' ').map(&:to_i)
num, diff = gets.split(' ').map(&:to_i)

st -= diff
fin -= diff
sum_st = 0
sum_fin = 0
(0...num).each do |i|
  l, r = i, num-1-i
  st += diff
  sum_st += st
  fin += diff
  sum_fin += fin
  if l == r || (r-l) == 1
    if (st-fin).abs <= diff
      if l == r
        puts sum_st + sum_fin - [st,fin].max
      else
        puts sum_st + sum_fin
      end
      exit
    end

    if l != r
      fin += diff
      sum_fin += fin
    end

    if st > fin
      l.downto(0) { |i|
        if (st-fin).abs <= diff
          puts sum_st + sum_fin - [st,fin].max
          exit
        end
        sum_st -= st
        st -= diff
        fin += diff
        sum_fin += fin
      }
    else
      (l...num).each do |i|
        if (st-fin).abs <= diff
          puts sum_st + sum_fin - [st,fin].max
          exit
        end
        sum_fin -= fin
        fin -= diff
        st += diff
        sum_st += st
      end
    end
  end
end


v1,v2 = gets.split.map(&:to_i)
t,d  = gets.split.map(&:to_i)

sum = v1 + v2
3.upto(t).each do |_|
  if v1 < v2
    v1 += d
    sum += v1
  else
    v2 += d
    sum += v2
  end
end

puts sum


DEBUG = false

start_v, end_v = gets.strip.split(' ').map &:to_i
time, delta = gets.strip.split(' ').map &:to_i

start_v, end_v = end_v, start_v if start_v > end_v

sum = start_v
current_v = start_v
2.upto(time) do |current_time|
  max_v = end_v + (time - current_time) * delta
  current_v = [max_v, current_v + delta].min
  sum += current_v

  if DEBUG
    puts "#{current_time}: max_v = #{max_v}, possible_curent_v = #{current_v + delta}, max_v = #{max_v}, sum = #{sum}"
  end
end

puts sum


st, fin = gets.split(' ').map(&:to_i)
num, diff = gets.split(' ').map(&:to_i)

sum_st = st
sum_fin = fin
(1...num).each do |i|
  l, r = i, num-1-i
  st += diff
  sum_st += st
  fin += diff
  sum_fin += fin
  if l == r || (r-l) == 1
    if (st-fin).abs <= diff
      if l == r
        puts sum_st + sum_fin - [st,fin].max
      else
        puts sum_st + sum_fin
      end
      exit
    end

    if l != r
      fin += diff
      sum_fin += fin
    end

    if st > fin
      l.downto(0) { |i|
        if (st-fin).abs <= diff
          puts sum_st + sum_fin - [st,fin].max
          exit
        end
        sum_st -= st
        st -= diff
        fin += diff
        sum_fin += fin
      }
    else
      (l...num).each do |i|
        if (st-fin).abs <= diff
          puts sum_st + sum_fin - [st,fin].max
          exit
        end
        sum_fin -= fin
        fin -= diff
        st += diff
        sum_st += st
      end
    end
  end
end


v1, v2 = gets.split.collect{|i| i.to_i}.minmax
t, d = gets.split.collect{|i| i.to_i}
if d.zero?
    puts v1*t
    exit
end
x = (1.0*d*t+v2-v1)/(2*d).round
ans = v1*x+d*(x-1)*x/2 + v2*(t-x)+d*(t-x-1)*(t-x)/2
puts ans


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


n = gets.to_i
s = gets
u = d = l = r = 0
s.each_char do |i|
  if i == 'U'
    u = u + 1
  elsif i == 'D'
    d = d + 1
  elsif i == 'L'
    l = l + 1
  elsif i == 'R'
    r = r + 1
  end
end
p 2 * ([l, r].min + [u, d].min)


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


MOD = 1000000007

def pow(x,n)
  if n == 0
    1
  elsif n % 2 == 0
    pow(x*x%MOD, n/2) % MOD
  else
    x * pow(x, n-1) % MOD
  end
end

n = gets.to_i
sum = 1
1.upto(n) do |i|
  sum *= i
  sum %= MOD
end
minus = pow(2,n-1)
sum -= minus
sum %= MOD
puts sum


mod=1000000007

n=gets.to_i
ans=1

1.upto(n) do|i|
    ans=ans*i
    ans%=mod
end

power=1

1.upto(n-1) do|i|
    power=power*2
    power%=mod
end

ans=(ans-power)%mod

if ans<0
    ans+=mod
end

puts ans


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

ev = ary.inject(:+)
(ary.max).upto(1000).each do |k|
  av = ary.map{|x| [0,k-x].max }.inject(:+)
  if av>ev
    puts k
    break
  end
end


n = gets.to_i
a = gets.split.map &:to_i
f = a.inject(0) { |s, x| s + x }
(a.max..201).each do |k|
  s = a.inject(0) { |s, x| s + k - x }
  if s > f
    puts k
    exit
  end
end


n = gets.to_i
a = gets.split.map &:to_i
f = a.inject { |s, x| s + x }
(a.max..201).each do |k|
  s = a.inject { |s, x| s + k - x }
  if s > f
    puts k
    exit
  end
end


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

as_max = as.max

e = as.inject(:+)
aw = as.map { |a| as_max - a }.inject(:+)
ans = -1
(as_max..100).each do |a|
  if aw + (a-as_max)*n > e
    ans = a
    break
  end
end
puts ans


n,a=$<.read.split.map &:to_i
p [*a,2*a.reduce(:+)/n+1].max


n = gets.to_i
d = []
n.times do |i|
    d.push(gets.split.map(&:to_i))
end
foo = []
1.upto((1 << n)-1) do |i|
    hoge = []
    n.times do |j|
        if (i & (1 << j)) > 0
            hoge.push(j)
        end
    end
    hoge.permutation.to_a.each do |arr|
        (6 ** arr.size).times do |j|
            tmp = 0
            arr.size.times do |k|
                idx = j % 6
                j /= 6
                tmp *= 10
                tmp += d[arr[k]][idx]
            end
            foo.push(tmp)
        end
    end
end
foo.uniq!.sort!
if foo[0] == 0
    foo.shift
end
ans = 0
idx = 0
loop {
    if foo[idx] == ans + 1
        ans += 1
        idx += 1
    else
        break
    end
    }
puts ans


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

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

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

puts solve(cubes.size, cubes)


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

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

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

    if flag
      return true
    end
  }
  return false
}

result = 0

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


n = gets.to_i
d = []
n.times do |i|
    d.push(gets.split.map(&:to_i))
end
foo = []
1.upto((1 << n)-1) do |i|
    hoge = []
    n.times do |j|
        if (i & (1 << j)) > 0
            hoge.push(j)
        end
    end
    hoge.permutation.to_a.each do |arr|
        (6 ** arr.size).times do |j|
            tmp = 0
            arr.size.times do |k|
                idx = j % 6
                j /= 6
                tmp *= 10
                tmp += d[arr[k]][idx]
            end
            foo.push(tmp)
        end
    end
end
foo = foo.uniq.sort
if foo[0] == 0
    foo.shift
end
ans = 0
idx = 0
loop {
    if foo[idx] == ans + 1
        ans += 1
        idx += 1
    else
        break
    end
    }
puts ans


def cmmdc a,b
  return a if a==b
  return cmmdc([a,b].min,(a-b).abs)
end

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

cmm = cmmdc(a,b)
a /= cmm
b /= cmm

p = n/a
q = m/b

min = [p,q].min

p = min * a
q = min * b

delta_center_x = p/2.0
delta_center_y = q/2.0

c1x =  delta_center_x
c1y =  delta_center_y

c2x = n - delta_center_x
c2y = m - delta_center_y

rx = 0
ry = 0

puts [x,y].join(" ")
puts [c1x,c1y].join(" ")
puts [c2x,c2y].join(" ")

if x<c1x and y<c1y
  rx = c1x
  ry = c1y
end

if x<c1x and y>c2y
  rx = c1x
  ry = c2y
end

if x>c2x and y<c1y
  rx = c2x
  ry = c1y
end

if x>c2x and y>c2y
  rx = c2x
  ry = c2y
end

if x>=c1x and x<=c2x and y<c1y
  ry = c1y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if x>=c1x and x<=c2x and y>c2y
  ry = c2y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if y>=c1y and y<=c2y and x<c1x
  rx = c1x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>c2x
  rx = c2x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>=c1x and x<=c2x
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

puts [rx-delta_center_x,ry-delta_center_y,rx+delta_center_x,ry+delta_center_y].map {|x| x.to_i}.join(' ')


def cmmdc a,b
  return a if a==b
  return cmmdc([a,b].min,(a-b).abs)
end

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

cmm = cmmdc(a,b)
a /= cmm
b /= cmm

p = n/a
q = m/b

min = [p,q].min

p = min * a
q = min * b

delta_center_x = p/2.0
delta_center_y = q/2.0

c1x =  delta_center_x
c1y =  delta_center_y

c2x = n - delta_center_x
c2y = m - delta_center_y

rx = 0
ry = 0

if x<c1x and y<c1y
  rx = c1x
  ry = c1y
end

if x<c1x and y>c2y
  rx = c1x
  ry = c2y
end

if x>c2x and y<c1y
  rx = c2x
  ry = c1y
end

if x>c2x and y>c2y
  rx = c2x
  ry = c2y
end

if x>=c1x and x<=c2x and y<c1y
  ry = c1y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if x>=c1x and x<=c2x and y>c2y
  ry = c2y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if y>=c1y and y<=c2y and x<c1x
  rx = c1x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>c2x
  rx = c2x
  if (c1y-y).ceil == (c1y-y).floor
    ry = c1y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>=c1x and x<=c2x
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

puts [rx-delta_center_x,ry-delta_center_y,rx+delta_center_x,ry+delta_center_y].map {|x| x.to_i}.join(' ')


def cmmdc a,b
  return a if a==b
  return cmmdc([a,b].min,(a-b).abs)
end

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

cmm = cmmdc(a,b)
a /= cmm
b /= cmm

p = n/a
q = m/b

min = [p,q].min

p = min * a
q = min * b

delta_center_x = p/2.0
delta_center_y = q/2.0

c1x =  delta_center_x
c1y =  delta_center_y

c2x = n - delta_center_x
c2y = m - delta_center_y

rx = 0
ry = 0

if x<c1x and y<c1y
  rx = c1x
  ry = c1y
end

if x<c1x and y>c2y
  rx = c1x
  ry = c2y
end

if x>c2x and y<c1y
  rx = c2x
  ry = c1y
end

if x>c2x and y>c2y
  rx = c2x
  ry = c2y
end

if x>=c1x and x<=c2x and y<c1y
  ry = c1y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if x>=c1x and x<=c2x and y>c2y
  ry = c2y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if y>=c1y and y<=c2y and x<c1x
  rx = c1x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>c2x
  rx = c2x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>=c1x and x<=c2x
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

puts [rx-delta_center_x,ry-delta_center_y,rx+delta_center_x,ry+delta_center_y].map {|x| x.to_i}.join(' ')


def cmmdc a,b
  return [a,b].max if a*b==0
  return cmmdc([a,b].min,[a,b].max % [a,b].min)
end

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

cmm = cmmdc(a,b)
a /= cmm
b /= cmm

p = n/a
q = m/b

min = [p,q].min

p = min * a
q = min * b

delta_center_x = p/2.0
delta_center_y = q/2.0

c1x =  delta_center_x
c1y =  delta_center_y

c2x = n - delta_center_x
c2y = m - delta_center_y

rx = 0
ry = 0

if x<c1x and y<c1y
  rx = c1x
  ry = c1y
end

if x<c1x and y>c2y
  rx = c1x
  ry = c2y
end

if x>c2x and y<c1y
  rx = c2x
  ry = c1y
end

if x>c2x and y>c2y
  rx = c2x
  ry = c2y
end

if x>=c1x and x<=c2x and y<c1y
  ry = c1y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if x>=c1x and x<=c2x and y>c2y
  ry = c2y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if y>=c1y and y<=c2y and x<c1x
  rx = c1x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>c2x
  rx = c2x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>=c1x and x<=c2x
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

puts [rx-delta_center_x,ry-delta_center_y,rx+delta_center_x,ry+delta_center_y].map {|x| x.to_i}.join(' ')


n = gets.to_i

k = Math.sqrt(n).to_i

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


n = gets.to_i
a = gets.split.map(&:to_i)
sum = a[0]
ans = [1]
(1..n-1).each do |i|
  if a[0] >= a[i]*2
    ans << i+1
    sum += a[i]
  end
end
if sum*2 > a.inject(:+)
  p ans.length
  puts ans.join(" ")
else
  p 0
end


n = gets.to_i
a = gets.split.map(&:to_i)
sum = a.inject(:+)
if a[0]*2 > sum
    puts 1,1
else
    ans = [1]
    s = a[0]
    (1...n).each do |i|
        if a[i]*2 <= a[0]
            ans << i+1
            s += a[i]
        end
    end
    if s*2 > sum
        puts ans.size,ans.join(" ")
    else
        puts 0
    end
end


n = gets.strip.to_i
alice, *other = gets.strip.split(' ').map(&:to_i)
total = alice + other.reduce(:+)
half = (total / 2).to_i

res = []
origin = other.dup
other.sort!

other.each do |s|
  if alice < s * 2
    break
  end
  res << s
end

if ((res.inject(:+) || 0) + alice) <= half
  puts 0
  exit
end

puts res.size + 1
puts ([1] + res.map do |e|
  idx = origin.find_index(e)
  origin[idx] = -1
  idx + 2
end).join(' ')


n,m,k=gets.split.map(&:to_i)
ans=0
MOD=1000000007
if k==1 || k>n then
        ans=1
        n.times{ ans=(ans*m)%MOD}
        p ans
elsif k % 2==0 then
        p m
elsif k % 2==1
        p m*m
end


Mod = (10**9)+7

def solve n,m,k
  if n<k then
    0
  elsif k == 1
    ans = 1
    for i in 0 ... n
      ans = ans * m
      ans = ans % Mod
    end
    ans
  elsif n == k
    ans = 1
    for i in 0 ... ((n+1)/2)
      ans = ans * m
      ans = ans%Mod
    end
    ans
  else
    m
  end
end

while line = gets
  a = line.split
  puts solve(a[0].to_i,a[1].to_i,a[2].to_i)
end


Mod = (10**9)+7

def solve n,m,k
  k = n if n<k
  if k == 1
    ans = 1
    for i in 0 ... n
      ans = ans * m
      ans = ans % Mod
    end
    ans
  elsif n == k
    ans = 1
    for i in 0 ... ((n+1)/2)
      ans = ans * m
      ans = ans%Mod
    end
    ans
  else
    m
  end
end

while line = gets
  a = line.split
  puts solve(a[0].to_i,a[1].to_i,a[2].to_i)
end


n,m,k=gets.split.map(&:to_i)
ans=0
MOD=1000000007
if k==1 then
        ans=1
        n.times{ ans=(ans*m)%MOD}
        p ans
elsif k % 2==0 then
        p m
elsif k % 2==1
        p m*m
end


Mod = (10**9)+7

def solve n,m,k
  if n<k then
    0
  elsif k == 1
    ans = 1
    for i in 0 ... n
      ans = ans * m
      ans = ans % Mod
    end
    ans
  else
    m
  end
end

while line = gets
  a = line.split
  puts solve(a[0].to_i,a[1].to_i,a[2].to_i)
end


Mod = (10**9)+7

def solve n,m,k
  if n == k
    ans = 1
    for i in 0 ... ((n+1)/2)
      ans = ans * m
      ans = ans%Mod
    end
    ans
  elsif k == 1 || n < k
    ans = 1
    for i in 0 ... n
      ans = ans * m
      ans = ans % Mod
    end
    ans
  elsif k%2 == 1
    (m*m)%Mod
  elsif k%2 == 0
    m
  end
end

while line = gets
  a = line.split
  puts solve(a[0].to_i,a[1].to_i,a[2].to_i)
end


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


k = gets.to_i

str = "ab"

i = 1
while 0 <= k-2*i
    str << "ab"
    k -= 2*i
    i += 1
end

('c'..'z').each{|c|
    break if k == 0
    str << c
    i = 1
    while 0 <= k-i
        str << c
        k -= i
        i += 1
    end
}
puts str
#p str.size


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


class Integer
  def divisers
    n=self
    res = []
    res << 1
    i=1
    while i*i <= n
      if n%i==0
        res << i
        res << n/i if i*i!=n
      end
      i+=1
    end
    res.sort!
  end
end

def solve n,m,k
  ds = n.divisers
  if n-1+m-1<k
    return -1
  end
  ans=0
  for w1 in ds
    k1=n/w1-1
    k2=k-k1
    if 0<=k1 && k1<=k && 0<=k2 && k2<=k
      w2=m/(k2+1)
      ans = [ans, w1*w2].max
    end
  end
  ans
end

n,m,k = gets.chomp.split.map{|i|i.to_i}
puts [solve(n,m,k),solve(m,n,k)].min


n,m,k=gets.split.map(&:to_i)
if k>n+m-2
  p -1
else
  r=[n/(k+1)*m,m/(k+1)*n]
  r << n/(k-m+2) if k-m+2 >0
  r << m/(k-n+2) if k-n+2 >0
  p r.max
end


class Integer
  def divisers
    n=self
    res = []
    res << 1
    i=1
    while i*i <= n
      if n%i==0
        res << i
        res << n/i if i*i!=n
      end
      i+=1
    end
    res.sort!
  end
end

def solve n,m,k
  ds = n.divisers
  if n-1+m-1<k
    return -1
  end
  ans=0
  for w1 in ds
    k1=n/w1-1
    k2=k-k1
    if 0<=k1 && k1<=k && 0<=k2 && k2<=k
      w2=m/(k2+1)
      ans = [ans, w1*w2].max
    end
  end
  ans
end

n,m,k = gets.chomp.split.map{|i|i.to_i}
puts [solve(n,m,k),solve(m,n,k)].max


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

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

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


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


#ruby 2.4.1

require "prime"
t = gets.to_i
t.times do |a, b|
	flag = false
	nums = ""
	nums = gets
	a1, b1 = nums.split(' ')
	a = a1.to_i
	b = b1.to_i
	if ((a-b)*(a+b)) == (a + b)
		puts "YES"
	else
		puts "NO"
	end
end


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


n=gets.to_i
k=n.size-1
p (n-('9'*k).to_i).to_s.chars.map(&:to_i).reduce(:+)+9*k


x = gets.chomp
n = gets.chomp.to_i

max = 0
(n..x.size).each do |l|
  y = x + x[-l..-l+n-1]
  # puts x[-l..-l+n-1]
  1.upto(y.length/2).each do |k|
    max = k*2 if y[-k..-1] == y[-2*k..-k-1] && max < k*2
  end
end

puts max


#! /usr/bin/env ruby
x = gets.chomp
n = gets.to_i

x << '*'*n
max = 0
(0..(x.size-1)).each do |st|
  en = st + 1

  while en < x.size
    len = en-st+1
    half_en = st + (len)/2
    # puts [x[st..en], st, en, len, half_en, x[st..half_en-1], x[half_en..en]].inspect
    flag = true
    (len/2-1).times do |i|

      # puts [x[st+i], x[half_en+i], st+i, half_en+i].inspect
      if x[st+i] != x[half_en+i] && x[half_en+i] != '*'
        flag = false
        break
      end
    end

    # puts [flag, len, max, x[st..en]].inspect
    # puts ''
    max = len if flag && max < len
    en +=2
  end
end

puts max


#!/usr/bin/ruby
s=gets.chomp
k=gets.to_i
l=s.size
m=l+k
m=m-m%2
if k>=l
	p m
else
	ma=0
	l.times{|i|
		1.step(l-i){|j|
			if 2*(i+j)>ma && i.step(i+j-1).all?{|k|(o+j>=l&&o+j<l+k)||a[o]==a[o+j]}
				ma=2*(i+j)
			end
		}
	}
	p ma
end


x = gets.chomp
n = gets.chomp.to_i

(n..x.size).each_with_index do |l, i|
  y = x + x[-l..-i-1]

  1.upto(y.length/2).each do |k|
    max = k*2 if y[-k..-1] == y[-2*k..-k-1] && max < k*2
  end
end


a,b,w,x,c=gets.split.map(&:to_i)
h={}
i=0
while true
  if c<=a
    p i
    exit
  end
  if b>=x
    bb=b-x
  else
    a-=1
    bb=w-(x-b)
  end
  break if h[bb]!= nil
  b=bb
  h[b]=[i,a]
  i+=1
  c-=1
end
cl=i-h[bb][0]
da=h[bb][1]-a
d=(c-a)/(cl-da)-1
a-=da*d
c-=d*cl
i+=d*cl
while c>a
  if b>=x
    b-=x
  else
    a-=1
    b=w-(x-b)
  end
  c-=1
  i+=1
end
p i


a,b,w,x,c=gets.split.map(&:to_i)
h={}
i=0
aa=a
while true
  if c<=a
    p i
    exit
  end
  if b>=x
    bb=b-x
  else
    aa=a-1
    bb=w-(x-b)
  end
  break if h[bb]!= nil
  b=bb
  a=aa
  h[b]=[i,a]
  i+=1
  c-=1
end
cl=i-h[bb][0]
da=h[bb][1]-aa
d=(c-a)/(cl-da)-1
a-=da*d
c-=d*cl
i+=d*cl
while c>a
  if b>=x
    b-=x
  else
    a-=1
    b=w-(x-b)
  end
  c-=1
  i+=1
end
p i


a,b,w,x,c=gets.split.map(&:to_i)
h={}
i=0
while true
  if c<=a
    p i
    exit
  end
  if b>=x
    b-=x
  else
    a-=1
    b=w-(x-b)
  end
  break if h[b]!= nil
  h[b]=[i,a]
  i+=1
  c-=1
end
cl=i-h[b][0]
da=h[b][1]-a
d=(c-a)/cl
a-=da*d
c-=d*cl
i+=d*cl
while c>a
  if b>=x
    b-=x
  else
    a-=1
    b=w-(x-b)
  end
  c-=1
  i+=1
end
i-=2 if d>0
p i


MOD = 1000000009

def clamp(x)
	x < 0 ? (x + MOD) : x
end

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

pow2m = 1

m.times { |_| pow2m = (pow2m << 1) % MOD }

ans = 1

(1..n).each do |i|
	ans = (ans * clamp(pow2m - i)) % MOD
end

puts ans


n,m=gets.split.map(&:to_i)
h={}
m.times do
  a,b=gets.split.map(&:to_i)
  h[[a,b]]=h[[b,a]]=1
end
a=(1..n).to_a
s=Time.now
while Time.now-s < 2.8
  b=a.shuffle
  f=true
  m.times do |i|
    if !h[[b[i],b[(i+1)%n]]].nil?
      f=false
      break
    end
  end
  if f
    m.times do |i|
      puts "#{b[i]} #{b[(i+1)%n]}"
    end
    exit
  end
end
p -1


def sum(x)
	y=((x+1)*x)/2
end
m=gets.chomp.to_i
b=gets.chomp.to_i
max=0
(b+1).times do |y|
	x=(-y*m)+b*m
	s=(y+1)*sum(x)+(x+1)*sum(y)
	max= max > s ? max : s
end
p max


def sum(x)
	y=((x+1)*x)/2
end
m,b  = gets.split.map(&:to_i)
max=0
(b+1).times do |y|
	x=(-y*m)+b*m
	s=(y+1)*sum(x)+(x+1)*sum(y)
	max= max > s ? max : s
end
p max


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

x,y,z = lscan

pl = x + z > y
mi = y + z > x

if pl && mi
  puts "?"
elsif pl
  puts "+"
elsif mi
  puts "-"
else
  puts "0"
end


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

x,y,z = lscan

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

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


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


a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
cnt =0
(0..a.length - 1).each{|i|
    if a[i] == b[i]
        p "YES"
        break
    else
        cnt += 1
    end
}
if cnt == a.length
    p "NO"
end


a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
cnt =0
(0..a.length - 1).each{|i|
    if a[i] == b[i]
        puts "YES"
        break
    else
        cnt += 1
    end
}
if cnt == a.length
    puts "NO"
end


a = gets.chomp.split(/ /).map!{|x|x.to_i}
b = gets.chomp.split(/ /).map!{|x|x.to_i}
ret = "NO"
for i in 0..2
	if a[i] == b[i]
		ret = "YES"
	end
end

puts ret


$MAXN = 2222;
$P = Integer(1e9 + 7);

def powmod(a,b,c)
  r=1;
  while b != 0
    if( (b&1) == 1 )
      r = r * a % c;
    end
    a = a * a % c;
    b /= 2;
  end
  return r;
end

def inv(a, b )
  return powmod( a, b - 2 , b ) ;
end

def facmod(n)
  r=1;
  (1..n).each{ |i|
    r = r * i % $P;
  }
  return r;
end

def C(n,m)
  if(m == 0)
    return 1;
  end;
  if(m>n)
    return 0;
  end;
  return (facmod(n) * inv( facmod(m), $P) * inv( facmod(n-m), $P ) ) % $P ;
end

n, m, k = gets().split(" ").map(&:to_i);
printf "%d\n", (C(n-1,2*k) * C(m-1,2*k) )% $P;


a=gets
s=gets
if s["00"].nil? && s["11"].nil?
    puts "Yes"
else
    puts "No"
end


low=[]
st=[]
cur=1989
step=10
(1..10).each{|i|
	len=cur.to_s.length
	st<<cur.to_s[len-i..len-1]
	low<<cur
	cur+=step
	step*=10
}
gets.to_i.times{
	x=gets[4..99].strip
	dig=x.length
	delta=x.to_i-st[dig-1].to_i
	delta+=10**dig if delta<0
	puts low[dig-1]+delta
}


#!/usr/bin/env ruby

n, l, r = gets.split.map(&:to_i)
#                  0:  1:&1 2:size 3:1_c
one_map = { 1 => [ [1], 1, 1, 1 ],
			0 => [ [0], 0, 1, 0 ] }

stack = []

c = n
while c > 1
  stack << c
  d = c >> 1
  one_map[c] = [d, c&1]
  c = d
end

#p stack

last = one_map[c]
while d = stack.pop
  cur = one_map[d]

  # size
  cur[2] = last[2] * 2 + 1
  # 1_c
  cur[3] = last[3] * 2 + cur[1]

  last = cur
end

c_start = l - 1
c_end = r - 1

def give_one_count(n, c_s, c_e, one_map)
  #p "n:#{n} c_s:#{c_s} c_e:#{c_e} one_map:#{one_map}"
  n_map = one_map[n]

  return n_map[3] if c_s == 0 && (c_e + 1) == n_map[2]

  mid = n >> 1
  mid_size = n_map[2] >> 1

  count = 0

  if c_s < mid_size
	#p "#{n} a"
    count += give_one_count(mid, c_s, [c_e, mid_size - 1].min, one_map)
  end

  if mid_size >= c_s && mid_size <= c_e
	#p "#{n} m"
	count += n_map[1]
  end

  if c_e > mid_size
	#p "#{n} b"
    count += give_one_count(mid, [c_s - 1 - mid_size, 0].max , c_e - 1 - mid_size, one_map)
  end

  count
end

puts give_one_count(n, c_start, c_end, one_map)


/(?<a>[\d]+) (?<b>[\d]+)/ =~ gets.chomp
a = a.to_i
b = b.to_i

def bite(size, mults=[2, 3, 5])	# => [b*2, b*3, b*5, leftovers || 1]
	bitesizes = []
	mults.each do |mult|
		bite = 0
		bite, size = (bite+1), (size/mult) while (size%mult)==0
		bitesizes << bite
	end
	bitesizes << size
	return bitesizes
end

*bitesA, leftA = bite(a)
*bitesB, leftB = bite(b)

if (leftA != leftB)
	puts -1
else
	p (bitesA.zip(bitesB).map{ |bA, bB|
		(bA-bB).abs
	}.reduce(&:+))
end


a,b=gets.chomp.split(' ').map(&:to_i)
tmp=a.gcd(b)
a/=tmp
b/=tmp
ctr=0
while a%2==0
	a/=2
	ctr+=1
end
while b%2==0
	b/=2
	ctr+=1
end
while a%3==0
	a/=3
	ctr+=1
end
while b%3==0
	b/=3
	ctr+=1
end
while a%5==0
	a/=5
	ctr+=1
end
while b%5==0
	b/=5
	ctr+=1
end
if a+b > 2
	puts "-1"
else
	puts ctr
end


a = gets.chomp.to_i
b = gets.chomp.to_s

# check if valid
COLORS = ['C', 'M', 'Y']
$valid = true
def valids(b)
	prev = nil
	b.split("").each do |char|
		if !(char == '?' || char != prev)
			return false
		end
		prev = char
	end
	return true
end
ll = b.length
def permute(b, cols, i, str, out)
	if i == b.length
		out.push(str)
	elsif out.length > 3
		return out
	else
		prevc = nil
		prevc = str[i-1] if i > 0
		nextc = nil
		while str[i] != '?' && i < b.length
			prevc = str[i]
			i += 1
		end
		nextc = b[i+1]
		if i < b.length
			cola = cols - [prevc, nextc]
			cola.each do |col|
				str[i] = col
				out = permute(b, cols, i+1, str.dup, out)
				str[i] = '?'
			end
		else
			out.push(str)
		end
	end
	return out
end
if valids(b)
	out = permute(b, COLORS, 0, b.dup, [])
	if out.length == 2
		puts "Yes"
	else
		puts "No"
	end
	# puts out
else
	puts "No"
end


a = gets.chomp.to_i
b = gets.chomp.to_s

# check if valid
COLORS = ['C', 'M', 'Y']
$valid = true
def valids(b)
	prev = nil
	b.split("").each do |char|
		if !(char == '?' || char != prev)
			return false
		end
		prev = char
	end
	return true
end
ll = b.length
def permute(b, cols, i, str, out)
	if i == b.length
		out.push(str)
	elsif out.length < 4
		prevc = nil
		prevc = str[i-1] if i > 0
		nextc = nil
		while str[i] != '?' && i < b.length
			prevc = str[i]
			i += 1
		end
		nextc = b[i+1]
		if i < b.length
			cola = cols - [prevc, nextc]
			cola.each do |col|
				str[i] = col
				out = permute(b, cols, i+1, str.dup, out)
				str[i] = '?'
			end
		else
			out.push(str)
		end
	end
	return out
end
if valids(b)
	out = permute(b, COLORS, 0, b.dup, [])
	if out.length >= 2
		puts "Yes"

	else
		puts "No"
	end
else
	puts "No"
end


n = gets.to_i
p = gets.split.map(&:to_i).map{|x| x - 1}

if p.uniq.size != n
  puts(-1)
  exit
end

ls = []
n.times do |i|
  x = p[i]
  len = 1
  until x == i
    x = p[x]
    len += 1
  end
  ls << len
end

ans = 1
ls.each do |l|
  l /= 2 if l.even?
  ans = ans.lcm(l)
end
puts ans


def f(x,y,x1,y1)
	return (x-x1).abs+(y-y1).abs
end
x,y=gets.split.map{|e| e.to_i}
x1,y1=gets.split.map{|e| e.to_i}
x,y=(x-x1).abs,(y-y1).abs
x,y=[x,y].min,[x,y].max
ans=0
if x==0
	ans=6+y*2+f(x-1,y,-1,1)+f(x+1,y,1,1)
else
	ans=4+f(x,y,-1,1)+f(x,y,1,-1)
end
puts ans


def f(x,y,x1,y1)
	return (x-x1).abs+(y-y1).abs
end
x,y=gets.split.map{|e| e.to_i}
x1,y1=gets.split.map{|e| e.to_i}
x,y=(x-x1).abs,(y-y1).abs
x,y=[x,y].min,[x,y].max
ans=0
if x==0
	ans=6+f(x,y,-1,1)+f(x,y,1,1)
else

	ans=4+f(x,y,-1,1)+f(x,y,1,-1)
end
puts ans


n,p,w,d = readline.split.map(&:to_i)

y = 0
r = p % w
while r != 0 and y < w do
  y += 1
  r -= d
  if r < 0 then
    r += w
  end
end

x = (p - y * d) / w
z = n - x - y

print(if x >= 0 and y < w and z >= 0 then [x,y,z]*" " else -1 end)


def check(as, bs)
    ax = [0,2,4,6].map{|i|as[i]}
    ay = [1,3,5,7].map{|i|as[i]}
    bcx = [0,2,4,6].map{|i|bs[i]}.inject(:+)/4.to_f
    bcy = [1,3,5,7].map{|i|bs[i]}.inject(:+)/4.to_f
    [bs[0,2], bs[2,2], bs[4,2], bs[6,2], [bcx,bcy]].each do |bx, by|
        if bx.between?(ax.min, ax.max) && by.between?(ay.min, ay.max)
            puts 'YES'
            exit
        end
    end
end

as = gets.split.map(&:to_i)
bs = gets.split.map(&:to_i)

check(as, bs)
4.times do |i|
    as[2*i], as[2*i+1] = [as[2*i] + as[2*i+1], as[2*i] - as[2*i+1]]
    bs[2*i], bs[2*i+1] = [bs[2*i] + bs[2*i+1], bs[2*i] - bs[2*i+1]]
end
check(bs, as)
puts 'NO'


def ascan; gets.split.map(&:to_i); end
require 'prime'

def funcd(n, div)
    total = 0
    while n >= div
        total += n/div
        n /= div
    end
    total
end

n,b = ascan

p b.prime_division.map{|pr,nm| funcd(n,pr)/nm}.min


require 'date'

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

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

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


def read_next_line
  gets.chomp.split.map(&:to_i)
end

def read(range)
  g = []
  range.each {
    g << read_next_line
  }
  g
end

matrix_size = 5

r = (0...matrix_size)
g = read(r)
max = 0
r.to_a.permutation.each { |p|
  happ = 0
  line = r.to_a
  while line.size > 1
    n = line.size
    n -= n % 2
    (0...n).step(2) { |i|
      happ += g[p[line[i]]][p[line[i + 1]]] + g[p[line[i + 1]]][p[line[i]]]
    }
    line.shift
  end
  max = happ if happ > max
}
puts max


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

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


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


END { load __FILE__ unless $stdin.eof? }

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

cnt = Array.new(n + 1, 0)
sum = Array.new(n + 2, 0)
cnt[n] = 1
sum[n] = 1

n.pred.downto(1) do |i|
  cnt[i] = sum[i+1]

  (i * 2).step(n, i) do |k|
    cnt[i] += sum[k]
    cnt[i] %= m
  end
  (i.succ * 2).step(n, i.succ) do |k|
    cnt[i] -= sum[k]
    cnt[i] %= m
  end

  sum[i] = (sum[i+1] + cnt[i]) % m
end

p cnt[1]


n, h, m = gets.split.map &:to_i
a = [h]*(n + 1)
m.times do
  l, r, x = gets.split.map &:to_i
  (l..r).each { |i| a[i] = [a[i], x].min }
end
puts a[1..-1].inject(0) { |s, x| s + x*x }


n, h, m = gets.split.map(&:to_i)
rest = Array.new(n, h)
m.times do
  l, r, x = gets.split.map(&:to_i)
  (l..r).each do |pos|
    rest[pos-1] = [rest[pos-1], x].min
  end
end

ans = 0
rest.each do |r|
  ans += r ** 2
end
puts ans


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

n, m = read_ints
a = read_ints
b = read_ints

ans = []
amb = false

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

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

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


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

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

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

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


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

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

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

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


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


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


require 'prime'

def sub(b, s)
  if b.empty?
    v = (s[0]+1)*(s[1]+2)*(s[2]+2) - s[0]*s[1]*s[2]
    return [v, v]
  end

  b = b.dup
  k, v = b.pop

  result = [1e100, 0]
  0.upto(v) do |i|
    0.upto(v-i) do |j|
      mn, mx = sub(b, [s[0]*k**i, s[1]*k**j, s[2]*k**(v-i-j)])
      result[0] = [result[0], mn].min
      result[1] = [result[1], mx].max
    end
  end

  result
end

def solve(n)
  a = Prime.prime_division(n)

  s1 = sub(a, [1, 1, 1])

=begin
  b = a.map{|k,v| [k]*v}.flatten
  r = []
  (3**b.size).times do |i|
    o = [1]*3
    ("0"*b.size+i.to_s(3))[-b.size,b.size].each_char.zip(b) do |j, k|
      o[j.to_i] *= k
    end
    r << [(o[0]+1)*(o[1]+2)*(o[2]+2) - o[0]*o[1]*o[2], o]
  end
  s2 = [r.min[0], r.max[0]]
=end
  s1
end

n = gets.to_i
puts solve(n)*" "


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

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


x=gets.to_i
(2..Math.sqrt(x)).each do |i|
    if x.modulo(i)==0
        puts "#{i}#{x/i}"
    end
end


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

pos = a.count {|x| x > 0}
neg = a.count {|x| x < 0}

target = a.length / 2.0

if pos >= target
  puts "1"
elsif neg >= target
  puts "-1"
else
  puts "0"
end


#!/usr/bin/env ruby

def test(len, inp)
  if inp[-(len + 1 - inp.size)..-1].nil? or (inp.length <= (len + 1)/2) then return "" end
  inp + inp[-(len + 1 - inp.size)..-1]
end

s = gets.strip
a = []
for i in 1..s.size-2
  sub = s[0..i]
  a.push(sub) if s.start_with? sub and s.end_with? sub
end

for i in 0..a.size-1
  if test(s.size - 1, a[i]) == s then
    puts "YES"
    puts a[i]
    exit 0
  end
end
puts "NO"


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


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


n=gets.to_i
sq,x,y=0,[],[]
n.times do
  x1,y1,x2,y2=gets.split.map(&:to_i)
  sq+=(x2-x1)*(y2-y1)
  x.push x1,x2
  y.push y1,y2
end
if (x.max-x.min)==(y.max-y.min) and (y.max-y.min)**2==sq
  puts "YES"
else
  puts "NO"
end


x,y = gets.split.map(&:to_i)
if x==y
  puts "YES"
  exit
end
bubun = []
if x[0] == 1
  bubun << x.to_s(2)
  bubun << x.to_s(2).reverse
else
  x1 = (x<<1) + 1
  bubun << x1.to_s(2)
  bubun << x1.to_s(2).reverse
  i = 0
  while x[i] == 0
    i += 1
  end
  x2 = x >> i
  bubun << x2.to_s(2)
  bubun << x2.to_s(2).reverse
end
y = y.to_s(2)
y_str = y.size
bubun.each do |str|
  len = str.size
  f = true
  i = /#{str}/ =~ y
  if i != nil
    0.upto(i-1) do |j|
      f = false if y[j] == "0"
    end
    (i+len).upto(y_str-1) do |j|
      f = false if y[j] == "0"
    end
    if f
      puts "YES"
      exit
    end
  end
end
puts "NO"


def nth_root_ceil(n, m)
  return n if m == 1
  i = 1
  j = n
  while j > i + 1 do
    mid = (i + j) / 2
    if mid ** m >= n
      j = mid
    else
      i = mid
    end
  end
  return j
end

class Solution
  def initialize(n)
    @n = n
    @x = []
    (1..n).each do |i|
      @x[i] = nth_root_ceil(n, i)
      break if @x[i] == 2
    end
  end

  def build_dp()
    @dp = Array.new(@x.size)
    (0...@x.size).each {|i| @dp[i] = Array.new(@x[2]+1, 1)}
    @dp[1][@x[2]] = 0 if ((@n - @x[2]) % 2 == 1)
    (3...@x.size).each do |m|
      (@x[m-1]-1).downto(@x[m]).each do |x|
        (m-1).downto(1).each do |y|
          @dp[y][x] = 0 if (@dp[y+1][x] == 1 and @dp[y][x+1] == 1)
        end
      end
    end
  end

  def solve_boundary_case(b)
    result = -1
    (b...@x.size).each do |i|
      if @dp[i][2] == 0
        result = (i - b) % 2 == 0 ? 1 : 0
        break
      end
    end
    return result
  end

  def solve(a, b)
    if (a > @x[2])
      result = (@n - a) % 2 == 1 ? 0 : 1
    elsif (a > 1)
      result = @dp[b][a]
    else
      result = solve_boundary_case(b)
    end
    return result
  end

  def print()
    (@dp.size-1).downto(1).each do |i|
      (2...@dp[i].size).each do |j|
        printf("%d ", @dp[i][j])
      end
      printf("\n")
    end
  end
end

a, b, n = gets.split.map(&:to_i)
# n = 2, a has to be 1, it's a draw
if n == 2
  puts "Missing"
  exit
end
solution = Solution.new(n)
solution.build_dp
answers = %w[Stas Masha Missing]
puts answers[solution.solve(a, b)]