Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well.We define a pair of integers (a, b) good, if GCD(a, b) = x and LCM(a, b) = y, where GCD(a, b) denotes the greatest common divisor of a and b, and LCM(a, b) denotes the least common multiple of a and b.You are given two integers x and y. You are to find the number of good pairs of integers (a, b) such that l ≤ a, b ≤ r. Note that pairs (a, b) and (b, a) are considered different if a ≠ b.

The only line contains four integers l, r, x, y (1 ≤ l ≤ r ≤ 109, 1 ≤ x ≤ y ≤ 109).

In the only line print the only integer — the answer for the problem.

NoteIn the first example there are two suitable good pairs of integers (a, b): (1, 2) and (2, 1).In the second example there are four suitable good pairs of integers (a, b): (1, 12), (12, 1), (3, 4) and (4, 3).In the third example there are good pairs of integers, for example, (3, 30), but none of them fits the condition l ≤ a, b ≤ r.

L, R, X, Y = gets.split.map(&:to_i)
if Y % X != 0
    puts 0
    exit
end

div = Y/X
sq = Math.sqrt(div).ceil
ans = 0
(1..sq).each do |i|
    next if div%i != 0
    j = div/i
    next if i > j
    next if i.gcd(j) != 1
    if (i*X).between?(L, R) && (j*X).between?(L, R)
        ans += i==j ? 1 : 2
    end
end
puts ans

require 'prime'

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

x_factors = Prime.prime_division(x)
y_factors = Prime.prime_division(y)
all_factors = {}
x_factors.each do |fp|
  all_factors[fp[0]] = [fp[1]]
end
y_factors.each do |fp|
  if all_factors[fp[0]]
    all_factors[fp[0]] << fp[1]
  else
    all_factors[fp[0]] = [0, fp[1]]
  end
end

if all_factors.values.map(&:size).uniq != [2]
  puts 0
  exit
end

all_factors_ary = all_factors.keys.sort
bl = all_factors_ary.size
n = 1<<bl
# puts all_factors_ary.inspect
# puts all_factors.inspect

pairs = []
0.upto(n).each do |bm|
  a = 1
  b = 1
  0.upto(bl-1).each do |s|
    f = all_factors_ary[s]
    # puts f
    if (bm & (1<<s)) > 0
      b *= (f ** all_factors[f][1])
      a *= (f ** all_factors[f][0])
    else
      a *= (f ** all_factors[f][1])
      b *= (f ** all_factors[f][0])
    end
  end
  # puts "(#{a},#{b})"
  if l<=a && a<=r && l<=b && b<=r
    pairs << [a,b]
  end
end
puts pairs.uniq.size


Special Agent Smart Beaver works in a secret research department of ABBYY. He's been working there for a long time and is satisfied with his job, as it allows him to eat out in the best restaurants and order the most expensive and exotic wood types there. The content special agent has got an important task: to get the latest research by British scientists on the English Language. These developments are encoded and stored in a large safe. The Beaver's teeth are strong enough, so the authorities assured that upon arriving at the place the beaver won't have any problems with opening the safe.And he finishes his aspen sprig and leaves for this important task. Of course, the Beaver arrived at the location without any problems, but alas. He can't open the safe with his strong and big teeth. At this point, the Smart Beaver get a call from the headquarters and learns that opening the safe with the teeth is not necessary, as a reliable source has sent the following information: the safe code consists of digits and has no leading zeroes. There also is a special hint, which can be used to open the safe. The hint is string s with the following structure:  if si = "?", then the digit that goes i-th in the safe code can be anything (between 0 to 9, inclusively);  if si is a digit (between 0 to 9, inclusively), then it means that there is digit si on position i in code;  if the string contains letters from "A" to "J", then all positions with the same letters must contain the same digits and the positions with distinct letters must contain distinct digits.  The length of the safe code coincides with the length of the hint. For example, hint "?JGJ9" has such matching safe code variants: "51919", "55959", "12329", "93539" and so on, and has wrong variants such as: "56669", "00111", "03539" and "13666".After receiving such information, the authorities change the plan and ask the special agents to work quietly and gently and not to try to open the safe by mechanical means, and try to find the password using the given hint.At a special agent school the Smart Beaver was the fastest in his platoon finding codes for such safes, but now he is not in that shape: the years take their toll ... Help him to determine the number of possible variants of the code to the safe, matching the given hint. After receiving this information, and knowing his own speed of entering codes, the Smart Beaver will be able to determine whether he will have time for tonight's show "Beavers are on the trail" on his favorite TV channel, or he should work for a sleepless night...

The first line contains string s — the hint to the safe code. String s consists of the following characters: ?, 0-9, A-J. It is guaranteed that the first character of string s doesn't equal to character 0. The input limits for scoring 30 points are (subproblem A1):    1 ≤ |s| ≤ 5.  The input limits for scoring 100 points are (subproblems A1+A2):    1 ≤ |s| ≤ 105.  Here |s| means the length of string s.

Print the number of codes that match the given hint.

#!/usr/bin/ruby
h={}
f=10
x=1
gets.chomp.each_char.with_index{|c,i|
	if i==0&&!('0'..'9').include?(c)
		f=9
	end
	if c=='?'
		x*=10
	elsif ('A'..'J').include?(c)
		h[c]=1
	end
}
p x*(11-h.size..10).reduce(1,:*)*f/10

hint = gets
temp = hint
arr = Array.new()
num = 10
res = 1
if hint[0] == '?'
  res *= 9
elsif hint[0] > '9'
  res *= 9
  hint = hint[0] + temp.gsub(hint[0],'')
  num = num - 1
end
hint[0] = ''
hint.each_char {|c|
  if c == '?'
    res *= 10
  elsif c > '9' && !(arr.include?(c))
    res *= num
    arr << c
    num = num - 1
  end
}

puts res

s = gets
ret = 1
hash = {}
for i in 0...s.size
  adjust = i == 0 ? 1 : 0
  ret *= 10 - adjust if s[i] == '?'
  if s[i] >= 'A' && s[i] <= 'J'
    ret *= 10 - hash.size - adjust unless hash[s[i]]
    hash[s[i]] = true
  end
end
puts ret

t = STDIN.gets
def ppp(start,lcounter)
  return start if lcounter == 1
  return 1 if lcounter < 1
  start*ppp(start-1, lcounter -1)
end
h = []
k = []
qcounter = 0

t.size.times do |i|
  if t[i] =~ /[A-J]/
    h << t[i] if not h.include? t[i]
  end
  if '0123456789'.include? t[i]
    k << t[i] if not k.include? t[i]
  end
  qcounter = qcounter + 1 if t[i] == '?'
end

p = h.count
p = 0 if p < 0
pp = 10

res = ((t[0] =~ /[A-J]/) ? ppp(pp-1, p-1)*(pp-1) : ppp(pp, p))* ((t[0] == '?') ? (10)**(qcounter - 1)*9 : (10)**qcounter)
puts res

#!/usr/bin/ruby
h={}
f=10
x=0
gets.chomp.each_char.with_index{|c,i|
	if i==0&&!('0'..'9').include?(c)
		f=9
	end
	if c=='?'
		x+=1
	elsif ('A'..'J').include?(c)
		h[c]=1
	end
}
r=(11-h.size..10).reduce(1,:*)
if x>0
	r*=10
	x-=1
end
r=r*f/10
puts r.to_s+'0'*x

def f(n)
  return 1 if n == 1
  return n * f(n-1)
end
s = gets
ret = 1
q = s.count('?')
c = 10
for i in 'A'..'J'
  if s.index(i)
    ret *= c
    c -= 1
  end
end
ret *= (10 ** q)
if s[0] == '?' || (s[0] >= 'A' && s[0] <= 'J')
  ret = ret / 10 * 9
end
puts ret

#!/usr/bin/ruby
h={}
f=10
x=1
gets.chomp.each_char.with_index{|c,i|
	if i==0&&!('0'..'9').include?(c)
		f=9
	end
	if c=='?'
		x*=10
	elsif ('A'..'J').include?(c)
		h[c]=1
	end
}
p x*(11-h.size..10).reduce(1,:*)/10*f


Let's consider equation:x2 + s(x)·x - n = 0,  where x, n are positive integers, s(x) is the function, equal to the sum of digits of number x in the decimal number system.You are given an integer n, find the smallest positive integer root of equation x, or else determine that there are no such roots.

A single line contains integer n (1 ≤ n ≤ 1018) — the equation parameter. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

Print -1, if the equation doesn't have integer positive roots. Otherwise print such smallest integer x (x > 0), that the equation given in the statement holds.

NoteIn the first test case x = 1 is the minimum root. As s(1) = 1 and 12 + 1·1 - 2 = 0.In the second test case x = 10 is the minimum root. As s(10) = 1 + 0 = 1 and 102 + 1·10 - 110 = 0.In the third test case the equation has no roots.

n=gets.to_i
x=Math.sqrt(n).round-200
for y in 1..400 do
	sum=0
	str=x.to_s
	for i in 0..str.size do
		sum+=str[i].to_i end;
	if x*(x+sum)==n and x>0
		puts x
		break
	end
	x+=1
	print -1 if y==400
	end

def s(x)
  res = 0
  until x == 0
    res += x%10
    x /= 10
  end
  return res
end

n = gets.to_i
x = Math.sqrt(n).floor
notfound = true
for i in [x-100000, 1].max..x
  if i**2 + s(i)*i - n == 0
    print i
    notfound = false
    break
  end
end

print -1 if notfound

#!/usr/bin/ruby2.0

n = STDIN.readline.to_i

candidates = []

(1..162).each do |b|
	x = (-b + (b*b + 4*n)**0.5) / 2
	x = x.round
	next if x*x + b*x != n
	t, sum = x, 0
	while t != 0
		sum += t%10
		t /= 10
	end
	next if sum != b
	candidates.push(x)
end

if candidates.length == 0
	puts -1
else
	puts candidates.sort[0]
end

#!/usr/bin/ruby2.0

n = STDIN.readline.to_i

candidates = []

(1..162).each do |c|
	low, high = 1, n
	while high-low > 1e-6
		mid = (low+high)/2.0
		if n/mid - mid > c
			low = mid
		else
			high = mid
		end
	end
	x = mid.round
	next if n != c*x + x*x
	t = x
	total = 0
	while t != 0
		total += t%10
		t /= 10
	end
	if total == c
		candidates.push(x)
	end
end

if candidates.length == 0
	puts -1
else
	puts candidates.sort[0]
end

n=gets.chomp.to_i
x=Math.sqrt(n).round
x-=200
for y in 1..400 do
	sum=0
	str=x.to_s
	for i in 0..str.size do
		sum+=str[i].to_i end;
	if x>0 and x*(x+sum)==n
		puts x
		break
	end
	x+=1
	print -1 if y==400
end

n = gets.to_i
x = Math.sqrt(n).round - 200
0.upto(400) do
  sum = 0
  str = x.to_s
  0.upto(str.size) do |i|
    sum += str[i].to_i
  end
  if x * (x + sum) == n and x > 0
    puts str
    exit
  end
  x += 1
end
puts -1

n = gets.to_i

def s(x)
  sum = 0
  x.to_s.each_byte { |c| sum += c - 48 }
  sum
end

def sqrt(y)
  lo, hi = 1, y
  while lo <= hi do
    mid = (lo + hi) / 2
    return mid if mid * mid == y
    lo = mid + 1 if mid * mid < y
    hi = mid - 1 if mid * mid > y
  end
  return -1
end

ans = []

for i in 1..81 do
  t = sqrt(i * i + 4 * n)
  if t != -1 then
    if t - i > 0 && (t - i) % 2 == 0 then
      ans << (t - i) / 2 if s((t - i) / 2) == i
    end
  end
end

if ans.size == 0 then
  puts "-1"
else
  puts ans.min
end

n = gets.to_i

# while l <= r
# 	x  = l + (r-l)/2
# 	s = x.to_s.split("").map(&:to_i).inject(0,:+)
# 	puts x
# 	if ((x * x) + (s * x) - n) == 0
# 		ans = x
# 		break
# 	else
# 		r = x - 1
# 	end
# end

# puts x
ans = false
min = n
(1..90).each do |s|
	a = 1
	b = s
	c = -n
	d = (b ** 2) - (4 * a * c)
	if d >=0
		r1 = (-b + Math.sqrt(d))/(2 * a)
		r2 = (-b - Math.sqrt(d))/(2 * a)
	elsif d == 0
		r1 = r2 = (-b)/(2 * a)
	end
	# puts "sum #{s} roots  #{r1} #{r2}"
	if r1 && r2
		arr = []
		sum = r1.to_i.to_s.split("").map(&:to_i).inject(0,:+)
		arr << r1 if r1 % 1 == 0 && r1 > 0 && s == sum
		sum = r2.to_i.to_s.split("").map(&:to_i).inject(0,:+)
		arr << r2 if  r2 % 1 == 0 && r2 > 0 && s == sum
		unless arr.empty?
		# puts "s #{s} ar #{r1} #{r2}"
			val = arr.min
			min = val if val && val < min
			# puts "#{val}"
			ans = true
		end
	end
end
if ans
	if min == 999959999
		puts -1
	else
	puts min.to_i
	end
else
	puts "-1"
end

def s(x)
  sum = 0
  while x>0
    sum+= x%10
    x /= 10
  end
  sum
end

n = gets.to_i
x = Math.sqrt(n).round - 160;

321.times do |i|
  if x*(x+s(x))==n && x > 0
    puts x
    exit
  end
  x+=1
end
puts -1

EPS=1e-9
def s(x)
	ret=0
	while x>0
		ret+=x%10
		x/=10
	end
	ret
end

def is_int?(_x)
	x=_x.round
	(x-EPS<=_x)&&(_x<=x+EPS)
end

n=gets.to_i
ans=100.times.map{|s|
	_x=(-s+(s*s+4*n)**0.5)/2.0
	if is_int?(_x)
		x=_x.round
		if x*x+s(x)*x-n==0
			x
		else
			nil
		end
	else
		nil
	end
}

p ans.any? ? ans.compact.min : -1

n=gets.to_i
x=n**0.5.round-200
for y in 1..400 do
	sum=0
	str=x.to_s
	for i in 0..str.size do
		sum+=str[i].to_i end;
	if x*(x+sum)==n
		puts x
		break
	end
	x+=1
	print -1 if y==400
	end

#!/usr/bin/ruby2.0

n = STDIN.readline.to_i

candidates = []

(1..162).each do |c|
	low, high = 1, n
	while high-low > 1e-6
		mid = (low+high)/2.0
		if n/mid - mid > c
			low = mid
		else
			high = mid
		end
	end
	x = mid.round
	t = x
	total = 0
	while t != 0
		total += t%10
		t /= 10
	end
	if total == c
		candidates.push(x)
	end
end

if candidates.length == 0
	puts -1
else
	puts candidates.sort[0]
end

n = gets.to_i

# while l <= r
# 	x  = l + (r-l)/2
# 	s = x.to_s.split("").map(&:to_i).inject(0,:+)
# 	puts x
# 	if ((x * x) + (s * x) - n) == 0
# 		ans = x
# 		break
# 	else
# 		r = x - 1
# 	end
# end

# puts x
ans = false
min = n
(1..90).each do |s|
	a = 1
	b = s
	c = -n
	d = (b ** 2) - (4 * a * c)
	if d >=0
		r1 = (-b + Math.sqrt(d))/(2 * a)
		r2 = (-b - Math.sqrt(d))/(2 * a)
	elsif d == 0
		r1 = r2 = (-b)/(2 * a)
	end
	# puts "sum #{s} roots  #{r1} #{r2}"
	if r1 && r2
		arr = []
		sum = r1.to_i.to_s.split("").map(&:to_i).inject(0,:+)
		arr << r1 if r1 % 1 == 0 && s == sum
		sum = r2.to_i.to_s.split("").map(&:to_i).inject(0,:+)
		arr << r2 if  r2 % 1 == 0 && s == sum
		# puts "sum #{s} #{arr}"
		val = arr.min
		min = val if val && val > 0 && val < min
		ans = true
	end
end
if ans
puts min.to_i
else
	puts "-1"
end

n = gets.to_i

# while l <= r
# 	x  = l + (r-l)/2
# 	s = x.to_s.split("").map(&:to_i).inject(0,:+)
# 	puts x
# 	if ((x * x) + (s * x) - n) == 0
# 		ans = x
# 		break
# 	else
# 		r = x - 1
# 	end
# end

# puts x
ans = false
min = n
(1..90).each do |s|
	a = 1
	b = s
	c = -n
	d = (b ** 2) - (4 * a * c)
	if d >=0
		r1 = (-b + Math.sqrt(d))/(2 * a)
		r2 = (-b - Math.sqrt(d))/(2 * a)
	elsif d == 0
		r1 = r2 = (-b)/(2 * a)
	end
	# puts "sum #{s} roots  #{r1} #{r2}"
	if r1 && r2
		arr = []
		sum = r1.to_i.to_s.split("").map(&:to_i).inject(0,:+)
		arr << r1 if r1 % 1 == 0 && r1 > 0 && s == sum
		sum = r2.to_i.to_s.split("").map(&:to_i).inject(0,:+)
		arr << r2 if  r2 % 1 == 0 && r2 > 0 && s == sum
		unless arr.empty?
		# puts "ar #{arr}"
			val = arr.min
			min = val if val && val < min
			# puts "#{val}"
			ans = true
		end
	end
end
if ans
	puts min.to_i
else
	puts "-1"
end

n = gets.to_i

# while l <= r
# 	x  = l + (r-l)/2
# 	s = x.to_s.split("").map(&:to_i).inject(0,:+)
# 	puts x
# 	if ((x * x) + (s * x) - n) == 0
# 		ans = x
# 		break
# 	else
# 		r = x - 1
# 	end
# end

# puts x
ans = false
min = n
(1..90).each do |s|
	a = 1
	b = s
	c = -n
	d = (b ** 2) - (4 * a * c)
	if d >=0
		r1 = (-b + Math.sqrt(d))/(2 * a)
		r2 = (-b - Math.sqrt(d))/(2 * a)
	elsif d == 0
		r1 = r2 = (-b)/(2 * a)
	end
	# puts "sum #{s} roots  #{r1} #{r2}"
	if r1 && r2
		arr = []
		sum = r1.to_i.to_s.split("").map(&:to_i).inject(0,:+)
		arr << r1 if r1 % 1 == 0 && s == sum
		sum = r2.to_i.to_s.split("").map(&:to_i).inject(0,:+)
		arr << r2 if  r2 % 1 == 0 && s == sum
		unless arr.empty?
			val = arr.min
			min = val if val && val > 0 && val < min
			ans = true
		end
	end
end
if ans
	puts min.to_i
else
	puts "-1"
end

n = gets.to_i

# while l <= r
# 	x  = l + (r-l)/2
# 	s = x.to_s.split("").map(&:to_i).inject(0,:+)
# 	puts x
# 	if ((x * x) + (s * x) - n) == 0
# 		ans = x
# 		break
# 	else
# 		r = x - 1
# 	end
# end

# puts x
ans = false
min = n
(1..90).each do |s|
	a = 1
	b = s
	c = -n
	d = (b ** 2) - (4 * a * c)
	if d >=0
		r1 = (-b + Math.sqrt(d))/(2 * a)
		r2 = (-b - Math.sqrt(d))/(2 * a)
	elsif d == 0
		r1 = r2 = (-b)/(2 * a)
	end
	# puts "sum #{s} roots  #{r1} #{r2}"
	if r1 && r2
		arr = []
		sum = r1.to_i.to_s.split("").map(&:to_i).inject(0,:+)
		arr << r1 if r1 % 1 == 0 && r1 > 0 && s == sum
		sum = r2.to_i.to_s.split("").map(&:to_i).inject(0,:+)
		arr << r2 if  r2 % 1 == 0 && r2 > 0 && s == sum
		unless arr.empty?
		puts "s #{s} ar #{r1} #{r2}"
			val = arr.min
			min = val if val && val < min
			# puts "#{val}"
			ans = true
		end
	end
end
if ans
	if min == 999959999
		puts -1
	else
	puts min.to_i
	end
else
	puts "-1"
end

def s(x)
  sum = 0
  while x>0
    sum+= x%10
    x /= 10
  end
  sum
end

n = gets.to_i
x = Math.sqrt(n).round-1
while x*x <= n
  if x*(x+s(x))==n
    puts x
    exit
  end
  x+=1
end
puts -1

def s(x)
  sum = 0
  while x>0
    sum+= x%10
    x /= 10
  end
  sum
end

n = gets.to_i
x = Math.sqrt(n).round - 160;

321.times do |i|
  if x*(x+s(x))==n
    puts x
    exit
  end
  x+=1
end
puts -1

def s(x)
  sum = 0
  while x>0
    sum+= x%10
    x /= 10
  end
  sum
end

n = gets.to_i
x = Math.sqrt(n).round-s(Math.sqrt(n).round)-1
while x*x <= n+s(x)
  if x*(x+s(x))==n
    puts x
    exit
  end
  x+=1
end
puts -1

def s(x)
  sum = 0
  while x>0
    sum+= x%10
    x /= 10
  end
  sum
end

n = gets.to_i
x =  Math.sqrt(n).round

while x*x <= n
   if x*(x+s(x))==n
     puts x
     exit
   end
  x+=1
end
puts -1

def s(x)
	ret=0
	while x>0
		ret+=x%10
		x/=10
	end
	ret
end

def is_int?(_x)
	x=_x.round
	(x-1e-9<_x)&&(_x<x+1e-9)
end

n=gets.to_i
ans=1.upto(162).map{|s|
	_x=(-s+(s*s+4*n)**0.5)/2.0
	if is_int?(_x)
		x=_x.round
		if x*x+s(x)*x-n==0
			x
		else
			nil
		end
	else
		nil
	end
}

p ans.any? ? ans.compact.min : -1

def s(x)
	ret=0
	while x>0
		ret+=x%10
		x/=10
	end
	ret
end

def is_int?(_x)
	x=_x.round
	(x-1e-9<_x)&&(_x<x+1e-9)
end

n=gets.to_i
ans=100.times.map{|s|
	_x=(-s+(s*s+4*n)**0.5)/2.0
	if is_int?(_x)
		x=_x.round
		if x*x+s(x)*x-n==0
			x
		else
			nil
		end
	else
		nil
	end
}

p ans.any? ? ans.compact.min : -1

def s(x)
	ret=0
	while x>0
		ret+=x%10
		x/=10
	end
	ret
end

def is_int?(_x)
	x=_x.round
	(x-1e-19<_x)&&(_x<x+1e-19)
end

n=gets.to_i
ans=1.upto(162).map{|s|
	_x=(-s+(s*s+4*n)**0.5)/2.0
	if is_int?(_x)
		x=_x.round
		if x*x+s(x)*x-n==0
			x
		else
			nil
		end
	else
		nil
	end
}

p ans.any? ? ans.compact.min : -1


Two little greedy bears have found two pieces of cheese in the forest of weight a and b grams, correspondingly. The bears are so greedy that they are ready to fight for the larger piece. That's where the fox comes in and starts the dialog: "Little bears, wait a little, I want to make your pieces equal" "Come off it fox, how are you going to do that?", the curious bears asked. "It's easy", said the fox. "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths. I'll eat a little here and there and make the pieces equal". The little bears realize that the fox's proposal contains a catch. But at the same time they realize that they can not make the two pieces equal themselves. So they agreed to her proposal, but on one condition: the fox should make the pieces equal as quickly as possible. Find the minimum number of operations the fox needs to make pieces equal.

The first line contains two space-separated integers a and b (1 ≤ a, b ≤ 109).

If the fox is lying to the little bears and it is impossible to make the pieces equal, print -1. Otherwise, print the required minimum number of operations. If the pieces of the cheese are initially equal, the required number is 0.

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>[\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 necklace can be described as a string of links ('-') and pearls ('o'), with the last link or pearl connected to the first one.  You can remove a link or a pearl and insert it between two other existing links or pearls (or between a link and a pearl) on the necklace. This process can be repeated as many times as you like, but you can't throw away any parts.Can you make the number of links between every two adjacent pearls equal? Two pearls are considered to be adjacent if there is no other pearl between them.Note that the final necklace should remain as one circular part of the same length as the initial necklace.

The only line of input contains a string $$$s$$$ ($$$3 \leq |s| \leq 100$$$), representing the necklace, where a dash '-' represents a link and the lowercase English letter 'o' represents a pearl.

Print "YES" if the links and pearls can be rejoined such that the number of links between adjacent pearls is equal. Otherwise print "NO". You can print each letter in any case (upper or lower).

s=gets.chomp
c=s.count(?o)
puts c>0&&s.size%c>0?:No: :Yes

s=gets.chomp

li=s.count('-')
pe=s.count('o')

if pe==0
  puts 'YES'
else
  if li%pe==0
    puts 'YES'
  else
    puts 'NO'
  end
end

PEARL = 'o'
LINK = '-'

necklace = STDIN.gets.chomp

def adjustable?(necklace)
  pearl_count = necklace.count(PEARL)
  return true if pearl_count == 0
  link_count = necklace.count(LINK)

  link_count % pearl_count == 0
end

if adjustable?(necklace)
  puts 'YES'
else
  puts 'NO'
end

s=gets.chomp
c=s.count(?o)
puts c==0||s.size%c>0?:No: :Yes

s=gets.chomp
c=s.count(?o)
puts c>0||s.size%c>0?:No: :Yes

PEARL = 'o'
LINK = '-'

necklace = STDIN.gets.chomp

def adjustable?(necklace)
  pearl_count = necklace.count(PEARL)
  if pearl_count == 1
    true
  else
    link_count = necklace.count(LINK)
    link_count % (pearl_count - 1) == 0
  end
end

if adjustable?(necklace)
  puts 'YES'
else
  puts 'NO'
end


Nicholas has an array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of size n.Nicholas want the minimum element (integer 1) and the maximum element (integer n) to be as far as possible from each other. He wants to perform exactly one swap in order to maximize the distance between the minimum and the maximum elements. The distance between two elements is considered to be equal to the absolute difference between their positions.

The first line of the input contains a single integer n (2 ≤ n ≤ 100) — the size of the permutation. The second line of the input contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n), where ai is equal to the element at the i-th position.

Print a single integer — the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap.

NoteIn the first sample, one may obtain the optimal answer by swapping elements 1 and 2.In the second sample, the minimum and the maximum elements will be located in the opposite ends of the array if we swap 7 and 2.In the third sample, the distance between the minimum and the maximum elements is already maximum possible, so we just perform some unnecessary swap, for example, one can swap 5 and 2.

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

first = arr.index(1)
last = arr.index(n)
if first == 0 and last == n - 1
    p (n - 1)
else
    p [first, last, n - 1 - first, n - 1 - last].max
end

n=gets.to_i
a,b=gets.split.map(&:to_i).each_with_index.select{|e,i|e==1||e==n}.map(&:last)
p [n-1-a,n-1-b,a,b].max

def checks(n, arrays)
  arrays = arrays.split.map(&:to_i)
  n = n.to_i

  for i in (0..n - 1)
    if arrays[i] == 1
      pos1 = i
    end

    if arrays[i] == n
      posn = i
    end
  end

  puts [posn, n - 1 - posn, pos1, n - 1 - pos1].max
end

checks(gets, gets)

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

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

pos = []
arr.each_with_index do |value, index|
    if 1 == value
        pos.push index
    elsif n == value
        pos.push index
    end
end

puts max(pos[1], n - pos[0] - 1)

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

pos = []
arr.each_with_index do |value, index|
    if 1 == value
        pos.push index
    elsif n == value
        pos.push index
    end
end

p [pos[1], n - pos[0] - 1].max

class Array
  def swap(a, b)
    clone = self.dup
    clone[a], clone[b] = clone[b], clone[a]

    clone
  end
  def max_distance
    (self.index(self.min) - self.index(self.max)).abs
  end
end

n = gets.chomp.to_i
arr = gets.chomp.split.map(&:to_i)
results = []

(0...n).each do |i|
  (0...n).each do |j|
    next if i == j
    results << arr.swap(i, j).max_distance
  end
end
puts results.max

n = readline.to_i
arr = readline.split(' ').collect(&:to_i)
i, j = arr.index(1), arr.index(n)
puts [[i, n - i - 1].max, [j, n - j - 1].max].max

n = gets.chomp.to_i
a = gets.chomp.split(/ /).map(&:to_i)
min_element = a.min
max_element = a.max
pos_min = a.index min_element
pos_max = a.index max_element
start_min_dist = pos_min
start_max_dist = pos_max
end_min_dist = n - pos_min - 1
end_max_dist = n - pos_max - 1
puts [start_min_dist, start_max_dist, end_min_dist, end_max_dist].max

n=gets.to_i
a=gets.split.map{|e| e.to_i}
b=a.index(1)
c=a.index(n)
b1=[b,c].min
c1=[b,c].max
n-=1
if b1==0 || c1==n then
	puts n
else
	puts [c1,n-b1].max
end

n = gets.to_i

numbers = gets

res = numbers.split(' ')

p_max = -1
p_min = -1

res.each_with_index do |r,i|
    if (r.to_i == 1)
        p_min = i + 1
    end
    if (r.to_i == n)
        p_max = i + 1
    end
end

if (p_max < p_min)
    t = p_max
    p_max = p_min
    p_min = t
end
puts (n-p_min>p_max-1)? n-p_min : p_max - 1

n=gets.to_i;a=gets.split.map(&:to_i);(amax,amin)=[a.find_index(a.max),a.find_index(a.min)];puts [amax, n-amax-1, amin, n-amin-1].max

n=gets.to_i;a=gets.split.map(&:to_i);puts [a.index(n), n-a.index(n)-1, a.index(1), n-a.index(1)-1].max

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

if n <= 3
  puts n - 1
else
  i = as.index(1)
  j = as.index(n)
  ans = (i - j).abs
  ans = [ans, n - 1 - [i, j].min].max
  ans = [ans, [i, j].max].max
  puts ans
end

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

for i in 0...n
	biggest = i + 1 if n == nums[i]
	smallest = i + 1 if 1 == nums[i]
end

puts [n - [smallest, biggest].min, [smallest, biggest].max - 1].max

=begin
7
1
6
5
3
4
7
2

6
6
5
4
3
2
1

3
2 3 1

=end

n = 0
nums = Array.new

biggest = 0
smallest = 100

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

#p nums
#puts n
#puts nums.last

for i in 0..n
	#nums.push($stdin.read.chomp.to_i)
	#thing = gets.chomp.split('|')[1].scan(/\d+/).map{|n| n.to_i}
	if n == nums[i]
		biggest = i + 1
	end
	if 1 == nums[i]
		smallest = i + 1
	end
end

#puts "Biggest: #{biggest}, smallest: #{smallest}\n\n"

puts [n - [smallest, biggest].min, [smallest, biggest].max - 1].max

=begin
7
1
6
5
3
4
7
2

6
6
5
4
3
2
1

3
2 3 1

=end

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

p (d.index(1)-d.index(n)).abs + [[d.index(1),d.index(n)].min,n-1-[d.index(1),d.index(n)].max].max

n = gets.to_i
a = gets.split.map(&:to_i)
b = [a.index(1), a.index(n)].sort
puts [n - 1 - b[0], b[1]].max

size_of_permutation = gets.chomp.to_i
integers_array = gets.chomp.split.map(&:to_i)

def max_distance(size, list)
  min_index = nil
  max_index = nil

  for i in 0..(list.length - 1)
    min_index = i if list[i] == 1
    max_index = i if list[i] == size

    if min_index && max_index
      break
    end
  end

  potential_distance = [min_index, max_index, size - 1 - min_index, size - 1 - max_index]
  max_distance = potential_distance[0]
  for i in potential_distance
    if i > max_distance
      max_distance = i
    end
  end
  max_distance
end

puts max_distance(size_of_permutation, integers_array)

size_of_permutation = gets.chomp.to_i
integers_array = gets.chomp.split.map(&:to_i)

def max_distance(size, list)
  min, max = list[0], list[0]
  min_index = 0
  max_index = 0

  for i in 0..(list.length - 1)
    if list[i] < min
      min = list[i]
      min_index = i
    end

    if list[i] > max
      max = list[i]
      max_index = i
    end
  end

  potential_distance = [min_index, max_index, size - 1 - min_index, size - 1 - max_index]
  max_distance = potential_distance[0]
  for i in potential_distance
    if i > max_distance
      max_distance = i
    end
  end
  max_distance
end

puts max_distance(size_of_permutation, integers_array)

arrLen = gets.chomp.to_i
arr = gets.chomp.split(' ').map { |i| i.to_i }

minIdx = arr.index { |i| i == arr.min}
maxIdx = arr.index { |i| i == arr.max}

biggerIdx = minIdx > maxIdx ? minIdx : maxIdx
nonBiggerIdx = minIdx < maxIdx ? minIdx : maxIdx

candidate_1 = (arrLen - 1) - nonBiggerIdx
candidate_2 = (arrLen - 1) - biggerIdx
candidate_3 = nonBiggerIdx - 0
candidate_4 = biggerIdx - 0

ans = Array.new
ans << candidate_1
ans << candidate_2
ans << candidate_3
ans << candidate_4

puts ans.max

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

first = arr.index(1)
p first
last = arr.index(n)
if first == 0 and last == n - 1
    p (n - 1)
else
    p [first, last, n - 1 - first, n - 1 - last].max
end

n = gets.chomp.to_i
a = gets.chomp.split(/ /).map(&:to_i)
min_element = a.min
max_element = a.max
pos_min = a.index min_element
pos_max = a.index max_element
if pos_min > pos_max
    pos_min = n - 1
elsif pos_max > pos_min
    pos_max = n - 1
end
dist = (pos_min - pos_max).abs
puts dist

n = gets.to_i

numbers = gets

res = numbers.split(' ')

p_max = -1
p_min = -1

res.each_with_index do |r,i|
    if (r.to_i == 1)
        p_min = i + 1
    end
    if (r.to_i == n)
        p_max = i + 1
    end
end

if (p_max < p_min)
    t = p_max
    p_max = p_min
    p_min = t
end
puts (n-p_min>p_max)? n-p_min : p_max

n = 0
nums = Array.new

biggest = 0
smallest = 100

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

#p nums
#puts n
#puts nums.last

for i in 0..n
	#nums.push($stdin.read.chomp.to_i)
	#thing = gets.chomp.split('|')[1].scan(/\d+/).map{|n| n.to_i}
	if n == nums[i]
		biggest = i + 1
	end
	if 1 == nums[i]
		smallest = i + 1
	end
end

#puts "Biggest: #{biggest}, smallest: #{smallest}\n\n"

puts [n - [smallest, biggest].min, [smallest, biggest].min - 1].max

=begin
7
1
6
5
3
4
7
2

6
6
5
4
3
2
1

=end

n = gets.to_i
a = gets.split.map(&:to_i)
b = [a.index(1), a.index(n)].sort
puts [n - b[0], b[1]].max - 1

size_of_permutation = gets.chomp.to_i
integers_array = gets.chomp.split.map(&:to_i)

def max_distance(size, list)
  min, max = list[0], list[0]
  min_index = 0
  max_index = 0

  for i in 0..(list.length - 1)
    if list[i] < min
      min = list[i]
      min_index = i
    end

    if list[i] > max
      max = list[i]
      max_index = i
    end
  end

  if min_index == 0 || max_index == 0 || min_index == size - 1 || max_index == size - 1
    return size - 1
  end

  min_index < max_index ? size - 1 - min_index : size - 1 - max_index
end

puts max_distance(size_of_permutation, integers_array)

size_of_permutation = gets.chomp.to_i
integers_array = gets.chomp.split.map(&:to_i)

def max_distance(size, list)
  min, max = list[0], list[0]
  min_index = 0
  max_index = 0

  for i in 0..(list.length - 1)
    if list[i] < min
      min = list[i]
      min_index = i
    end

    if list[i] > max
      max = list[i]
      max_index = i
    end
  end

  max_distance = min_index < max_index ? size - 1 - min_index : size - 1 - max_index
end

puts max_distance(size_of_permutation, integers_array)

arrLen = gets.chomp.to_i
arr = gets.chomp.split(' ').map { |i| i.to_i }

minIdx = arr.index { |i| i == arr.min}
maxIdx = arr.index { |i| i == arr.max}

biggerIdx = minIdx > maxIdx ? minIdx : maxIdx
nonBiggerIdx = minIdx < maxIdx ? minIdx : maxIdx

ans = if (arrLen - biggerIdx) > (nonBiggerIdx - 0)
	arrLen - nonBiggerIdx
else
	biggerIdx - 0
end

puts ans - 1


Jabber ID on the national Berland service «Babber» has a form <username>@<hostname>[/resource], where   <username> — is a sequence of Latin letters (lowercase or uppercase), digits or underscores characters «_», the length of <username> is between 1 and 16, inclusive.  <hostname> — is a sequence of word separated by periods (characters «.»), where each word should contain only characters allowed for <username>, the length of each word is between 1 and 16, inclusive. The length of <hostname> is between 1 and 32, inclusive.  <resource> — is a sequence of Latin letters (lowercase or uppercase), digits or underscores characters «_», the length of <resource> is between 1 and 16, inclusive. The content of square brackets is optional — it can be present or can be absent.There are the samples of correct Jabber IDs: mike@codeforces.com, 007@en.codeforces.com/contest.Your task is to write program which checks if given string is a correct Jabber ID.

The input contains of a single line. The line has the length between 1 and 100 characters, inclusive. Each characters has ASCII-code between 33 and 127, inclusive.

str = gets.strip!
re_pattern = /^\w{1,16}@(\w{1,16}(\.\w{1,16})*)(\/\w{1,16})?$/
if str.match(re_pattern) && $1.size <= 32
	puts 'YES'
else
	puts 'NO'
end

str = gets.strip!
re_pattern = /^\w{1,16}@\w{1,16}(\.\w{1,16})*(\/\w{1,16})?$/
if str.match(re_pattern)
	puts 'YES'
else
	puts 'NO'
end

def valid_word?(s)
    s =~ /^[_a-zA-Z0-9_]{1,16}$/
end

def solve(s)

    words = (s + '.a').split(/[.@\/]/)
    words.pop
    return false unless words.all? { |w| valid_word? w }

    wr = '[_a-zA-Z0-9_]{1,16}' # word regex
    rr = '[_a-zA-Z0-9_.]{1,32}' # resource regex

    return s =~ Regexp.new("^#{wr}@#{wr}(\\.#{wr})*(\\/#{wr})?") &&
           s =~ Regexp.new("^#{wr}@#{rr}(\\/#{wr})?")

end

s = gets.chomp
#s = 'mike@codeforces.com'
#s = 'john.smith@codeforces.ru/contest.icpc/12'
#s = '007@en.codeforces.com/contest'
puts solve(s) ? 'YES' : 'NO'

s = gets.strip
unless s =~ %r{^([0-9a-zA-Z_]{1,16})@([0-9a-zA-Z_]{1,16}(\.[0-9a-zA-Z_]{1,16})*)((/[0-9a-zA-Z_]{1,16})?)$}
  puts "NO"
  exit 0
end
#puts $1, $2, $3, $4
username, hostname, resource = $1, $2, $4

if hostname.length >= 1 && hostname.length <= 32
  puts "YES"
else
  puts "NO"
end

def user_ok(s)
  s.length >= 1 && s.length <= 16 && s =~ /^[0-9a-zA-Z_]*$/
end

def host_ok(s)
#  puts s
  if s.length < 1 || s.length > 32
    return false
  end
  if s[0] == "." || s[-1] == "."
    return false
  end
  for st in s.split(".") do
#    puts st
    unless user_ok(st)
      return false
    end
  end
  true
end

s = gets.strip
ok = true
unless s =~ %r{^([^@]+)@([^/]+)((/[0-9a-zA-Z_]*)?)$}
  puts "NO"
  exit 0
end
username, hostname, resource = $1, $2, $3
unless user_ok(username) && host_ok(hostname)
  ok = false
end

if resource.length == 1 || resource.length > 17
  ok = false
end

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

def del(s)
    return s.delete("abcdefghijklmnopqrstuvwxyz_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
end

s = gets.chomp
result = "YES"
if (k = s.index("@")) then
    if s[0...k].size < 1 then
        result = "NO"
    end
    user = del(s[0...k])
    if user.size > 0 then
        result = "NO"
    end
    if (n = s.index("/")) then
        host = s[k+1...n]
        resource = s[n+1..-1]
    else
        host = s[k+1..-1]
        resource = nil
    end
    if host.size < 1 then
        result = "NO"
    end
    k = -1
    while (m = host.index(".", k+1)) do
        if m-k > 17 || m-k < 2 then
            result = "NO"
        end
        k = m
    end
    host = del(host).delete(".")
    if host.size > 0 then
        result = "NO"
    end

    if resource then
        if s[n-1] == "." then
            result = "NO"
        end
        if resource.size > 16 || resource.size < 1 then
            result = "NO"
        else
            resource = del(resource)
            if resource.size > 0 then
                result = "NO"
            end
        end
    else
        if s[-1] == "." then
            result = "NO"
        end
    end
else
    result = "NO"
end

puts result

def del(s)
    return s.delete("abcdefghijklmnopqrstuvwxyz_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
end

s = gets.chomp
result = "YES"
if (k = s.index("@")) then
    if s[0...k].size < 1 then
        result = "NO"
    end
    user = del(s[0...k])
    if user.size > 0 then
        result = "NO"
    end
    if (n = s.index("/")) then
        host = s[k+1...n]
        resource = s[n+1..-1]
    else
        host = s[k+1..-1]
        resource = nil
    end
    if host.size < 1 then
        result = "NO"
    end
    k = -1
    while (m = host.index(".", k+1)) do
        if m-k > 16 then
            result = "NO"
        end
        k = m
    end
    host = del(host).delete(".")
    if host.size > 0 then
        result = "NO"
    end

    if resource then
        if s[n] == "." then
            result = "NO"
        end
        if resource.size > 16 then
            result = "NO"
        else
            resource = del(resource)
            if resource.size > 0 then
                result = "NO"
            end
        end
    else
        if s[-1] == "." then
            result = "NO"
        end
    end
else
    result = "NO"
end

puts result

def del(s)
    return s.delete("abcdefghijklmnopqrstuvwxyz_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
end

s = gets.chomp
result = "YES"
if (k = s.index("@")) then
    if s[0...k].size < 1 then
        result = "NO"
    end
    user = del(s[0...k])
    if user.size > 0 then
        result = "NO"
    end
    if (n = s.index("/")) then
        host = s[k+1...n]
        resource = s[n+1..-1]
    else
        host = s[k+1..-1]
        resource = nil
    end
    if host.size < 1 then
        result = "NO"
    end
    k = -1
    while (m = host.index(".", k+1)) do
        if m-k > 16 then
            result = "NO"
        end
        k = m
    end
    host = del(host).delete(".")
    if host.size > 0 then
        result = "NO"
    end

    if resource then
        if resource.size > 16 then
            result = "NO"
        else
            resource = del(resource)
            if resource.size > 0 then
                result = "NO"
            end
        end
    end
else
    result = "NO"
end

puts result

def del(s)
    return s.delete("abcdefghijklmnopqrstuvwxyz_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
end

s = gets.chomp
result = "YES"
if (k = s.index("@")) then
    if s[0...k].size < 1 then
        result = "NO"
    end
    user = del(s[0...k])
    if user.size > 0 then
        result = "NO"
    end
    if (n = s.index("/")) then
        host = s[k+1...n]
        resource = s[n+1..-1]
    else
        host = s[k+1..-1]
        resource = nil
    end
    if host.size < 1 then
        result = "NO"
    end
    k = -1
    while (m = host.index(".", k+1)) do
        if m-k > 17 || m-k < 2 then
            result = "NO"
        end
        k = m
    end
    host = del(host).delete(".")
    if host.size > 0 then
        result = "NO"
    end

    if resource then
        if s[n] == "." then
            result = "NO"
        end
        if resource.size > 16 then
            result = "NO"
        else
            resource = del(resource)
            if resource.size > 0 then
                result = "NO"
            end
        end
    else
        if s[-1] == "." then
            result = "NO"
        end
    end
else
    result = "NO"
end

puts result

def valid_word?(s)
    s =~ /^[_a-zA-Z0-9_]{1,16}$/
end

def solve(s)
    a = s.split('@')
    return false unless a.size == 2
    return false unless valid_word? a.shift
    a = a.first
    return false if a[-1..-1] == '/'
    a = a.split('/')
    return false if a.size > 2
    return false if a.size == 2 && !valid_word?(a[1])
    a = a[0]
    return false unless a.size <= 32
    return false if a[-1..-1] == '.'
    a = a.split('.')
    return a.all? { |w| valid_word? w }
end

s = gets.chomp
#s = 'mike@codeforces.com'
#s = 'john.smith@codeforces.ru/contest.icpc/12'
#s = '007@en.codeforces.com/contest'
puts solve(s) ? 'YES' : 'NO'

s = gets.strip
unless s =~ %r{([0-9a-zA-Z_]{1,16})@([0-9a-zA-Z_]{1,16}(\.[0-9a-zA-Z_]{1,16})*)((/[0-9a-zA-Z_]{1,16})?)$}
  puts "NO"
  exit 0
end
#puts $1, $2, $3, $4
username, hostname, resource = $1, $2, $4

if hostname.length >= 1 && hostname.length <= 32
  puts "YES"
else
  puts "NO"
end

def del(s)
    return s.delete("abcdefghijklmnopqrstuvwxyz_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
end

s = gets.chomp
result = "YES"
k = s.index("@")
user = del(s[0...k])
if user.size > 0 then
    result = "NO"
end
if (n = s.index("/")) then
    host = s[k+1...n]
    resource = s[n+1..-1]
else
    host = s[k+1..-1]
    resource = nil
end
k = -1
while (m = host.index(".", k+1)) do
    if m-k > 16 then
        result = "NO"
    end
    k = m
end
host = del(host).delete(".")
if host.size > 0 then
    result = "NO"
end

if resource then
    if resource.size > 16 then
        result = "NO"
    else
        resource = del(resource)
        if resource.size > 0 then
            result = "NO"
        end
    end
end

puts result

def del(s)
    return s.delete("abcdefghijklmnopqrstuvwxyz_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
end

s = gets.chomp
result = "YES"
if (k = s.index("@")) then
    if s[0...k].size < 1 then
        result = "NO"
    end
    user = del(s[0...k])
    if user.size > 0 then
        result = "NO"
    end
    if (n = s.index("/")) then
        host = s[k+1...n]
        resource = s[n+1..-1]
    else
        host = s[k+1..-1]
        resource = nil
    end
    if host.size < 1 then
        result = "NO"
    end
    k = -1
    while (m = host.index(".", k+1)) do
        if m-k > 17 || m-k < 2 then
            result = "NO"
        end
        k = m
    end
    host = del(host).delete(".")
    if host.size > 0 then
        result = "NO"
    end

    if resource then
        if s[n-1] == "." then
            result = "NO"
        end
        if resource.size > 16 then
            result = "NO"
        else
            resource = del(resource)
            if resource.size > 0 then
                result = "NO"
            end
        end
    else
        if s[-1] == "." then
            result = "NO"
        end
    end
else
    result = "NO"
end

puts result


You are given a regular polygon with $$$n$$$ vertices labeled from $$$1$$$ to $$$n$$$ in counter-clockwise order. The triangulation of a given polygon is a set of triangles such that each vertex of each triangle is a vertex of the initial polygon, there is no pair of triangles such that their intersection has non-zero area, and the total area of all triangles is equal to the area of the given polygon. The weight of a triangulation is the sum of weigths of triangles it consists of, where the weight of a triagle is denoted as the product of labels of its vertices.Calculate the minimum weight among all triangulations of the polygon.

The first line contains single integer $$$n$$$ ($$$3 \le n \le 500$$$) — the number of vertices in the regular polygon.

Print one integer — the minimum weight among all triangulations of the given polygon.

NoteAccording to Wiki: polygon triangulation is the decomposition of a polygonal area (simple polygon) $$$P$$$ into a set of triangles, i. e., finding a set of triangles with pairwise non-intersecting interiors whose union is $$$P$$$.In the first example the polygon is a triangle, so we don't need to cut it further, so the answer is $$$1 \cdot 2 \cdot 3 = 6$$$.In the second example the polygon is a rectangle, so it should be divided into two triangles. It's optimal to cut it using diagonal $$$1-3$$$ so answer is $$$1 \cdot 2 \cdot 3 + 1 \cdot 3 \cdot 4 = 6 + 12 = 18$$$.


Luke Skywalker got locked up in a rubbish shredder between two presses. R2D2 is already working on his rescue, but Luke needs to stay alive as long as possible. For simplicity we will assume that everything happens on a straight line, the presses are initially at coordinates 0 and L, and they move towards each other with speed v1 and v2, respectively. Luke has width d and is able to choose any position between the presses. Luke dies as soon as the distance between the presses is less than his width. Your task is to determine for how long Luke can stay alive.

The first line of the input contains four integers d, L, v1, v2 (1 ≤ d, L, v1, v2 ≤ 10 000, d < L) — Luke's width, the initial position of the second press and the speed of the first and second presses, respectively.

Print a single real value — the maximum period of time Luke can stay alive for. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.  Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

NoteIn the first sample Luke should stay exactly in the middle of the segment, that is at coordinates [2;4], as the presses move with the same speed.In the second sample he needs to occupy the position . In this case both presses move to his edges at the same time.

d,l,v1,v2=gets.split.map{|e| e.to_f}
puts (l-d)/(v1+v2)

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

puts (l - d) / (v1 + v2).to_f

d,l,v,w=gets.split.map &:to_f
puts "%.20f" % ((l - d) / (v + w))

d,l,v1,v2 = gets.chomp.split(' ').map(&:to_i)
puts((l-d).to_f/(v1+v2))

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

puts (l - d).to_f/(v1 + v2)

d, L, v1, v2 = gets.chomp.split(' ').map(&:to_f)

res = (L - d) / (v1 + v2)
p res

class Solver
	def main
		d, l, v1, v2 = gets.split.map { |x| x.to_i }
		puts format("%.8f", (l - d).to_f / (v1 + v2).to_f)
	end
end
Solver.new.main


Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky.Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≤ x, y ≤ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y.Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number.

The first line contains a single integer n (1 ≤ n ≤ 109) — Polycarpus's number.

Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky.

NoteIn the first test sample all numbers that do not exceed 10 are undoubtedly lucky.In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky.

# # n = gets.to_i
# # l = n.to_s.length

# # if l > 2
# # 	list = [9,90]
# # 	x = 81
# # 	(l - 2).times do |i|
# # 		x *= 2
# # 	end
# # 	x +=  9
# # else
# # 	puts n
# # end

# # def dfs(num) #// run it as dfs(0)
# #   @ans += 1 if (num > 0 && num <= @n)
# #   return if (num >= 10**8)
# #   (0..9).each do |a|
# #   	number = num*10+a
# #     if number >0
# #       if number.to_s.split("").uniq.length <= 2
# #         dfs(number)
# #       end
# #     end
# #   end
# # end
# # require 'Set'
# def dfs(num) #// run it as dfs(0)
#   (1..@n).each do |a|
# 	  return if (a >= 10**8)
# 	  if @n > 100000
# 	  	@ans += 1
# 	  else
# 	      if a.to_s.chars.to_a.uniq.length <= 2
# 	      	@ans += 1
# 	      end
#   	  end
#   end
# end

# @n = gets.to_i
# @ans = 0
# dfs(0)
# puts @ans

require 'Set'

def dfs(x,y,num,cnt=false)
	return if num > @n  || (num == 0 && !cnt)
	@s << num if num > 0
	dfs(x,y,num * 10 + x)
	dfs(x,y,num * 10 + y)
end

@n = gets.to_i
@s = Set.new
(0..9).each do |i|
	((i+1)..9).each do |j|
		dfs(i,j,0,true)
	end
end
puts @s.length

require 'set'

$set=Set.new
$n=gets.to_i

def dfs num,x,y
  if (1..$n)===num
    $set<<num
    dfs(num*10+x,x,y)
    dfs(num*10+y,x,y)
  end
end

count=0

(0..9).each {|x|
  (0..9).each {|y|
    dfs(x,x,y)
    dfs(y,x,y)
  }
}

puts $set.size

require 'set'

$set=Set.new
$n=gets.to_i

def dfs num,x,y
  if (1..$n)===num
    $set<<num
    dfs(num*10+x,x,y)
    dfs(num*10+y,x,y)
  end
end

count=0

(0..9).each {|x|
  (0..9).each {|y|
    dfs(x,x,y)
  }
}

puts $set.size


Vasya started working in a machine vision company of IT City. Vasya's team creates software and hardware for identification of people by their face.One of the project's know-how is a camera rotating around its optical axis on shooting. People see an eye-catching gadget — a rotating camera — come up to it to see it better, look into it. And the camera takes their photo at that time. What could be better for high quality identification?But not everything is so simple. The pictures from camera appear rotated too (on clockwise camera rotation frame the content becomes rotated counter-clockwise). But the identification algorithm can work only with faces that are just slightly deviated from vertical.Vasya was entrusted to correct the situation — to rotate a captured image so that image would be minimally deviated from vertical. Requirements were severe. Firstly, the picture should be rotated only on angle divisible by 90 degrees to not lose a bit of information about the image. Secondly, the frames from the camera are so huge and FPS is so big that adequate rotation speed is provided by hardware FPGA solution only. And this solution can rotate only by 90 degrees clockwise. Of course, one can apply 90 degrees turn several times but for the sake of performance the number of turns should be minimized.Help Vasya implement the program that by the given rotation angle of the camera can determine the minimum number of 90 degrees clockwise turns necessary to get a picture in which up direction deviation from vertical is minimum.The next figure contains frames taken from an unrotated camera, then from rotated 90 degrees clockwise, then from rotated 90 degrees counter-clockwise. Arrows show direction to "true up".  The next figure shows 90 degrees clockwise turn by FPGA hardware.

The only line of the input contains one integer x ( - 1018 ≤ x ≤ 1018) — camera angle in degrees. Positive value denotes clockwise camera rotation, negative — counter-clockwise.

Output one integer — the minimum required number of 90 degrees clockwise turns.

NoteWhen the camera is rotated 60 degrees counter-clockwise (the second example), an image from it is rotated 60 degrees clockwise. One 90 degrees clockwise turn of the image result in 150 degrees clockwise total rotation and deviation from "true up" for one turn is 150 degrees. Two 90 degrees clockwise turns of the image result in 240 degrees clockwise total rotation and deviation from "true up" for two turns is 120 degrees because 240 degrees clockwise equal to 120 degrees counter-clockwise. Three 90 degrees clockwise turns of the image result in 330 degrees clockwise total rotation and deviation from "true up" for three turns is 30 degrees because 330 degrees clockwise equal to 30 degrees counter-clockwise.From 60, 150, 120 and 30 degrees deviations the smallest is 30, and it it achieved in three 90 degrees clockwise turns.


After finishing eating her bun, Alyona came up with two integers n and m. She decided to write down two columns of integers — the first column containing integers from 1 to n and the second containing integers from 1 to m. Now the girl wants to count how many pairs of integers she can choose, one from the first column and the other from the second column, such that their sum is divisible by 5.Formally, Alyona wants to count the number of pairs of integers (x, y) such that 1 ≤ x ≤ n, 1 ≤ y ≤ m and  equals 0.As usual, Alyona has some troubles and asks you to help.

The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1 000 000).

Print the only integer — the number of pairs of integers (x, y) such that 1 ≤ x ≤ n, 1 ≤ y ≤ m and (x + y) is divisible by 5.

NoteFollowing pairs are suitable in the first sample case:   for x = 1 fits y equal to 4 or 9;  for x = 2 fits y equal to 3 or 8;  for x = 3 fits y equal to 2, 7 or 12;  for x = 4 fits y equal to 1, 6 or 11;  for x = 5 fits y equal to 5 or 10;  for x = 6 fits y equal to 4 or 9. Only the pair (1, 4) is suitable in the third sample case.

n, m = gets.strip.split.map(&:to_i)
count = Array.new(1e6 + 1, 0)
ans = 0
(1..n).each do |i|
    count[i % 5] += 1
end
(1..m).each do |i|
    ans += count[(5 - i % 5) % 5]
end
puts ans

x,y=gets.split.map{|e| e.to_i}
x,y=[x,y].min,[x,y].max
z=x+y
ans=0
4.step(z,5){|i|
	if i>y then
		ans+=(x-(i-y))
	else
		ans+=[x,i].min
	end
}
puts ans

# t = gets.to_i
# t.times do
n, m = gets.split.map(&:to_i)
count = 0
(1..n).each do |x|
	y1 = 5 - x % 5
	k = ((m - y1)/5).to_i
	# y2 = y1 + 5* k
	count += k+1
end
puts count

n, m = gets.split(' ').map(&:to_i)
n_full, n_rest = n.divmod 5
m_full, m_rest = m.divmod 5

puts (1..4).map { |i| (n_full + (n_rest >= i ? 1 : 0)) * (m_full + (m_rest >= (5-i) ? 1 : 0)) }.reduce(:+) + n_full * m_full

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

ans = 0
for i in 0..4
  x = n / 5
  x += 1 if 0 < i && i <= n % 5
  y = m / 5
  y += 1 if i < 5 && 5 - i <= m % 5
  ans += x * y
end

puts ans

n=gets.chomp.to_s.split(" ")
n,m=n[0].to_s.to_i,n[1].to_s.to_i
res=0
1.upto(n) do |x|   # 5-0
                   # 4-1 3-2 2-3 1-4
  res+=(m+x%5)/5

end
print res

n, m = gets.chomp.split(' ').map(&:to_i)
x = 1
ans = 0
until x > n
	ans += ((m + x) / 5) - (x / 5)
	x += 1
end

puts ans


The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, because he has business of national importance. For example, he has to pay an official visit to square t. As the king is not in habit of wasting his time, he wants to get from his current position s to square t in the least number of moves. Help him to do this.  In one move the king can get to the square that has a common side or a common vertex with the square the king is currently in (generally there are 8 different squares he can move to).

The first line contains the chessboard coordinates of square s, the second line — of square t. Chessboard coordinates consist of two characters, the first one is a lowercase Latin letter (from a to h), the second one is a digit from 1 to 8.

In the first line print n — minimum number of the king's moves. Then in n lines print the moves themselves. Each move is described with one of the 8: L, R, U, D, LU, LD, RU or RD.  L, R, U, D stand respectively for moves left, right, up and down (according to the picture), and 2-letter combinations stand for diagonal moves. If the answer is not unique, print any of them.

def num(x)
    if x == "a"
        return 1
    elsif x == "b"
        return 2
    elsif x == "c"
        return 3
    elsif x == "d"
        return 4
    elsif x == "e"
        return 5
    elsif x == "f"
        return 6
    elsif x == "g"
        return 7
    else
        return 8
    end
end

def simple(h,v)
    step = []
    if h > 0 then
        step += ["R"]*h
    elsif h < 0 then
        step += ["L"]*h.abs
    elsif v > 0
        step += ["U"]*v
    elsif v < 0
        step += ["D"]*v.abs
    end
    return step
end

k = gets.chomp
t = gets.chomp

step = []

h = num(t[0]) - num(k[0])
v = t[1].to_i - k[1].to_i

d = (0-v).abs < (0-h).abs ? v : h

if d != 0 then
    if h == d then
        if h > 0 && v > 0 then
            step += ["RU"]*h
        elsif h > 0 && v < 0 then
            step += ["RD"]*h
        elsif h < 0 && v > 0 then
            step += ["LU"]*h.abs
        else
            step += ["LD"]*h.abs
        end
        if v > 0 then
            v -= h.abs
        else
            v += h.abs
        end
        h = 0
    else
        if h > 0 && v > 0 then
            step += ["RU"]*v
        elsif h > 0 && v < 0 then
            step += ["RD"]*v.abs
        elsif h < 0 && v > 0 then
            step += ["LU"]*v
        else
            step += ["LD"]*v.abs
        end
        if h > 0 then
            h -= v.abs
        else
            h += v.abs
        end
        v = 0
    end
end

step += simple(h,v)

puts step.size
step.size.times do |i|
    puts step[i]
end

# require 'pry-byebug'
file = File.exist?('in.in') ? File.new('in.in','r') : STDIN

DX = [0, 1, 0, -1, 1, -1, 1, -1]
DY = [1, 0, -1, 0, 1, -1, -1, 1]
MOV = ["R", "D", "L", "U", "RD", "LU", "LD", "RU"]

def inside?(i, j, grid)
  i>=0 && i<= grid.size-1 && j>=0 && j <=grid[0].size-1
end

map = {"a"=> 0, "b"=> 1, "c"=> 2, "d"=> 3, "e"=> 4,"f"=> 5, "g"=> 6, "h" => 7}

s2,s1 = file.gets.chomp.split('')
t2,t1 = file.gets.chomp.split('')

s1 = (s1.to_i-1-7).abs
s2 = map[s2]
t1 = (t1.to_i-1-7).abs
t2 = map[t2]

# puts "#{s1}#{s2}"
# puts "#{t1}#{t2}"

grid = Array.new(8) { Array.new(8) }
dist = Array.new(8) { Array.new(8, 100) }
queue = [[s1, s2]]
dist[s1][s2]=0
grid[s1][s2]=[]

while(queue.size != 0)
  x, y  = queue.shift
  8.times do |i|
    new_x = x+DX[i]
    new_y = y+DY[i]
    if inside?(new_x, new_y, grid) && dist[new_x][new_y]==100

      if dist[x][y]+1 < dist[new_x][new_y]
        dist[new_x][new_y]  = dist[x][y]+1
        grid[new_x][new_y] = grid[x][y] + [MOV[i]]
      end

      # dist[new_x][new_y] = [dist[new_x][new_y],dist[x][y]+1].min
      break if new_x == t1 && new_y == t2
      # binding.pry if dist[new_x][new_y] == 7
      queue << [new_x, new_y]
    end
  end
end
puts dist[t1][t2]
grid[t1][t2].each do |e|
  puts e
end

source = gets.chomp!
destination = gets.chomp!
h = []
v = []
while true
	if source[0] < destination[0]
		h << 'R'
		source[0] = (source[0].ord + 1).chr
	elsif source[0] > destination[0]
		h << 'L'
		source[0] = (source[0].ord - 1).chr
	else
		h << ''
	end

	if source[1] < destination[1]
		v << 'U'
		source[1] = (source[1].to_i + 1).to_s
	elsif source[1] > destination[1]
		v << 'D'
		source[1] = (source[1].to_i - 1).to_s
	else
		v << ''
	end

	break if source == destination

end
if h.size == 1 && v.size == 1 && h[0] == '' && v[0] == ''
	puts 0
else
	puts h.size
	h.each_index {|index| puts h[index] + v[index]}
end

#!usr/bin/env ruby

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

start = gets.chomp
finish = gets.chomp
x1 = start[0]
x2 = finish[0]

y1 = start[1]
y2 = finish[1]

count = 0
moves = []

while !(x1 == x2 && y1 == y2) do
 move = ""

 if x1 < x2
  move += "R"
  x1 = x1.next
 elsif x1 > x2
  move += "L"
  x1 = (x1.ord - 1).chr
 end

 if y1 < y2
  move += "U"
  y1 = y1.next
 elsif y1 > y2
  move += "D"
  y1 = (y1.ord - 1).chr
 end
 count += 1
 moves << move
 end

puts count
moves.each { |mv| puts mv }

s=gets.split[0].bytes.to_a
t=gets.split[0].bytes.to_a
sx=s[0]-97
sy=56-s[1]
tx=t[0]-97
ty=56-t[1]
puts [(sx-tx).abs,(sy-ty).abs].max
loop do
        if sx==tx && sy==ty then
                break
        end
        if sx<tx then
                print "R"
                sx+=1
        elsif sx>tx then
                print "L"
                sx-=1
        end
        if sy<ty then
                print "D"
                sy+=1
        elsif sy>ty then
                sy-=1
                print "U"
        end
        puts
end

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

class Node
  attr_accessor :distance, :visited, :prev, :x, :y, :cost

  def initialize(x=0, y=0, cost=1)
    @distance = Float::INFINITY
    @visited = false
    @prev = nil
    @x = x
    @y = y
    @cost = cost
  end
end

def make_graph(x, y)
  (0...x).map { |x| (0...y).map { |y| Node.new(x, y) } }
end

def find_unvisited(graph)
  graph.flatten { |y| y.visited == false }
end

def min(unvisited, graph)
  min = unvisited.map { |node| node.distance }.each_with_index.min
  unvisited[min[1]]
end

def in_range(x, y, graph)
  x >= 0 && y >= 0 && x < graph.size && y < graph[0].size && !graph[x][y].visited
end

def get_neighbors(x, y, graph)
  l = [x-1, y]
  r = [x+1, y]
  t = [x, y+1]
  b = [x, y-1]
  lt = [x-1, y+1]
  rt = [x+1, y+1]
  rb = [x+1, y-1]
  lb = [x-1, y-1]

  [l,r,t,b,lt,rt,rb,lb].select { |neighbor| in_range(neighbor[0], neighbor[1], graph) }
end

def visit(node, graph)
  neighbors = get_neighbors(node.x, node.y, graph)
  neighbors.each do |n|
    neighbor = graph[n[0]][n[1]]
    neighbor.visited = true
    dist = node.distance + neighbor.cost
    if dist < neighbor.distance
      neighbor.distance = dist
      neighbor.prev = node
    end
  end
end

def dijkstra(graph, source, target)
  graph[source[0]][source[1]].distance = 0
  unvisited = find_unvisited(graph)

  while unvisited.any?
    current_node = min(unvisited, graph)
    return current_node if current_node.x == target[0] && current_node.y == target[1]
    unvisited.delete(current_node)
    visit(current_node, graph)
  end
end

def convert_source(coord)
  [coord[0].ord - 96 - 1, Integer(coord[1]) -1]
end

def to_directions(from, to)
  return if to == nil
  str = ""
  if to.x > from.x
    str << "R"
  end
  if to.x < from.x
    str << "L"
  end
  if to.y > from.y
    str << "U"
  end
  if to.y < from.y
    str << "D"
  end
  str
end

def get_answer_nodes(root)
  nodes = []
  current_node = root
  while !current_node.nil?
    nodes << current_node
    current_node = current_node.prev
  end
  nodes.reverse
end

def format_answer(answer)
  nodes = get_answer_nodes(answer)
  directions = [nodes.size-1]
  nodes.each_with_index do |node, i|
    directions << to_directions(nodes[i], nodes[i+1])
  end
  directions
end

def solve
  graph = make_graph(8, 8)
  source = gets
  target = gets
  answer = dijkstra(graph, convert_source(source.chomp), convert_source(target.chomp))
  puts format_answer(answer).join("\n")
end

solve()

s = gets
t = gets
sx = s[0..0].ord
sy = s[1..1].to_i
tx = t[0..0].ord
ty = t[1..1].to_i
dx = sx-tx
dy = sy-ty
step = [dx.abs,dy.abs].max
puts step
step.times do
  if dx<0
    print "R"
    dx += 1
  elsif dx>0
    print "L"
    dx -= 1
  end
  if dy<0
    print "U"
    dy += 1
  elsif dy>0
    print "D"
    dy -= 1
  end
  puts
end

input = gets
king = [input[0].ord - 96, input[1].ord - 48]
input = gets
target = [input[0].ord - 96, input[1].ord - 48]

path = Array.new

while king != target
  s = String.new
  if king[0] < target[0]
    s << "R"
    king[0] += 1
  elsif king[0] > target[0]
    s << "L"
    king[0] -= 1
  end
  if king[1] < target[1]
    s << "U"
    king[1] += 1
  elsif king[1] > target[1]
    s << "D"
    king[1] -= 1
  end
  path.concat([s])
end

puts path.count
path.each {|x| puts x}

s, t = gets.to_s, gets.to_s
row = t[0].ord - s[0].ord
col = t[1].ord - s[1].ord
if row.abs > col.abs
	puts row.abs
	rc = 1
	diff = (row.abs - col.abs).to_i
	sqr = row.abs - diff
else
	puts col.abs
	rc = 0
	diff = (col.abs - row.abs).to_i
	sqr = col.abs - diff
end
if row >= 0 and col >= 0
	sqr.times{
		puts "RU"
	}
	if rc == 1
		diff.times{
			puts "R"
		}
	else
		diff.times{
			puts "U"
		}
	end
elsif row <= 0 and col >= 0
	sqr.times{
		puts "LU"
	}
	if rc == 1
		diff.times{
			puts "L"
		}
	else
		diff.times{
			puts "U"
		}
	end
elsif row <= 0 and col <= 0
	sqr.times{
		puts "LD"
	}
	if rc == 1
		diff.times{
			puts "L"
		}
	else
		diff.times{
			puts "D"
		}
	end
else
	sqr.times{
		puts "RD"
	}
	if rc == 1
		diff.times{
			puts "R"
		}
	else
		diff.times{
			puts "D"
		}
	end
end

class King
  def initialize(s, t)
    @start = s
    @end = t
  end

  def get_path
    return 0 if @start == @end
    result = []
    horizontal = (@start[0] == @end[0])
    vertical = (@start[1] == @end[1])

    current = @start
    steps = 0
    until horizontal || vertical
      current = move_diag(current)
      horizontal = (current[0] == @end[0])
      vertical = (current[1] == @end[1])
      result << direction.join
      steps += 1
    end

    if vertical
      current_steps = (current[0].ord.to_i - @end[0].ord.to_i).abs
      result += [direction.first]*current_steps
      steps += current_steps
    elsif horizontal
      current_steps = (current[1].to_i - @end[1].to_i).abs
      result += [direction.last]*current_steps
      steps += current_steps
    end
    result.unshift(steps)

    # if @start == @end
    #   result << 0
    # elsif horizontal
    #   result = [steps_x.abs] + [direction.last]*steps_x.abs
    # elsif vertical
    #   result = [steps_y.abs] + [direction.first]*steps_y.abs
    # else
      # current = @start
      # steps = 0
      # until horizontal || vertical
      #   current = move_diag(current)
      #   horizontal = (current[0] == @end[0])
      #   vertical = (current[1] == @end[1])
      #   result << direction.join
      #   steps += 1
      # end
    # end
    result
  end

  def move_diag(position)
    x_inc = steps_x == 0 ? 0 : steps_x/steps_x.abs
    y_inc = steps_y == 0 ? 0 : steps_y/steps_y.abs
    [(position[0].ord - y_inc).chr, (position[1].to_i - x_inc)].join
  end

  def direction
    [direction_y, direction_x]
  end

  def direction_x
    steps_x > 0 ? 'D' : 'U'
  end

  def direction_y
    steps_y > 0 ? 'L' : 'R'
  end

  def steps_x
    @start[1].to_i - @end[1].to_i
  end

  def steps_y
    @start[0].ord.to_i - @end[0].ord.to_i
  end
end

s, t = [gets.chomp, gets.chomp]
game = King.new(s, t)
puts game.get_path

a = gets.chomp
b = gets.chomp
x, y = a[0].ord-b[0].ord, a[1].to_i-b[1].to_i
puts [x.abs, y.abs].max
s = ""
if x.abs > y.abs
    s = (x>0 ? "L" : "R")
else
    s = (y>0 ? "D" : "U")
end
print (s+"\n")*(x.abs-y.abs).abs
if x > 0
    s = (y > 0 ? "LD" : "LU")
else
    s = (y > 0 ? "RD" : "RU")
end
print (s+"\n")*[x.abs, y.abs].min

s=gets

t=gets

r=t[0].ord-s[0].ord

c=t[1].hex-s[1].hex

a=[""]*9

f=lambda{|r,c|r.times{|i|a[i]+=c}}

f.call(r,"R")

f.call(-r,"L")

f.call(c,"U")

f.call(-c,"D")

a.select!{|i|i!=""}

p a.size

puts a

include Math
a=0
"a".each_byte do |n|
  a=n
end
memo=gets.chomp!.scan(/./)
sx=0
memo[0].each_byte do |n|
  sx=n-a+1
end
sy=memo[1].to_i
ex=0
memo=gets.chomp!.scan(/./)
memo[0].each_byte do |n|
  ex=n-a+1
end
ey=memo[1].to_i
xx=ex-sx
yy=ey-sy
hosu=[xx<0 ? -xx : xx,yy<0 ? -yy :yy].max
puts hosu
if xx >=0 && yy>=0
  if xx>=yy
    yy.times do
      puts "RU"
    end
    (xx-yy).times do
      puts "R"
    end
  else
    xx.times do
      puts "RU"
    end
    (yy-xx).times do
      puts "U"
    end
  end
end
if xx >=0 && yy<0
  yy=-yy
  if xx>=yy
    yy.times do
      puts "RD"
    end
    (xx-yy).times do
      puts "R"
    end
  else
    xx.times do
      puts "RD"
    end
    (yy-xx).times do
      puts "D"
    end
  end
end
if xx < 0 && yy >=0
  xx=-xx
  if xx>=yy
    yy.times do
      puts "LU"
    end
    (xx-yy).times do
      puts "L"
    end
  else
    xx.times do
      puts "LU"
    end
    (yy-xx).times do
      puts "U"
    end
  end
end
if xx <0 && yy<0
  xx=-xx
  yy=-yy
  if xx>=yy
    yy.times do
      puts "LD"
    end
    (xx-yy).times do
      puts "L"
    end
  else
    xx.times do
      puts "LD"
    end
    (yy-xx).times do
      puts "D"
    end
  end
end

s=gets.chomp.split("")
t=gets.chomp.split("")
valu=1..8
alph=('a' .. 'h')
valu=[*valu]
alph=[*alph]
valu_hash={}
i=0
loop do
	break if i==8
	valu_hash[alph[i]]=valu[i]
	i+=1
 end
dir_arry=[]
if valu_hash[s[0]]< valu_hash[t[0]]
   dir_arry[0]="R"
elsif valu_hash[s[0]]> valu_hash[t[0]]
   dir_arry[0]="L"
else
   dir_arry[0]=0
end

if s[1].to_i< t[1].to_i
   dir_arry[1]="U"
elsif s[1].to_i> t[1].to_i
   dir_arry[1]="D"
else
   dir_arry[1]=0
end

hmove=valu_hash[s[0]] - valu_hash[t[0]]
if hmove<0
hmove=hmove*(-1)
end
vmove=s[1].to_i-t[1].to_i
if vmove<0
vmove=vmove*(-1)
end

if hmove==0
	puts vmove
	vmove.times{puts dir_arry[1]}
elsif vmove==0
	puts hmove
	hmove.times{puts dir_arry[0] }
elsif vmove==hmove
	puts hmove
    hmove.times{puts dir_arry[0]+dir_arry[1] }
elsif hmove>vmove
	puts hmove
	vmove.times{puts dir_arry[0]+dir_arry[1] }
	(hmove-vmove).times {puts dir_arry[0]}
elsif vmove>hmove
	puts vmove
	hmove.times{puts dir_arry[0]+dir_arry[1] }
	(vmove-hmove).times{puts dir_arry[1]}
end

s1 = gets
s2 = gets
dst1 = s2[0].ord - s1[0].ord
dst2 = s2[1].to_i - s1[1].to_i

puts [dst2.abs, dst1.abs].max
while dst2 != 0 or dst1 != 0 do
    if dst1 > 0 then
        printf "R"
        dst1 -= 1
    end
    if dst1 < 0 then
        printf "L"
        dst1 += 1
    end

    if dst2 > 0 then
        printf "U"
        dst2 -= 1
    end
    if dst2 < 0 then
        printf "D"
        dst2 += 1
    end
    puts
end

def get_path(src, dest)
  s_x, s_y = src.chars
  d_x, d_y = dest.chars
  path = []
  while s_x != d_x || s_y != d_y
    move = ''
    if (s_x > d_x)
      s_x = (s_x.ord - 1).chr
      move += 'L'
    elsif (s_x < d_x)
      s_x.next!
      move += 'R'
    end

    if (s_y > d_y)
      s_y = (s_y.ord - 1).chr
      move += 'D'
    elsif (s_y < d_y)
      s_y.next!
      move += 'U'
    end

    path << move
  end
  path
end

if __FILE__ == $0
  path = get_path(gets, gets)
  puts path.size
  path.each { |m| puts m }
end

start_point = gets.chomp
end_point = gets.chomp

character_start_point = start_point[0]
number_start_point = start_point[1].to_i

character_end_point = end_point[0]
number_end_point = end_point[1].to_i

vertical_moves_count = (number_start_point-number_end_point).abs
vertical_moves_character = number_start_point > number_end_point ? 'D' : 'U'

horizontal_moves_count = (character_start_point.ord - character_end_point.ord).abs
horizontal_moves_character = character_start_point.ord < character_end_point.ord ? 'R' : 'L'

max_steps = vertical_moves_count > horizontal_moves_count ? vertical_moves_count : horizontal_moves_count

puts max_steps

while max_steps > 0
  print horizontal_moves_character if horizontal_moves_count > 0
  print vertical_moves_character if vertical_moves_count > 0
  puts
  horizontal_moves_count -= 1
  vertical_moves_count -= 1
  max_steps -= 1
end

def f(x,y,gx,gy)
	if x==gx && y==gy then
		return ""
	else
		res=""
		x1=x
		y1=y
		if x<gx then
			x1=x+1
			res+="R"
		elsif x>gx
			x1=x-1
			res+="L"
		end
		if y<gy then
			y1=y+1
			res+="U"
		elsif y>gy then
			y1=y-1
			res+="D"
		end
		return res+"\n"+f(x1,y1,gx,gy)
	end

end

h=	Hash["a",1,"b",2,"c",3,"d",4,
		 "e",5,"f",6,"g",7,"h",8]

s1=gets.chomp
s2=gets.chomp
if s1==s2 then
	puts "0\n"
else
	x=f(h[s1[0]],s1[1].to_i,h[s2[0]],s2[1].to_i).split()
	puts x.size
	puts x*"\n"
end

start = gets().chomp!
finish =  gets().chomp!
LETTERS = ["a", "b", "c", "d", "e", "f", "g", "h"]
h_distance, v_distance = [LETTERS.index(finish[0,1]) - LETTERS.index(start[0,1]), finish[1,1].to_i - start[1,1].to_i]
moves = Array.new(7){""}
h_distance.abs().times {
|i|
  h_distance > 0 ? moves[i] << "R" : moves[i] << "L"
}
v_distance.abs().times {
|i|
  v_distance > 0 ? moves[i] << "U" : moves[i] << "D"
}
moves = moves.select(){|elem| elem != ""}
puts moves.length, moves

start = gets().chomp!
finish =  gets().chomp!
LETTERS = ["a", "b", "c", "d", "e", "f", "g", "h"]
h_distance = LETTERS.index(finish[0,1]) - LETTERS.index(start[0,1])
v_distance = finish[1,1].to_i - start[1,1].to_i
moves = Array.new(7){""}
h_distance.abs().times {
|i|
  h_distance > 0 ? moves[i] << "R" : moves[i] << "L"
}
v_distance.abs().times {
|i|
  v_distance > 0 ? moves[i] << "U" : moves[i] << "D"
}
moves = moves.select(){|elem| elem != ""}
puts moves.length, moves

s,t = readlines.join.split
sx,sy = s[0].ord - 'a'.ord, s[1].to_i-1
gx,gy = t[0].ord - 'a'.ord, t[1].to_i-1
a=[""]*9
f = lambda{|r,c| r.times{|i| a[i] += c}}
f.call(gx-sx,'R')
f.call(sx-gx,'L')
f.call(sy-gy,'D')
f.call(gy-sy,'U')
a.select!{|i| i != ""}
puts a.size
puts a

s,t = readlines.join.split
sx,sy = s[0].ord - 'a'.ord, s[1].to_i-1
gx,gy = t[0].ord - 'a'.ord, t[1].to_i-1

dx    = (sx-gx).abs
dy    = (sy-gy).abs
xchar = sx < gx ? 'R' : 'L'
ychar = sy < gy ? 'U' : 'D'

ans = []
cnt = 0
while dx > 0 && dy > 0
  ans << (xchar+ychar)
  dx -= 1
  dy -= 1
end

while dx > 0
  ans << xchar
  dx -= 1
end

while dy > 0
  ans << ychar
  dy -= 1
end

puts ans.size
puts ans

start = gets
start_x = start[0].ord - 97
start_y = start[1].to_i - 1

finish = gets
finish_x = finish[0].ord - 97
finish_y = finish[1].to_i - 1

result = []

if start_x < finish_x
	# go right
	(finish_x - start_x).times do
		result << ["R"]
	end
else
	# go left
	(start_x - finish_x).times do
		result << ["L"]
	end
end

if start_y < finish_y
	# go up
	(finish_y - start_y).times do |index|
		if result[index].nil?
			result << ["U"]
		else
			result[index][0]<< "U"
		end
	end
else
	# go down
	(start_y - finish_y).times do |index|
		if result[index].nil?
			result << ["D"]
		else
			result[index][0]<< "D"
		end
	end
end

puts result.length
puts result

s = STDIN.readline
t = STDIN.readline

print [(s[0].ord-t[0].ord).abs,(s[1].ord-t[1].ord).abs].max
print ?\n

while s != t ;
	if s[0] < t[0] ;
		print ?R
		s[0] = (s[0].ord+1).chr
	elsif s[0] > t[0] ;
		print ?L
		s[0] = (s[0].ord-1).chr
	end
	if s[1] < t[1] ;
		print ?U
		s[1] = (s[1].ord+1).chr
	elsif s[1] > t[1] ;
		print ?D
		s[1] = (s[1].ord-1).chr
	end
	print ?\n
end

class Solver
  def solve(*a)
    p = a[0]
    t = a[1]

    moves = []
    while p != t && moves.size < 8
      #puts "#{p} -> #{t}"

      moves << navigate(p,t)
    end
    moves
  end

  def navigate(p,t)
    dx = dir(t[0].ord - p[0].ord)
    dy = dir(t[1].ord - p[1].ord)

    p[0] = (p[0].ord + dx).chr
    p[1] = (p[1].ord + dy).chr

    ['L','','R'][dx+1] + ['D','','U'][dy+1]
  end

  def dir(d)
    return 0 if d == 0
    return 1 if d > 0
    return -1
  end
end

if __FILE__==$0
  a = gets.chomp
  b = gets.chomp
  s = Solver.new
  sol = s.solve(a,b)
  puts sol.count
  sol.each do |s|
    puts s
  end
end

s,t = gets.strip.bytes.to_a, gets.strip.bytes.to_a
moves = []
while s != t
  move = ""
  if s[0] < t[0]
    s[0] += 1
    move += "R"
  elsif s[0] > t[0]
    s[0] -= 1
    move += "L"
  end
  if s[1] < t[1]
    s[1] += 1
    move += "U"
  elsif s[1] > t[1]
    s[1] -= 1
    move += "D"
  end
  moves << move
end

puts moves.size
for s in moves do
  puts s
end

coord_src = gets
coord_dest = gets

coord_src_x = 1 + coord_src[0].ord - 'a'.ord
coord_src_y = coord_src[1].to_i
coord_dest_x = 1 + coord_dest[0].ord - 'a'.ord
coord_dest_y = coord_dest[1].to_i

#puts coord_src_x.to_s + " " + coord_src_y.to_s
#puts coord_dest_x.to_s + " " + coord_dest_y.to_s

moves = []

# Firstly, move diagonally in the correct direction.
while (coord_src_x != coord_dest_x && coord_src_y != coord_dest_y) do
  if coord_src_x > coord_dest_x
    coord_src_x -= 1
    if coord_src_y > coord_dest_y
      moves.push("LD")
      coord_src_y -= 1
    else
      moves.push("LU")
      coord_src_y += 1
    end
  else
   coord_src_x += 1
    if coord_src_y > coord_dest_y
      moves.push("RD")
      coord_src_y -= 1
    else
      moves.push("RU")
      coord_src_y += 1
    end
  end
end

# Now it is just a straightaway
if(coord_src_x > coord_dest_x)
  (coord_src_x - coord_dest_x).times { moves.push("L") }
elsif (coord_src_x < coord_dest_x)
  (coord_dest_x - coord_src_x).times { moves.push("R") }

elsif(coord_src_y > coord_dest_y)
  (coord_src_y - coord_dest_y).times { moves.push("D") }
elsif (coord_src_y < coord_dest_y)
  (coord_dest_y - coord_src_y).times { moves.push("U") }
end

puts moves.size
moves.each { |move| puts move }

input = STDIN.read.split("\n")
a, b = input[0].split(''), input[1].split('')
a[1], b[1] = a[1].to_i, b[1].to_i

if a == b
	STDOUT.print 0
	exit
end

if a[0] > b[0]
	x_dir = "L"
elsif a[0] < b[0]
	x_dir = "R"
end

if a[1] > b[1]
	y_dir = "D"
elsif a[1] < b[1]
	y_dir = "U"
end

str = ""
count = 0

while true do

	if a[0] != b[0]
		a[0].next! if x_dir == "R"
		a[0] = (a[0].ord - 1).chr if x_dir == "L"
		str << x_dir
	end

	if a[1] != b[1]
		a[1] += 1 if y_dir == "U"
		a[1] -= 1 if y_dir == "D"
		str << y_dir
	end

	count += 1
	str << "\n"

	break if a == b

end

STDOUT.print count, "\n", str

input = STDIN.read.split("\n")
a, b = input[0].split(''), input[1].split('')
a[0], b[0], a[1], b[1] = a[0].ord, b[0].ord, a[1].to_i, b[1].to_i

if a == b
	STDOUT.print 0
	exit
end

x_dir = a[0] > b[0] ? "L" : "R"
y_dir = a[1] > b[1] ? "D" : "U"

str, count = '', 0

loop do

	if a[0] != b[0]
		x_dir == "R" ? a[0] += 1 : a[0] -= 1
		str << x_dir
	end

	if a[1] != b[1]
		y_dir == "U" ? a[1] += 1 : a[1] -= 1
		str << y_dir
	end

	count += 1
	str << "\n"

	break if a == b

end

STDOUT.print count, "\n", str

input = STDIN.read.split("\n")
a, b = input[0].split(''), input[1].split('')
a[1], b[1] = a[1].to_i, b[1].to_i

if a == b
	STDOUT.print 0
	exit
end

x_dir = a[0] > b[0] ? "L" : "R"
y_dir = a[1] > b[1] ? "D" : "U"

str, count = '', 0

while true do

	if a[0] != b[0]
		a[0] = x_dir == "R" ? a[0].next : (a[0].ord - 1).chr
		str << x_dir
	end

	if a[1] != b[1]
		y_dir == "U" ? a[1] += 1 : a[1] -= 1
		str << y_dir
	end

	count += 1
	str << "\n"

	break if a == b

end

STDOUT.print count, "\n", str

# require 'pry-byebug'
file = File.exist?('in.in') ? File.new('in.in','r') : STDIN

DX = [0, 1, 0, -1, 1, -1, 1, -1]
DY = [1, 0, -1, 0, 1, -1, -1, 1]
MOV = ["R", "D", "L", "U", "RD", "LU", "LD", "RU"]

def inside?(i, j, grid)
  i>=0 && i<= grid.size-1 && j>=0 && j <=grid[0].size-1
end

map = {"a"=> 0, "b"=> 1, "c"=> 2, "d"=> 3, "e"=> 4,"f"=> 5, "g"=> 6, "h" => 7}

s1,s2 = file.gets.chomp.split('')
t1,t2 = file.gets.chomp.split('')

return puts 0 if s1==t1 && s2==t2

s1 = map[s1]
s2 = (s2.to_i-1-7).abs
t1 = map[t1]
t2 = (t2.to_i-1-7).abs

# puts "#{s1}#{s2}"
# puts "#{t1}#{t2}"

grid = Array.new(8) { Array.new(8) }
dist = Array.new(8) { Array.new(8, 100) }
queue = [[s1, s2]]
dist[s1][s2]=0
grid[s1][s2]=[]

while(queue.size != 0)
  x, y  = queue.shift
  8.times do |i|
    new_x = x+DX[i]
    new_y = y+DY[i]
    if inside?(new_x, new_y, grid) && dist[new_x][new_y]==100

      if dist[x][y]+1 < dist[new_x][new_y]
        dist[new_x][new_y]  = dist[x][y]+1
        grid[new_x][new_y] = grid[x][y] + [MOV[i]]
      end

      # dist[new_x][new_y] = [dist[new_x][new_y],dist[x][y]+1].min
      break if new_x == t1 && new_y == t2
      # binding.pry if dist[new_x][new_y] == 7
      queue << [new_x, new_y]
    end
  end
end
puts dist[t1][t2]
grid[t1][t2].each do |e|
  puts e
end

#!usr/bin/env ruby

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

start = gets.chomp
finish = gets.chomp

while ! start.eql? finish do
 move = ""

 if start[0] < finish[0]
  move += "U"
  start[0] = start[0].next
 elsif start[0] > finish[0]
  move += "D"
  start[0] = (start[0].ord - 1).chr
 end

 if start[1] < finish[1]
  move += "R"
  start[1] = start[1].next
 elsif start[1] > finish[1]
  move += "L"
  start[1] = (start[1].ord - 1).chr
 end

 puts move
end

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

class Node
  attr_accessor :distance, :visited, :prev, :x, :y, :cost

  def initialize(x=0, y=0, cost=1)
    @distance = Float::INFINITY
    @visited = false
    @prev = nil
    @x = x
    @y = y
    @cost = cost
  end
end

def make_graph(x, y)
  (0...x).map { |x| (0...y).map { |y| Node.new(x, y) } }
end

def find_unvisited(graph)
  graph.flatten { |y| y.visited == false }
end

def min(unvisited, graph)
  min = unvisited.map { |node| node.distance }.each_with_index.min
  unvisited[min[1]]
end

def in_range(x, y, graph)
  x >= 0 && y >= 0 && x < graph.size && y < graph[0].size && !graph[x][y].visited
end

def get_neighbors(x, y, graph)
  l = [x-1, y]
  r = [x+1, y]
  t = [x, y+1]
  b = [x, y-1]
  lt = [x-1, y+1]
  rt = [x+1, y+1]
  rb = [x+1, y-1]
  lb = [x-1, y-1]

  [l,r,t,b,lt,rt,rb,lb].select { |neighbor| in_range(neighbor[0], neighbor[1], graph) }
end

def visit(node, graph)
  neighbors = get_neighbors(node.x, node.y, graph)
  neighbors.each do |n|
    neighbor = graph[n[0]][n[1]]
    neighbor.visited = true
    dist = node.distance + neighbor.cost
    if dist < neighbor.distance
      neighbor.distance = dist
      neighbor.prev = node
    end
  end
end

def dijkstra(graph, source, target)
  graph[source[0]][source[1]].distance = 0
  unvisited = find_unvisited(graph)

  while unvisited.any?
    current_node = min(unvisited, graph)
    return current_node if current_node.x == target[0] && current_node.y == target[1]
    unvisited.delete(current_node)
    visit(current_node, graph)
  end
end

def convert_source(coord)
  [coord[0].ord - 96 - 1, Integer(coord[1]) -1]
end

def to_directions(from, to)
  return if to == nil
  str = ""
  if to.x > from.x
    str << "R"
  end
  if to.y > from.y
    str << "U"
  end
  if to.y < from.y
    str << "D"
  end
  if to.x < from.x
    str << "L"
  end
  str
end

def get_answer_nodes(root)
  nodes = []
  current_node = root
  while !current_node.nil?
    nodes << current_node
    current_node = current_node.prev
  end
  nodes.reverse
end

def format_answer(answer)
  nodes = get_answer_nodes(answer)
  directions = [nodes.size-1]
  nodes.each_with_index do |node, i|
    directions << to_directions(nodes[i], nodes[i+1])
  end
  directions
end

def solve
  graph = make_graph(8, 8)
  source = gets
  target = gets
  answer = dijkstra(graph, convert_source(source.chomp), convert_source(target.chomp))
  puts format_answer(answer).join("\n")
end

solve()

s = gets
t = gets
sx = s[0..0].ord
sy = s[1..1].to_i
tx = t[0..0].ord
ty = t[1..1].to_i
dx = sx-tx
dy = sy-ty
step = [dx.abs,dy.abs].max
puts step
step.times do
  if dx<0
    print "R"
    dx += 1
  elsif dx>0
    print "L"
    dx -= 1
  end
  if dy<0
    print "U"
    dy += 1
  elsif dy>0
    print "D"
    dy += 1
  end
  puts
end

class King
  def initialize(s, t)
    @start = s
    @end = t
  end

  def get_path
    result = []
    horizontal = (@start[0] == @end[0])
    vertical = (@start[1] == @end[1])
    if @start == @end
      result << 0
    elsif horizontal
      result = [steps_x.abs] + [direction.last]*steps_x.abs
    elsif vertical
      result = [steps_y.abs] + [direction.first]*steps_y.abs
    else
      current = @start
      steps = 0
      until horizontal || vertical
        current = move_diag(current)
        horizontal = (current[0] == @end[0])
        vertical = (current[1] == @end[1])
        result << direction.join
        steps += 1
      end
      if vertical
        current_steps = (current[0].ord.to_i - @end[0].ord.to_i).abs
        result += [direction.last]*current_steps
        steps += current_steps
      elsif horizontal
        current_steps = (current[1].to_i - @end[1].to_i).abs
        result += [direction.first]*current_steps
        steps += current_steps
      end
      result.unshift(steps)
    end
    result
  end

  def move_diag(position)
    x_inc = steps_x == 0 ? 0 : steps_x/steps_x.abs
    y_inc = steps_y == 0 ? 0 : steps_y/steps_y.abs
    [(position[0].ord+x_inc).chr, (position[1].to_i + y_inc)].join
  end

  def direction
    [direction_y, direction_x]
  end

  def direction_x
    steps_x > 0 ? 'D' : 'U'
  end

  def direction_y
    steps_y > 0 ? 'L' : 'R'
  end

  def steps_x
    @start[1].to_i - @end[1].to_i
  end

  def steps_y
    @start[0].ord.to_i - @end[0].ord.to_i
  end
end

s, t = [gets.chomp, gets.chomp]
game = King.new(s, t)
puts game.get_path

class King
  def initialize(s, t)
    @start = s
    @end = t
  end

  def get_path
    result = []
    horizontal = (@start[0] == @end[0])
    vertical = (@start[1] == @end[1])
    if @start == @end
      result << 0
    elsif horizontal
      result = [steps_x] + [direction.last]*steps_x.abs
    elsif vertical
      result = [steps_y] + [direction.first]*steps_y.abs
    else
      current = @start
      steps = 0
      until horizontal || vertical
        current = move_diag(current)
        horizontal = (current[0] == @end[0])
        vertical = (current[1] == @end[1])
        result << direction.join
        steps += 1
      end
      if vertical
        current_steps = (current[0].ord.to_i - @end[0].ord.to_i).abs
        result += [direction.last]*current_steps
        steps += current_steps
      elsif horizontal
        current_steps = (current[1].to_i - @end[1].to_i).abs
        result += [direction.first]*current_steps
        steps += current_steps
      end
      result.unshift(steps)
    end
    result
  end

  def move_diag(position)
    x_inc = steps_x == 0 ? 0 : steps_x/steps_x.abs
    y_inc = steps_y == 0 ? 0 : steps_y/steps_y.abs
    [(position[0].ord+x_inc).chr, (position[1].to_i + y_inc)].join
  end

  def direction
    [direction_y, direction_x]
  end

  def direction_x
    steps_x > 0 ? 'D' : 'U'
  end

  def direction_y
    steps_y > 0 ? 'L' : 'R'
  end

  def steps_x
    @start[1].to_i - @end[1].to_i
  end

  def steps_y
    @start[0].ord.to_i - @end[0].ord.to_i
  end
end

s, t = [gets.chomp, gets.chomp]
game = King.new(s, t)
puts game.get_path

class King
  def initialize(s, t)
    @start = s
    @end = t
  end

  def get_path
    result = []
    horizontal = (@start[0] == @end[0])
    vertical = (@start[1] == @end[1])
    if @start == @end
      result << 0
    elsif horizontal
      result = [steps_x] + [direction.first]*steps_x.abs
    elsif vertical
      result = [steps_y] + [direction.last]*steps_y.abs
    else
      current = @start
      steps = 0
      until horizontal || vertical
        current = move_diag(current)
        horizontal = (current[0] == @end[0])
        vertical = (current[1] == @end[1])
        result << direction.join
        steps += 1
      end
      if vertical
        current_steps = (current[0].ord.to_i - @end[0].ord.to_i).abs
        result += [direction.last]*current_steps
        steps += current_steps
      elsif horizontal
        current_steps = (current[1].to_i - @end[1].to_i).abs
        result += [direction.first]*current_steps
        steps += current_steps
      end
      result.unshift(steps)
    end
    result
  end

  def move_diag(position)
    x_inc = steps_x == 0 ? 0 : steps_x/steps_x.abs
    y_inc = steps_y == 0 ? 0 : steps_y/steps_y.abs
    [(position[0].ord+x_inc).chr, (position[1].to_i + y_inc)].join
  end

  def direction
    [direction_y, direction_x]
  end

  def direction_x
    steps_x > 0 ? 'D' : 'U'
  end

  def direction_y
    steps_y > 0 ? 'L' : 'R'
  end

  def steps_x
    @start[1].to_i - @end[1].to_i
  end

  def steps_y
    @start[0].ord.to_i - @end[0].ord.to_i
  end
end

s, t = [gets.chomp, gets.chomp]
game = King.new(s, t)
puts game.get_path

s1 = gets
s2 = gets
dst1 = s2[0].ord - s1[0].ord
dst2 = s2[1].to_i - s1[1].to_i

puts [dst2.abs, dst1.abs].max
while dst2 != 0 and dst1 != 0 do
    if dst1 > 0 then
        printf "R"
        dst1 -= 1
    end
    if dst1 < 0 then
        printf "L"
        dst1 += 1
    end

    if dst2 > 0 then
        printf "U"
        dst2 -= 1
    end
    if dst2 < 0 then
        printf "D"
        dst2 += 1
    end
    puts
end

start = gets().chomp!
finish =  gets().chomp!
LETTERS = ["a", "b", "c", "d", "e", "f", "g", "h"]
h_distance, v_distance = [LETTERS.index(finish[0,1]) - LETTERS.index(start[0,1]), finish[1,1].to_i - start[1,1].to_i]
moves = Array.new(7){""}
h_distance.abs().times {
|i|
  h_distance > 0 ? moves[i] << "R" : moves[i] << "L"
}
v_distance.abs().times {
|i|
  v_distance > 0 ? moves[i] << "U" : moves[i] << "D"
}
moves.select(){|elem| elem != ""}
puts moves.length, moves

s,t = readlines.join.split
sx,sy = s[0].ord - 'a'.ord, s[1].to_i-1
gx,gy = t[0].ord - 'a'.ord, t[1].to_i-1
a=[""]*9
f = lambda{|r,c| r.times{|i| a[i] += c}}
f.call(sx-gx,'R')
f.call(gx-sx,'L')
f.call(sy-gy,'D')
f.call(gy-sy,'U')
a.select!{|i| i != ""}
puts a.size
puts a

a, b = gets.chop.split(''), gets.chop.split('')
a[1], b[1] = a[1].to_i, b[1].to_i

if a[0] > b[0]
	x_dir = "L"
elsif a[0] < b[0]
	x_dir = "R"
end

if a[1] > b[1]
	y_dir = "D"
elsif a[1] < b[1]
	y_dir = "U"
end

str = ""
count = 0

while true do

	if a[0] != b[0]
		a[0].next! if x_dir == "R"
		a[0].prev! if x_dir == "L"
		str << x_dir
	end

	if a[1] != b[1]
		a[1] += 1 if y_dir == "U"
		a[1] -= 1 if y_dir == "D"
		str << y_dir
	end

	count += 1
	str << "\n"

	break if a == b

end

STDOUT.print count, "\n", str


There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle with its sides parallel to the sides of the table. If you will take a look from above and assume that the bottom left corner of the table has coordinates $$$(0, 0)$$$, and coordinate axes are left and bottom sides of the table, then the bottom left corner of the white sheet has coordinates $$$(x_1, y_1)$$$, and the top right — $$$(x_2, y_2)$$$.After that two black sheets of paper are placed on the table. Sides of both black sheets are also parallel to the sides of the table. Coordinates of the bottom left corner of the first black sheet are $$$(x_3, y_3)$$$, and the top right — $$$(x_4, y_4)$$$. Coordinates of the bottom left corner of the second black sheet are $$$(x_5, y_5)$$$, and the top right — $$$(x_6, y_6)$$$.     Example of three rectangles. Determine if some part of the white sheet can be seen from the above after the two black sheets are placed. The part of the white sheet can be seen if there is at least one point lying not strictly inside the white sheet and strictly outside of both black sheets.

The first line of the input contains four integers $$$x_1, y_1, x_2, y_2$$$ $$$(0 \le x_1 < x_2 \le 10^{6}, 0 \le y_1 < y_2 \le 10^{6})$$$ — coordinates of the bottom left and the top right corners of the white sheet. The second line of the input contains four integers $$$x_3, y_3, x_4, y_4$$$ $$$(0 \le x_3 < x_4 \le 10^{6}, 0 \le y_3 < y_4 \le 10^{6})$$$ — coordinates of the bottom left and the top right corners of the first black sheet. The third line of the input contains four integers $$$x_5, y_5, x_6, y_6$$$ $$$(0 \le x_5 < x_6 \le 10^{6}, 0 \le y_5 < y_6 \le 10^{6})$$$ — coordinates of the bottom left and the top right corners of the second black sheet. The sides of each sheet of paper are parallel (perpendicular) to the coordinate axes.

If some part of the white sheet can be seen from the above after the two black sheets are placed, print "YES" (without quotes). Otherwise print "NO".

NoteIn the first example the white sheet is fully covered by black sheets.In the second example the part of the white sheet can be seen after two black sheets are placed. For example, the point $$$(6.5, 4.5)$$$ lies not strictly inside the white sheet and lies strictly outside of both black sheets.

#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']

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

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

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

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

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

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

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

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

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

hx = {}
hy = {}
a = inp
b = inp
c = inp
x = [a[0],a[2],b[0],b[2],c[0],c[2]].sort
y = [a[1],a[3],b[1],b[3],c[1],c[3]].sort
x.each.with_index do |d,i|
  hx[d] = i
end
y.each.with_index do |d,i|
  hy[d] = i
end
t = na2(x.size,y.size,false)
(hx[a[0]]...hx[a[2]]).each do |x|
  (hy[a[1]]...hy[a[3]]).each do |y|
    t[x][y] = true
  end
end
(hx[b[0]]...hx[b[2]]).each do |x|
  (hy[b[1]]...hy[b[3]]).each do |y|
    t[x][y] = false
  end
end
(hx[c[0]]...hx[c[2]]).each do |x|
  (hy[c[1]]...hy[c[3]]).each do |y|
    t[x][y] = false
  end
end

puts (t.flatten.include?(true))? "YES" : "NO"

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 bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} 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
########### ( ˘ω˘ )ｽﾔｧ… ###########
x1,y1,x2,y2=gsmi
x3,y3,x4,y4=gsmi
x5,y5,x6,y6=gsmi

# full cover
if (x3<=x1&&y3<=y1&&x4>=x2&&y4>=y2)||(x5<=x1&&y5<=y1&&x6>=x2&&y6>=y2)
  putsend('NO')
end
# half cover
# たて
if (y3<=y1&&y4>=y2 &&y5<=y1&&y6>=y2) && ((x5<=x2&&x6>=x2&&x3<=x1&&x4>=x1&&x4>=x5)||(x3<=x2&&x4>=x2&&x5<=x1&&x6>=x1&&x6>=x3))
  putsend('NO')
end
# よこ
if (x3<=x1&&x4>=x2 &&x5<=x1&&x6>=x2) && ((y5<=y2&&y6>=y2&&y3<=y1&&y4>=y1&&y4>=y5)||(y3<=y2&&y4>=y2&&y5<=y1&&y6>=y1&&y6>=y3))
  putsend('NO')
end

putsend('YES')

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 bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} 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
########### ( ˘ω˘ )ｽﾔｧ… ###########
x1,y1,x2,y2=gsmi
x3,y3,x4,y4=gsmi
x5,y5,x6,y6=gsmi

# full cover
if (x3<=x1&&y3<=y1&&x4>=x2&&y4>=y2)||(x5<=x1&&y5<=y1&&x6>=x2&&y6>=y2)
  putsend('NO')
end
# half cover
# たて
if (y3<=y1&&y4>=y2 &&y5<=y1&&y6>=y2) && ((x5<=x2&&x6>=x2&&x3<=x1&&x4>=x1&&x4>=x5)||(x3<=x2&&x4>=x2&&x5<=x1&&x6>=x1&&x6>=x4))
  putsend('NO')
end
# よこ
if (x3<=x1&&x4>=x2 &&x5<=x1&&x6>=x2) && ((y5<=y2&&y6>=y2&&y3<=y1&&y4>=y1&&y4>=y5)||(y3<=y2&&y4>=y2&&y5<=y1&&y6>=y1&&y6>=y4))
  putsend('NO')
end

putsend('YES')


The input contains a single integer a (0 ≤ a ≤ 1 000 000).

a=gets.chomp.to_i.to_s(8)
b=a.count("1")
puts "#{b}"

n = gets.to_i

puts n.to_s(8).count('1')

# print number of ones in BASE 8 notaion of a
p gets.to_i.to_s(8).chars.count ?1

#!/usr/bin/env ruby
puts gets.to_i.to_s(8).chars.count('1')

# print number *** BASE 8 notaion of a
p gets.to_i % 8

# print number *** BASE 8 notaion of a
s = gets.to_i.to_s 8
p s.chars.map(&:to_i).reduce(:+) / s.size

#!/usr/bin/env ruby
puts gets.to_i.to_s(8)[-1]


Johny likes numbers n and k very much. Now Johny wants to find the smallest integer x greater than n, so it is divisible by the number k.

The only line contains two integers n and k (1 ≤ n, k ≤ 109).

Print the smallest integer x > n, so it is divisible by the number k.

# cook your code here

a1,b1=gets.chomp.split

n = a1.to_i
k = b1.to_i

n = n + k - n%k

puts n

n, k = gets.strip.split.map(&:to_i)
remainder = n / k
ans = k * remainder + k
puts ans

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

x = (n/k).to_i

puts (x+1)*k

n,k = gets.split(' ').map(&:to_i)
puts ((n/k) * k) + k

n, k = gets.strip.split.map(&:to_i)
mod = n % k
remainder = 0
remainder = n / k if mod != 0
remainder = mod if mod ==
ans = n + remainder
ans += k if remainder == 0
puts ans

n, k = gets.strip.split.map(&:to_i)
mod = n % k
remainder = mod
remainder = n / k if mod != 0
ans = n + remainder
ans += k if remainder == 0
puts ans


One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one!Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture.In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals n, then the player wins, otherwise the player loses.The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals n.

The only line contains n (1 ≤ n ≤ 25) — the required sum of points.

Print the numbers of ways to get the second card in the required way if the first card is the queen of spades.

NoteIn the first sample only four two's of different suits can earn the required sum of points.In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use.In the third sample there is no card, that would add a zero to the current ten points.

n = gets.chomp.to_i - 10
if n < 1 || n > 11
  puts 0
elsif n == 10
  puts 15
else
  puts 4
end

need = STDIN.readline.to_i - 10
if need == 10
  puts 15
elsif need >= 1 and need <= 11
  puts 4
else
  puts 0
end

n = gets.to_i
if n <= 10 or n >= 22
  puts 0
else
  if n== 20
    puts 15
  else
    puts 4
  end
end

#104A
n=gets.to_i
if n <= 10
    puts "0"
elsif n >= 11 and n <= 19
    puts "4"
elsif n == 20
    puts "15"
elsif n == 21
    puts"4"
else
    puts "0"
end

a=gets.chomp.to_i
a=a-10
if a<=0 || a>=12
puts "0"
elsif a==10
puts "15"
else
puts "4"
end

def blackjack
  c = { }
  1.upto(11) { |i| c[i] = 4 }
  c[10] = 15

  goal = gets.to_i
  if goal-10 < 1
    puts 0
  else
    puts (c.has_key?(goal-10)) ? c[goal-10] : 0
  end
end

blackjack

case gets.chomp.to_i
  when 11..19, 21
    puts 4
  when 20
    puts 15
  else
    puts 0
end

# Codeforces Beta Round #80
# Problem A -- Blackjack
cards = [0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 15, 4]
n = gets.to_i - 10
puts (0 <= n and n <= 11) ? cards[n] : 0

#!/usr/bin/env ruby

def solve n
  diff = n - 10
  if 1 < diff and diff < 10
    return 4
  end
  if diff == 1 or diff == 11
    return 4
  end
  if diff == 10
    return 4*3+3
  end
  return 0 # if diff < 0 and 11 < diff
end

#n = STDIN.gets.split.map {|i| i.to_i }

puts solve(STDIN.gets.to_i)

s=gets.to_i
need=s-10

data=[10]*(3*4-1)+[1, 11]
for i in 2..10
	data+=[i]*4
end

puts data.count(need)

def blackjack
  c = { }
  1.upto(11) { |i| c[i] = 4 }
  c[10] = 15

  goal = gets.to_i
  if goal-10 < 1
    puts 0
  else
    puts c[goal-10]
  end
end

blackjack

#!/usr/bin/env ruby

def solve n
  diff = n - 10
  return 0 if diff < 0 and 10 < diff
  if 1 < diff and diff < 10
    return 4
  end
  if diff == 1
    return 4
  end
  if diff >= 10
    return 4*3+3
  end
end

#n = STDIN.gets.split.map {|i| i.to_i }

puts solve(STDIN.gets.to_i)

#!/usr/bin/env ruby

def solve n
  return 0 if n < 11
  diff = n - 10
  if 1 < diff and diff < 10
    return 4
  end
  if diff == 1
    return 4
  end
  if diff >= 10
    return 4*3+3
  end
end

#n = STDIN.gets.split.map {|i| i.to_i }

puts solve(STDIN.gets.to_i)

#!/usr/bin/env ruby

def solve n
  diff = n - 10
  return 0 if diff < 0 and 11 < diff
  if 1 < diff and diff < 10
    return 4
  end
  if diff == 1 or diff == 11
    return 4
  end
  if diff >= 10
    return 4*3+3
  end
end

#n = STDIN.gets.split.map {|i| i.to_i }

puts solve(STDIN.gets.to_i)