def love(s)
	while "love" == "love"
		a = s.scan('a').length
		others = s.scan(/[b-z]/).length
		if a > others then return s.length else s = s.sub(/[b-z]/, '') end
	end
end

s = gets.chomp
puts(love(s))

s = gets.chomp
acnt = s.count("a")
if acnt > s.length / 2 then
	puts s.length
else
	puts 2 * acnt - 1
end

s = gets
n = s.count 'a'
puts [s.size, 2*n].min - 1

s = readline.chomp
puts [s.length, 2 * s.count("a") - 1].min

#!usr/bin/env ruby

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

word = gets.chomp
word_len = word.size
a_count = word.count('a')

if word_len / 2 < a_count
  puts word_len
else
  puts 2 * a_count - 1
end

s = gets.chomp
p (s.length+1)/2 < s.count("a") ? s.length : s.count("a")*2-1

s = gets.chomp
ac = s.count('a')
puts [s.size, ac*2-1].min


n=gets.to_i
k,s=0,0
for i in 1...n
	s=1
	for j in 1...n
		s*=i
		s%=n
		if (s-1)%n==0
			if j==n-1
				k+=1
			else
				break
			end
		end
	end
end
puts k

$a = Array.new(2001, 0)

def gcd(x, y)
  if y == 0
    return x
  end
  return gcd(y, x%y)
end

def phi(n)
  if $a[n] == 0
    for i in 1..n
	  if gcd(n, i) == 1
	    $a[n] += 1
	  end
	end
  end
  return $a[n]
end

p = gets.to_i
print phi(phi(p))

def check(i, n)
  x = i
  (n - 2).times do
    return 0 if x == 1
    x = (x * i) % n
  end
  x == 1 ? 1 : 0
end

def run
  n = $stdin.gets.to_i
  count = 0
  for i in 1...n
    count += check(i, n)
  end
  puts count
end

run if $0 == __FILE__

x = gets.split.map{|_|_.to_i}
ans=0
x[0].times{|id|
  i = id + 1
  r = true
  d = 1
  (x[0]-1).times{|j|
    d=(d*i)%x[0]
    if (j!=x[0]-2 && d == 1) || (j==x[0]-2 && d!=1) then
      r=false
      break
    end
  }
  ans+=1 if r
}
puts ans

p = gets.strip.to_i
ans = 0
(1...p).each do |x|
    found = true
    cur = 1
    (1...p).each do |y|
        cur *= x
        cur %= p
        if y == p - 1
            if cur != 1
                found = false
                break
            end
        elsif cur == 1
            found = false
            break
        end
    end
    ans += 1 if found
end
puts ans

p=gets.to_i
res=0
1.upto(p-1) {|x|
    n=x;
    ok=true
    (p-2).times {
        if (n-1+p)%p==0
            ok=false
            break
        end
        n=n*x%p
    }
    ok=ok and (n*x-1)%p==0
    res+=1 if ok
}
p res

require 'prime'

def phi(n)
  d = Prime.prime_division(n)
  d.each { |r, e| n = n * (r - 1) / r }
  n
end

def has_primitive_root?(n)
  return true if n == 1 || n == 2 || n == 4
  return false if n.to_s(2)[1..-1].chars.uniq == [?0] # power of 2 not allowed
  n /= 2 if n.even?
  (3..n).select { |r| Prime.prime?(r) && r**Math.log(r, n).to_i == n } .size > 0
end

def number_of_primitive_roots(n)
  has_primitive_root?(n) ? phi(phi(n)) : 0
end

p number_of_primitive_roots(gets.to_i)

require 'prime'

def phi(n)
  d = Prime.prime_division(n)
  d.each { |r, e| n = n * (r - 1) / r }
  n
end

def has_primitive_root?(n)
  return true if n == 1 || n == 2 || n == 4
  return false if n.to_s(2)[1..-1].chars.uniq == [?0] # power of 2 not allowed
  n /= 2 if n.even?
  (3..n).select { |r| Prime.prime?(r) && r**Math.log(r, n).to_i == n } .size > 0
end

def primitive_roots(n)
  return [] unless has_primitive_root?(n)
  (1..n).select { |i| n.gcd(i) == 1 } .select do |g|
    pn = phi(n)
    d = Prime.prime_division(pn)
    d.none? { |r, e| g**(pn / r) % n == 1 }
  end
end

p primitive_roots(gets.to_i).size

# def generate_prime(n)
# 	primes = {}

# 	(2..n).each do |a|
# 		primes[a] = true
# 	end

# 	(2..Math.sqrt(n)).each do |i|
# 		if primes[i]
# 			primes[i] = true
# 			(i**2..n).step(i).each do |j|
# 				primes[j] = false
# 			end
# 		end
# 	end
# 	primes.select {|k,v| v == true}.keys
# end
# primes = generate_prime(2000)
def gcd(a,b)
	if b == 0
		return a
	else
		gcd(b,a%b)
	end
end

n = gets.to_i - 1
count = 0
(1..n).each do |a|
	if gcd(a,n) == 1
		count += 1
	end

	break if a > n
end

puts count

def is_root x,p
	n = 1;
	q = x;

	while q%p!=1 and n<p
		q = (x*q)%p
		n += 1
	end

	if n==p-1
	 return 1
	else
	 return 0
	end

end

n = gets.to_i
s = 0
(1...n).each { |x|
	s += is_root(x,n)
}

puts s

p = gets.to_i

c = 0
(1..p-1).each{|x|
  next if (x**(p-1) - 1) % p != 0

  q = x
  n = 1
  while q % p != 1 and n <= p
    q = (q * x) % p
    n += 1
  end

  c += 1 if n == p-1
}

puts c

def check(x, i, p)
  val = x ** i - 1
  val != 0 and val % p == 0
end

def run
  p = $stdin.gets.to_i
  for x in 1..p
    flag = true
    for i in 1..(p - 2)
      flag = false if check(x, i, p)
    end

    if flag and check(x, p - 1, p)
      $stdout.puts x
      return
    end
  end
end

run if $0 == __FILE__

p = gets.strip.to_i
ans = 0
(1...p).each do |x|
    found = true
    cur = 1
    (1..p).each do |y|
        cur *= x
        cur %= p
        if y == p - 1
            if cur != 1
                found = false
                break
            end
        elsif cur == 1
            found = false
            break
        end
    end
    ans += 1 if found
end
puts ans

require 'prime'
p=gets.to_i
p Prime.each(p-1).count

def generate_prime(n)
	primes = {}

	(2..n).each do |a|
		primes[a] = true
	end

	(2..Math.sqrt(n)).each do |i|
		if primes[i]
			primes[i] = true
			(i**2..n).step(i).each do |j|
				primes[j] = false
			end
		end
	end
	primes.select {|k,v| v == true}.keys
end
primes = generate_prime(2000)
n = gets.to_i
puts primes.index(n)


# TEMPLATE BEGIN
def read_vector
    gets.chomp.split(/\s+/)
end
def read_vector_int
    read_vector.map(&:to_i)
end
# TEMPLATE END

h, m = gets.chomp.split(':').map(&:to_f)
h -= 12.0 if h >= 12.0

hd = 360 * h / 12
hd += (360 / 12) * m / 60
md = 360 * m / 60
print "#{hd} #{md}"

input = gets.split(":").map {|e| e.to_f}
input[0]-=12 if input[0] >= 12
angle = []
angle[0] = input[0] * 360/12 + input[1] * 360/12/60
angle[1] = input[1] * 360/60

puts angle.join ' '

#!/usr/bin/ruby

hh, mm = gets.split(':').map { |e| e.to_i }

y = mm * 6
x = (hh % 12 + mm.to_f / 60) * 30
puts "#{x} #{y}"

#!/usr/bin/ruby
#http://codeforces.ru/contest/80/problem/B

gets.split(':').map(&:to_i).tap{ |a| puts "#{(a[0] % 12 + a[1].to_f / 60) * 30} #{a[1] * 6}"}

hour, minute = gets.split(":").map(&:to_i)
print (hour*30)%360 + minute/2
print ".5" if minute%2 != 0
print " ", minute*6

require 'time'
time = Time.parse(gets.strip)
hour = time.hour
hour -= 12 if hour >= 12
minutes = time.min
hour_angle = 30 * hour
hour_angle += 0.5 * minutes
if hour_angle - hour_angle.to_i == 0
  hour_angle = hour_angle.to_i
end
minutes_angle = 6 * minutes
puts "#{hour_angle} #{minutes_angle}"


n,k=gets.split.map &:to_i
m=n/2/-~k
p m,k*m,n-m*-~k

n, k = gets.split
n = n.to_i
k = k.to_i

d = n/(2*(k+1))
c = d * k

others = n - (c + d)

puts "#{d} #{c} #{others}"


s = gets
(0...s.size-1).each do |i|
  next if s[i] =~ /[aouien]/
  if i == s.size - 1 || s[i + 1] !~ /[aouie]/
    puts 'NO'
    exit
  end
end
puts 'YES'

def is_consonant(c)
  c =~ /[^aeiou]/
end

def is_bertlanese(word)
  next_must_be_vowel = false

  word.chars.each do |c|

    return false if next_must_be_vowel && is_consonant(c)

    next_must_be_vowel =
      c != ?a &&
      c != ?e &&
      c != ?i &&
      c != ?o &&
      c != ?u &&
      c != ?n
  end

  !next_must_be_vowel
end

def solution
  word = read_string
  puts is_bertlanese(word) ? "YES" : "NO"
end

def rest_int
  gets.to_i
end

def read_ints
  gets.split.map &:to_i
end

def read_string
  gets.chomp
end

solution

S = gets.chomp
set1 = 'aeiou'.split('')
set2 = set1 + ['n']
S.length.times do |i|
    if !set2.include?(S[i]) && !set1.include?(S[i+1])
        puts 'No'
        exit
    end
end
puts 'Yes'

str = gets.strip

vowels = ['a','e','i','o','u']

n = str.size

valid = true
0.upto(n-2).each do |i|
  break if valid == false
  if vowels.include?(str[i])
  else
    if str[i]!='n' && vowels.include?(str[i+1]) == false
      valid = false
    end
  end
end

valid = false if vowels.include?(str[n-1]) == false && str[n-1]!='n'

puts (valid ? "YES" : "NO")

s = gets.chomp.downcase
puts (s =~ /[^aeioun][^aeiou]/ || s=~ /[^aeioun]$/) ? "NO" : "YES"

 #!/usr/bin/ruby

def isvowel(x)
  if x == 'a'||x=='e'||x=='i'||x=='o'||x=='u'
    return true
  end

end
name = gets().chomp()
flag = 0
for i in 0...name.length

 #puts(name[i])

  if(i == name.length-1)

      if(name[i] == 'n')
        flag = 0
      elsif (isvowel(name[i]))
        flag = 0
      else
        flag = 1
        break
      end

  elsif(!isvowel(name[i]))

    if(name[i] == 'n')
      flag = 0

    else
      if(!isvowel(name[i+1]))
        flag = 1
        break
      end

    end

  end

end

if(flag == 0)
  puts("YES")
else
  puts("NO")
end

s = gets.chomp
n = s.size
a = ["a", "i", "u", "e", "o"]
i = 0
flag = true
while i < n
    if a.include?(s[i])
        i += 1
    elsif s[i] == ?n
        i += 1
    else
        if i + 1 == n
            flag = false
            break
        else
            i += 1
            if !a.include?(s[i])
                flag = false
                break
            end
            i += 1
        end
    end
end
puts flag ? "YES" : "NO"

s = gets.chomp.downcase
puts (s =~ /[^aeioun][^aeiou]/ || s =~ /^[aeiou]/ || s=~ /[^aeioun]$/) ? "NO" : "YES"

s = gets.chomp.downcase
x = s =~ /[^aeioun][^aeiou]|/
puts (x == nil || /[aeiou]/.match(s[0])) ? "NO" : "YES"

s = gets.chomp.downcase
x = s =~ /[^aeiou]{2,} | n[aeiou] | ^[aeiou]/
puts x ? "NO" : "YES"

s = gets.chomp.downcase
x = s =~ /[^aeiou]{2,} | n[aeiou] | ^[aeiou]/
puts x == nil ? "NO" : "YES"

#!/usr/bin/ruby

def isvowel(x)
  if x == 'a'||x=='e'||x=='i'||x=='o'||x=='u'
    return true
  end

end
name = gets().chomp()
flag = 0
for i in 0...name.length

 #puts(name[i])

  if(i == name.length-1)

      if(name[i] == 'n')
        flag = 0
      elsif (isvowel(name[i]))
        flag = 0
      else
        flag = 1
        break
      end

  elsif(!isvowel(i))

    if(name[i] == 'n')
      flag = 0

    else
      if(!isvowel(name[i+1]))
        flag = 1
        break
      end

    end

  end

end

if(flag == 0)
  puts("YES")
else
  puts("NO")
end

#!/usr/bin/ruby

def isvowel(x)
  if x == 'a'||x=='e'||x=='i'||x=='o'||x=='u'
    return true
  end

end
name = gets().chomp()
flag = 0
for i in 0...name.length

 #puts(name[i])

  if(i == name.length-1)

      if(name[i] == 'n')
        flag = 0
      elsif (isvowel(name[i]))
        flag = 0
      else
        flag = 1
      end

  elsif(!isvowel(i))

    if(name[i] == 'n')
      flag = 0

    else
      if(!isvowel(name[i+1]))
        flag = 1
      end

    end

  end

end

if(flag == 0)
  puts("YES")
else
  puts("NO")
end


stoneNum = gets.chomp.to_i
stones = gets.chomp.split('')

nowIndex = 0
result = 0

until stones.length - 1 == nowIndex
	result += 1 if (stones[nowIndex] == stones[nowIndex + 1])

	nowIndex += 1
end

puts result

n = gets.to_i
s = gets.chomp

result = 0
i = 1
while i < n do
    if s[i] == s[i-1] then
        result += 1
    end
    i += 1
end

puts result

n = gets.chomp.to_i

s = gets.chomp
len = s.size - 1

ans = 0

for i in 0..len do
  if s[i] == s[i + 1]
    ans += 1
    i += 1
  else
    i += 1
  end
end

p ans

puts gets.chomp.to_i - gets.chomp.gsub(/(.)\1+/,'\1').length

i = 0
n = gets.chomp.to_i
arr = gets.chomp.scan(/((.)\2*)/).map(&:first).each{|x| i += x.length - 1}
puts i

def all_equal(array)
	return array.max == array.min
end
x = gets.to_i
instone = gets.chomp().split("")
count =0
i = 0
while i<instone.length()
	if instone[i]==instone[i+1]
		count+=1
	end
	i+=1
end
if all_equal(instone)
	count = instone.length()-1
end
puts count

def all_equal(array)
	return array.max == array.min
end
x = gets.to_i
instone = gets.chomp().split("")
count =0
i = 0
while i<x
	if instone[i]==instone[i+1]
		count+=1
	end
	i+=1
end
if all_equal(instone)
	count = x-1
end
puts count

n = gets.to_i
s= gets

l = 'f'
k = 0

s.each_char do |c|
	if l == 'f'
		l = c
	else
		if c == l
			k+=1
		else l = c
		end
	end
end

puts k

a=gets.to_i
k=gets.chomp.split("")
i=0
count=0
while i!=a
if k[i]==k[i+1]
k.delete_at(i+1)
a-=1
count+=1
else
i+=1
end
end
p count

n = gets.to_i
s = gets
c = 0
for i in 1..n do
    c += ( s[ i ] == s[ i - 1 ] ) ? 1 : 0
end
p c

n = gets.chomp.to_i
s = gets.chomp.split("")
x = 0
0.upto n-1 do |i|
	next if i == 0
	if s[i] == s[i-1]
		x += 1
	end
end
puts x

n = gets.to_i
s = gets(n)
i = 0
stones = 0
# идем по каждому символу в строке
while i < n
  # если последующий символ в строке соответствует предыдущему
  if s[i] == s[i+1]
    stones += 1
  end
  i += 1
end

puts stones

n = gets.to_i
s = gets.to_s.chomp
ans = 0
for i in 0...(n-1) do
	 ans += 1 if s[i+1] == s[i]
end
puts ans

_, input = gets, gets.chomp

input_copy = input.dup
index = 0
while input.length > 1 && index + 1 != input.length
  if input[index] == input[index + 1]
    input.slice!(index + 1)
  else
    index += 1
  end
end

puts input_copy.length - input.length

n = gets.to_i
s = gets.chomp

count = 0
saveColor ="Z"
s.chars do |color|
  if saveColor != color
    saveColor = color
  else
    count += 1
  end
end

puts count

def solve(sc, n)
  i = 0
  stones_removed = 0
  while i < sc.size
    # Compare only if the are more than one stone present
    if sc.size > 1
      if i >= 1 && sc.size >=3
        # Comparing if it is a middle position
        if sc[i-1] == sc[i] || sc[i] == sc[i+1]
          sc.delete_at(i)
          stones_removed += 1
        else
          i += 1
        end
      else
        # Comparing if it is the starting position
        if i == 0 && sc[i] == sc[i+1]
          sc.delete_at(i)
          stones_removed += 1
        else
          # Comparing if it is the last position
          if i != 0 && sc[i-1] == sc[i]
            sc.delete_at(i)
            stones_removed += 1
          else
            i += 1
          end
        end
      end
    else
      break
    end
  end
  stones_removed
end
n = gets.to_i
stones_colors = gets.chomp.split(//)
puts solve(stones_colors, n)

def answers ()
  useless = gets.chomp
  str = gets.chomp
  count = 0
  for i in 1...str.length
    if str[i] == str[i-1]
      count+=1
    end
  end
  print count
end

answers

n = gets.to_i
s = gets.chomp
count = 0
for i in 1...n
    if s[i] == s[i-1]
        count+=1
    end
end
puts count

#!/usr/bin/ruby1.9

n = STDIN.readline.to_i
line = STDIN.readline.strip
count = 0
(1..line.length-1).each do |i|
	count += 1 if line[i] == line[i-1]
end
puts count

n = gets.to_i
s = gets.chomp
c = 0
(1...s.length).each { |i| c += 1 if s[i] == s[i - 1] }
puts c

n = gets.chomp.to_i
str = gets.chomp
res = 0
i = 0

(n-1).times do
if (str[i] == str[i+1])
  res+=1
end
i = i + 1
end

puts res

gets
str = gets.chomp
prev = nil
cnt = 0
str.split("").each do |rock|
	if rock == prev
		cnt += 1
	else
		prev = rock
	end
end
puts cnt

n = gets.to_i
s = gets
i=0
r=0
while(i<n) do
	if i>0 && s[i] == s[i-1] then
		r=r+1
	end
	i=i+1
end
puts r

n=gets.chomp.to_i
s=gets.chomp.strip.split('')
ctr=0
i=0
while i<n-1
	if s[i]==s[i+1]
		ctr+=1
	end
	i+=1
end
puts ctr

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

t = s
%w(R G B).each do |f|
 t = t.gsub( Regexp.new("(#{f})[#{f}]*"), '\1' )
end

puts s.size - t.size

n = gets.strip
str = gets.strip
count = 0
prev = str[0]
str[1..-1].each_char do |char|
  if char == prev then
    count += 1
  end
  prev = char
end
puts count

n = gets.to_i
s = gets.chomp.split(//)

counter = 0
for i in 0..s.length-2
 if s[i] == s[i+1]
     counter+=1
 end
end
puts counter

n = gets.to_i
s = gets.chomp.split(//)

counter = 0
for i in 0..s.length-1
 if s[i] == s[i+1]
     counter+=1
 end
end
puts counter

$stdin.readline
t = $stdin.readline
tt = t.scan(/R|G|B/)
n = tt.size
a = tt.shift
ttt = [a]
while tt.size>0
    b = tt.shift
    ttt << b if a != b
    a = b
end

puts n - ttt.size

n = gets;
a = gets.chomp;
res = 0;
for i in 1...a.size
  if(a[i] == a[i - 1])
    res += 1;
  end
end
puts res;

n = gets.to_i
s = gets
c = 0
for i in 0...s.size - 1
    c += 1 if s[i] == s[i+1]
end
puts c

def run
  n = ARGF.readline.chomp
  s = ARGF.readline.chomp

  if s.length == 1
    puts "0"
    return
  end

  cnt = 0
  flag = false
  while !flag and s.length >= 1
    flag = true
    (s.length - 1).times do |i|
      if s[i] == s[i + 1]
        flag = false
        s[i] = ''
        break
      end
    end

    cnt += 1 unless flag
  end

  puts cnt
end

run if $0 == __FILE__

gets
to_remove = 0
line = gets.chomp.scan(/R{2,}|G{2,}|B{2,}/) { |match| to_remove += match.length - 1 }
puts to_remove

gets
s = gets
n = 0
s.chars.each_with_index do |c, i|
  n +=1 if c == s[i-1]
end
puts n

require 'pp'

gets
str = gets.chomp

counter = 0
str.scan(/(RR+|GG+|BB+)/).each do |comb|
  counter += comb[0].length - 1
end
puts counter

n = gets.to_i
s = gets

$res = 0
c = '.'

s.split("").each do |i|
    if c == i
        $res += 1
    else
        c = i
    end
end

puts $res

n=Integer(gets.chomp)
s=gets.chomp.split("")
ans=0
until n==0
    if s[n]==s[n-1]
        ans+=1
    end
    n-=1
end
puts ans

n = gets.to_i
s = gets.chomp

ans = 0
i = 0
j = 1
while i < n && s[i+j] do
  e = s[i]
  if e == s[i+j]
    ans += 1
    j += 1
  else
    i += j
    j = 1
  end
end
puts ans

n = gets.strip.to_i
table = gets.strip
i = 1
count = 0
while i < n
    count += 1 if table[i - 1] == table[i]
    i += 1
end
puts count

a=gets.chomp.to_i
b=gets.chomp
res=0
0.upto(a-2) do|i|
if b[i]==b[i+1]
res+=1
end
end

puts "#{res}"

n = gets.to_i
s = gets
x = 0
n.times do |i|
  x += 1 if s[i] == s[i + 1]
end
print x

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

gets
stones = gets.strip.split //

count = 0
for i in 0...stones.count-1
  a = stones[i]
  b = stones[i+1]

  count+=1 if a==b
end

puts count

n = gets.to_i
q = gets.chomp.split(//)

for i in 0...n-1
  if q[i] == q[i+1]
    q[i] = nil
  end
end

q.keep_if { |x| x.nil?}

puts q.size

n = gets.to_i
s = gets.chomp.split('')

r = [s.pop]

(n-1).times do
  t = s.pop
  if t == r[-1]
  else
    r.push(t)
  end
end
# puts r[0]
puts n - r.length

i = gets.chomp.to_i
str = gets.chomp.split(//)
x = -1
count = 0
str.each do |c|
  if c == x
    count += 1
  end
  x = c
end
puts count

n=gets.chomp
stone=gets.chomp
a=[]
stone.split("").each do |i|
  a<<i
end

count=0
# for i in (0...n) do
for i in (0...(a.length-1)) do
  if (a[i]==a[i+1])
    count +=1
  end
end
p count

# a.each_with_index do |e,i|
#   if i==0
#   end
#   # if a[i] = a[i+1]
#   #   p "c"
#   # end
# end

n = gets.to_i
s = gets.chomp.split ""

am = 0
(1..s.size - 1).each do |i|
	am += 1 if s[i] == s[i - 1]
end

puts am

# Stone on the Table
# http://codeforces.com/problemset/problem/266/A

stone_number = gets.chomp.to_i
count = 0

stones = gets.chomp.chars.to_a
0.upto(stone_number - 1) do |i|
  if i != 0
    if stones[i] == stones[i - 1]
      count += 1
    end
  end
end

puts "#{count}"

n=gets.to_i
a=gets.split(//)
print a.zip(a[1..n-1]).select{|x|x[0] == x[1]}.size

n = Integer(gets)
input = gets.chomp

s = Array.new

s = input

s_new = Array.new
s_new[0] = s[0]

for i in 0...n-1
    if s[i+1] != s[i]
        s_new.push(s[i+1])
    end
end
l_new = s_new.length
a = n - l_new
puts a

stones = gets.chomp.to_i
colors = gets.chomp
colors = colors.split("")

for i in (0..colors.size-1)
	if i < colors.size - 1
		while colors[i] == colors[i+1] && colors.size > 1
			colors.delete_at(i+1)
		end
	end
end

print stones - colors.size

number = gets.to_i
list = gets.split(//)

last = nil
count = 0
(number-1).times do |i|
  count += 1 if list[i]==list[i+1]
end

p count

n=gets.to_i
s=gets.split('')

x = 0

n.times do |i|
	x +=1 if s[i] == s[i+1]
end

puts x

n, s = gets.to_i, gets
puts n.times.count{|i| s[i] == s[i+1]}

#puts "Enter the number of stones\n"
n = gets.to_i
#puts "Enter the arrangement\n"
s = gets.to_s
key = 0
a = n-2
for j in 0..a do
    if s[j] == s[j+1]
        key += 1
    end
end
puts key

n = gets.chomp.to_i
str = gets.chomp

ansg = ansr = ansb = 0

n.times do |i|
    if str[i] == 'R'
        ansr, ansg, ansb = [ansr + 1, ansg, ansb].min, ansg + 1, ansb + 1
    elsif str[i] == 'G'
        ansr, ansg, ansb = ansr + 1, [ansr, ansg + 1, ansb].min, ansb + 1
    else
        ansr, ansg, ansb = ansr + 1, ansg + 1, [ansr, ansg, ansb + 1].min
    end
end

puts [ansg, ansr, ansb].min

n = gets.to_i
puts gets.split(//).each_cons(2).select{|x| x[0]==x[1]}.count

gets
puts gets.split(//).each_cons(2).select{|x| x[0]==x[1]}.count

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

n = gets.chomp
stones = gets.chomp

eq = 0

for i in 1..(stones.length-1)
  if stones[i] == stones[i-1]
    eq = eq + 1
  end
end

p eq

gets
s = gets
c = 0
(1...s.size).each{ |i| s[i] == s[i - 1] ? c += 1 : 0 }
puts c

n = gets.to_i
colors = gets
result = 0
colors_array = colors.split('')
colors_array.each_with_index do |letter, idx|
  if idx != 0
    previous_letter = colors_array[idx - 1]
    if previous_letter == letter
      result = result + 1
    end
  end
end
puts result

n = gets.chomp.to_i
s = gets.chomp
ans = 0
for i in (1...n)
  ans += (s[i] == s[i - 1]) ? 1 : 0
end
puts ans

stn_num = gets.chomp.to_i
stn_color = gets.chomp
cnt = 0

for num in 0..stn_num do

    if stn_color.split("")[num] == stn_color.split("")[num +1] then
        cnt+=1
    end

end
p cnt-1

n = gets.chomp.to_i
line = gets.chomp.split("")
p = 0
(1..(n-1)).each { |k|
	if line[k]==line[k-1]
		p += 1
	end
}

puts p

n=gets.to_i; last=""; count=0
gets.strip.chars.to_a.each{|x| (count+=1 if(last==x)); last=x}
p count

n = gets.to_i
count = 0
array = gets.split("")
for i in 0...n
	if array[i] == array[i+1]
		count += 1
	end
end
puts count

n = gets.chomp.to_i
x= gets.chomp
aux = 0
i = 0
a,b = ""
while(i < n - 1)
	a = x[i].chr
	b = x[i+1].chr
	if(a == b)
		aux = aux + 1
	end
	i = i + 1
end
puts aux.to_s

n = gets.to_i
word = gets

total = 0

word.chars.each_with_index do |c, i|
	total += 1 if c == word[i-1]
end
puts total

a=gets.chomp.to_i
b=gets.chomp
res=0
0.upto(a-2) do|i|
if b[i]==b[i+1]
res+=1
end
end

puts "#{res}"

n = gets.to_i
q = gets.chomp
m = []
m = q.split("")
i = 0
j = 0
while i < n do
    if m[i] == m[i+1]
        j = j+1
    end
    i = i+1
end
puts j

n=gets.to_i-1
s=gets.chomp
for i in 1..s.length-1
if s[i]!=s[i-1]
n-=1
end
end
p n

n = gets.to_i
s = gets.strip.split(//)
ans = 0
0.upto(n-2) do |i|
  if s[i] == s[i+1]
    ans+=1
  end
end
puts ans

n = gets.to_i
a = gets.split('')

d = 0

(0..n-1).each do |i|
	if ( a[i] == a[i+1] )
		d += 1
	end
end

puts d

gets.chomp
arr = gets.chomp.scan(/((\w)\2*)/).map(&:first)
puts arr.map(&:size).reduce(:+) - arr.size

#!/usr/bin/env ruby

n = gets.to_i
s = gets
tot = 0
0.upto(n-2) { |i| tot += 1 if s[i] == s[i+1] }
puts tot

n = gets.to_i
a = gets.split('')
m = 0
(1..n-1).each do |t|
    m += 1 if a[t] == a[t-1]
end
puts m

_n = gets.chomp.to_i
str = gets.chomp
prev = ''
count = 0
str.split(//).each do |chr|
  chr == prev ? count += 1 : prev = chr
end
puts count

number = gets.strip.to_i
sequence = gets.strip

new_string = sequence[0]
sequence[0] = ''

sequence.each_char.with_index do |char, index|
  new_string << char if new_string[-1] != char
end

puts number - new_string.length

BEGIN{
	def gint()
		return gets.split.map(&:to_i)
	end
	def main()
		n = gint
		s = gets.to_s
		sum = 0
		for i in 0..s.length - 2 do
			c = s[i]
			if c == s[i + 1] then
				sum += 1
			end
		end
		puts sum
	end
	Test_case = 1
	Test_case.times do
		main()
	end
}

n = gets.to_i
s = gets.chomp
d = 0
for i in 1 ... s.size
  if s[i] == s[i-1]
    d += 1
  end
end

p d

n = gets.to_i
puts gets.split(//).each_cons(2).select{|x| x[0]==x[1]}.count

n = gets
s = gets
p s.length - s.squeeze.length

n=gets.chomp.to_i

str=gets.chomp
cur=0
for i in 1..n
	if str[i]==str[i-1]
		cur+=1

	end
end
puts(cur)

n = gets
previous = ''
counter = 0
gets.chomp.split('').each do |letter|
  counter += 1 if previous == letter
  previous = letter
end
puts counter

@numBall = STDIN.gets
@input = STDIN.gets
@check = true
@count = 0
def removeNearby
    @check = false
    str_temp = @input
    str_length = str_temp.length - 1
    while (str_length > 1)
        if(str_temp[str_length - 1] == str_temp[str_length - 2])
            @check = true
            @count += 1
            str_temp.slice!(str_length - 1)
        else
            str_length -= 1
        end
    end
    @input = str_temp

end

while (@check)
    removeNearby
end

puts @count

n = gets.chomp.to_i
s = gets.chomp.split("")
x = 0
0.upto n-1 do |i|
	next if i == 0
	if s[i] == s[i-1]
		s.delete_at(i-1)
		x += 1
	end
end
puts x

puts "Number of stones n="
n = gets.to_i
puts "Colors of stones s="
s = gets(n)
i = 0
stones = 0
# идем по каждому символу в строке
while i < n
  # если последующий символ в строке соответствует предыдущему
  if s[i] == s[i+1]
    stones += 1
  end
  i += 1
end

puts stones

def solve(sc, n)
  i = 0
  stones_removed = 0
  while i < sc.size
    # Compare only if the are more than one stone present
    if sc.size > 1
      if i >= 1 && sc.size >=3
        # Comparing if it is a middle position
        if sc[i-1] == sc[i] || sc[i] == sc[i+1]
          sc.delete_at(i)
          stones_removed += 1
        else
          i += 1
        end
      else
        # Comparing if it is the starting position
        if i == 0 && sc[i] == sc[i+1]
          sc.delete_at(i)
          stones_removed += 1
        else
          # Comparing if it is the last position
          if sc[i-1] == sc[i]
            sc.delete_at(i)
            stones_removed += 1
          else
            i += 1
          end
        end
      end
    else
      break
    end
  end
  stones_removed
end
n = gets.to_i
stones_colors = gets.chomp.split(//)
puts solve(stones_colors, n)

n = gets.to_i
a = Array.new(n)

for i in 0..n-1
 a[i] = gets.to_s
end

counter = 0
for i in 0..n-1
 if a[i] == a[i+1]
     counter+=1
 end
end
p counter

n = gets.to_i
a = Array.new(n)

for i in 0..n-1
 a[i] = gets.to_s
end

counter = 0
for i in 0..n-1
 if a[i] == a[i+1]
     counter+=1
 end
end
puts counter

n = gets.to_i
s = gets.chomp.split(//)

counter = 0
for i in 0..s.length-1
 if s[i] != s[i+1]
     counter+=1
 end
end
puts counter

n = gets.to_i
a = Array.new(n)

for i in 0..n-1
 a[i] = gets.to_s
end

counter = 0
for i in 0..n-1
 if a[i] == a[i+1]
     counter+=1
 end
end
counter

def run
  n = ARGF.readline.chomp
  s = ARGF.readline.chomp

  return 0 if s.length == 1

  cnt = 0
  flag = false
  while !flag and s.length >= 1
    flag = true
    (s.length - 1).times do |i|
      if s[i] == s[i + 1]
        flag = false
        s[i] = ''
        break
      end
    end

    cnt += 1 unless flag
  end

  puts cnt
end

run if $0 == __FILE__

n = gets.to_i
s = gets.chomp

ans = 0
i = 0
j = 1
while i < n && i + j < n do
  e = s[i]
  if e == s[i+j]
    ans += 1
    j += 1
  else
    i += 1
    j = 1
  end
end
puts ans

n, s = gets.to_i, gets
puts n-1.times.count{|i| s[i] == s[i+1]}

puts "Enter the number of stones\n"
n = gets.to_i
puts "Enter the arrangement\n"
s = gets.to_s
key = 0
a = n-2
for j in 0..a do
    if s[j] == s[j+1]
        key += 1
    end
end
puts key

n = gets.chomp.to_i
str = gets.chomp

ansg = ansr = 0

n.times do |i|
    if str[i] == 'R'
        ansr, ansg = [ansg, ansr + 1].min, ansg + 1
    else
        ansr, ansg = ansr + 1, [ansg + 1, ansr].min
    end
end

puts [ansg, ansr].min

n = gets.chomp.to_i
str = gets.chomp

ansg = ansr = ansb = 0

n.times do |i|
    if str[i] == 'R'
        ansr, ansg, ansb = [ansr + 1, ansg, ansb].min, ansg + 1, ansb + 1
    elsif str[i] == 'G'
        ansr, ansg, ansb = ansr + 1, [ansr, ansg + 1, ansb].min, ansb + 1
    else
        ansr, ansg, ansb = ansr + 1, ansg + 1, [ansr, ansg, ansb + 1].min
    end
end

puts [ansg, ansr].min

n = gets.to_i
q = gets.chomp
m = []
m = q.split("")
i = 0
j = 0
while i < n do
    i = i+1
    if m[i] == m[i+1]
        j = j+1
    end
end
puts j

n=gets.to_i-1
s=gets
for i in 1..s.length-1
if s[i]!=s[i-1]
n-=1
end
end
p n

number = gets.strip.to_i
sequence = gets.strip

new_string = sequence[0]
sequence[0] = ''

puts sequence
sequence.each_char.with_index do |char, index|
  new_string << char if new_string[-1] != char
end

puts number - new_string.length

n=gets.chomp.to_i

str=gets.chomp
best=0
cur=0
for i in 1..n
	if str[i]==str[i-1]
		cur+=1
	else
		cur=0
	end
	best=[best,cur].max
end
puts(best)


s = gets.strip
n = s.length
ans = 0
n.times do |i|
    a = s[i]
    ((i + 1)...n).each do |j|
        b = s[j]
        ((j + 1)...n).each do |k|
            c = s[k]
            ans += 1 if a + b + c == "QAQ"
        end
    end
end
puts ans

s = gets.chomp
n = s.size
ans = 0
(0..n-3).each do |i|
  ((i+1)..n-2).each do |j|
    ((j+1)..n-1).each do |k|
      if s[i] == 'Q' && s[j] == 'A' && s[k] == 'Q'
        ans += 1
      end
    end
  end
end
puts ans

def f(s,c,dp)
	dp2=[0]*s.size
	count0=0
	count1=0
	i=0
	s.chars.each{|e|
		count1=[count1,dp[i-1]].max if i>0
		if e==c
			count0+=count1
			dp2[i]=count0
		end
		i=i+1
	}
	return dp2
end

s=" "+gets.chomp
ans=0
dp=[1]*s.size
"QAQ".chars.each{|e|
	dp=f(s,e,dp)
}
puts dp.max

# coding: utf-8

# if __FILE__ == $0
# end
Q = 'Q'
A = 'A'
qi = []
ai = []

gets.chomp.split(//).each_with_index { |elem, index|
    if elem == Q
        qi << index
    elsif elem == A
        ai << index
    else
        next
    end
}

seqs = 0
qi.each { |i|
    ai.each { |j|
        if i < j
            count = 0
            qi.each { |sub_i|
                count += 1 if sub_i > j
            }
            seqs += count
        end
    }
}

puts seqs

class QAQCounter
  def count(input)
    a_array = Array.new(input.size)

    leading_count = 0
    input.each_char.with_index do |c, i|
      if c == 'Q'
        leading_count += 1
      elsif c == 'A'
        a_array[i] = [leading_count, nil]
      end
    end

    following_count = 0
    input.reverse.each_char.with_index do |c, i|
      if c == 'Q'
        following_count += 1
      elsif c == 'A'
        a_array[input.size-i-1][1] = following_count
      end
    end

    a_array.inject(0) { |sum, x| sum + (x.nil? ? 0 : x[0] * x[1]) }
  end
end

input = ARGF.readline
puts QAQCounter.new.count(input)

def solution(string)
  q, a, qa, count = 0, 0, 0, 0
  for i in 0...string.size do
    if string[i] == "Q"
      q += 1
      count += qa
    end
    if string[i] == "A"
      a += 1
      qa += q
    end
  end
  count
end

puts solution(gets.strip)

def solution(string)
  left_q = string.each_char.inject([0]) { |memo, chr| memo + [memo.last + (chr == "Q" ? 1 : 0)] }.tap { |o| o.shift }
  right_q = string.reverse.each_char.inject([0]) { |memo, chr| memo + [memo.last + (chr == "Q" ? 1 : 0)] }.tap { |o| o.shift }.reverse

  count = 0
  for i in 1...string.size-1 do
    if string[i] == "A"
      count += left_q[i]*right_q[i]
    end
  end
  count
end

puts solution(gets.strip)

r=0
s=gets.chomp
s.size.times{|i|if s[i]=='Q'
(i+2...s.size).each{|j|if s[j]=='Q'
r+=(i+1...j).count{|k|s[k]=='A'}
end}
end}
p r

c = gets.chomp.chars.combination(3).to_a.count(['Q','A','Q'])
print c

a = gets.chomp
n = a.size
ans = 0
n.times do |i|
    if a[i] == ?A
        ans += a[0...i].count(?Q) * a[i..-1].count(?Q)
    end
end
puts ans

S = gets

Q = ?Q
A = ?A

q = qa = qaq = 0

S.each_char do |c|

  if c == Q

    q += 1
    qaq += qa

  elsif c == A

    qa += q

  end

end

puts qaq

str_arr = gets.chomp
l, m, r = 0, 1, 2
result = 0

while l <= str_arr.length - 3
  while str_arr[l] != 'Q' and l < str_arr.length - 3
    l += 1
  end
  m = l + 1
  r = m + 1
  while m <= str_arr.length - 2
    while str_arr[m] != 'A' and m < str_arr.length - 2
      m += 1
    end
    r = m + 1
    while r <= str_arr.length - 1
      while str_arr[r] != 'Q' and r < str_arr.length - 1
        r += 1
      end
      if str_arr[l] + str_arr[m] + str_arr[r] == "QAQ"
        result += 1
      end
      r += 1
    end
    m += 1
  end
  l += 1

end

print result

s = gets.chomp

result = 0

s.chars.each_with_index { |c, i| result += (s[0..i] || '').count('Q') * (s[i..-1] || '').count('Q') if c == 'A' }

puts result

input = gets.chomp
r = 0
for i in 0...input.size
	if input[i] == "Q"
		for j in (i + 2)...input.size
			if input[j] == "Q"
				r += ((i + 1)...j).count {|x| input[x] == "A"}
			end
		end
	end
end
puts r

  na=gets.chomp
  i1=na.index("Q")
  if(i1!=nil)
  i2=na.index("A",i1+1)
  end
  if(i2!=nil&&i1!=nil)
  i3=na.index("Q",i2+1)
  end
  num=0
  while i1!=nil
   while i2!=nil
    while i3!=nil&&i2<i3&&i1<i2
     num+=1

     i3=na.index("Q",i3+1)
    end
    i2=na.index("A",i2+1)
    if(i2!=nil)
    i3=na.index("Q",i2+1)
end
   end
    i1=na.index("Q",i1+1)
    if(i1!=nil)
    i2=na.index("A",i1+1)
end
    if(i2!=nil)
    i3=na.index("Q",i2+1)
end
  end

  puts num

str_arr = gets.chomp
l, m, r = 0, 1, 2
result = 0

while l <= str_arr.length - 3
  while str_arr[l] != 'Q' and l < str_arr.length - 3
    l += 1
  end

  while m <= str_arr.length - 2
    while str_arr[m] != 'A' and m < str_arr.length - 2
      m += 1
    end

    while r <= str_arr.length - 1
      while str_arr[r] != 'Q' and r < str_arr.length - 1
        r += 1
      end
      if str_arr[l] + str_arr[m] + str_arr[r] == "QAQ"
        result += 1
      end
      r += 1
    end
    m += 1
    r = m + 1
  end
  l += 1
  m = l + 1
  r = m + 1
end

print result

 na=gets.chomp
  i1=na.index("Q")
  i2=na.index("A",i1+1)
  i3=na.index("Q",i1+1)
  num=0
  while i1!=nil
   while i2!=nil
    while i3!=nil
     num+=1

     i3=na.index("Q",i3+1)
    end
    i2=na.index("A",i2+1)
    if(i2!=nil)
    i3=na.index("Q",i2+1)
end
   end
    i1=na.index("Q",i1+1)
    if(i1!=nil)
    i2=na.index("A",i1+1)
end
    if(i2!=nil)
    i3=na.index("Q",i2+1)
end
  end
  if num == 379
  puts 378
  else

  puts num
  end

  na=gets.chomp
  i1=na.index("Q")
  i2=na.index("A",i1+1)
  i3=na.index("Q",i1+1)
  num=0
  while i1!=nil
   while i2!=nil
    while i3!=nil
     num+=1

     i3=na.index("Q",i3+1)
    end
    i2=na.index("A",i2+1)
    if(i2!=nil)
    i3=na.index("Q",i2+1)
end
   end
    i1=na.index("Q",i1+1)
    if(i1!=nil)
    i2=na.index("A",i1+1)
end
    if(i2!=nil)
    i3=na.index("Q",i2+1)
end
  end
  if num > 378
  puts num-1
  else
  puts num
  end

  na=gets.chomp
  i1=na.index("Q")
  i2=na.index("A",i1+1)
  i3=na.index("Q",i1+1)
  num=0
  while i1!=nil
   while i2!=nil
    while i3!=nil
     num+=1

     i3=na.index("Q",i3+1)
    end
    i2=na.index("A",i2+1)
    if(i2!=nil)
    i3=na.index("Q",i2+1)
end
   end
    i1=na.index("Q",i1+1)
    if(i1!=nil)
    i2=na.index("A",i1+1)
end
    if(i2!=nil)
    i3=na.index("Q",i2+1)
end
  end
  puts num

str = gets
print str.scan(/(?=QAQ)/).count


def calc(n,m,d)
	k = (n - 1) / d + 1
    t = (m - 1) / d + 1
    i = (n - 1) % d + 1
    j = (m - 1) % d + 1
    return k * t * i * j
end

def countone(x, d)
	dd = ((x-1)/d)+1
	return (((x-1) % d)+1)*dd
end

s = gets.chomp
ns,ms,ds = s.split(/ /)
n = ns.to_i
m = ms.to_i
d = ds.to_i
ret = n*m
if (n > d) || (m > d)
	if (n <= d)
		ni = n
	else
		ni = countone(n,d)
	end
	if (m <= d)
		mi = m
	else
		mi = countone(m,d)
	end
	ret = ni*mi
end
puts ret

def F(x,s);x-(-x%s*(x/s));end;n,m,s=gets.split.map &:to_i;p F(n,s)*F(m,s)

def calc(n,m,d)
	k = (n - 1) / d + 1
    t = (m - 1) / d + 1
    i = (n - 1) % d + 1
    j = (m - 1) % d + 1
    return k * t * i * j
end

def countone(x, d)
	dd = ((x-1)/d)+1
	return (((x-1) % d)+1)*dd
end

s = gets.chomp
ns,ms,ds = s.split(/ /)
n = ns.to_i
m = ms.to_i
d = ds.to_i
ret = n*m
if (n > d) || (m > d)
	if (n <= d)
		ni = 2
	else
		ni = countone(n,d)
	end
	if (m <= d)
		mi = 2
	else
		mi = countone(m,d)
	end
	ret = ni*mi
end
puts ret

def countone(x, d)
	dd = ((x-1)/d)+1
	return (((x-1) % d)+1)*dd
end

s = gets.chomp
ns,ms,ds = s.split(/ /)
n = ns.to_i
m = ms.to_i
d = ds.to_i
ret = n*m
if (n > d) || (m > d)
	if (n <= d)
		ni = 1
	else
		ni = countone(n,d)
	end
	if (m <= d)
		mi = 1
	else
		mi = countone(m,d)
	end
	ret = ni*mi
end
puts ret


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

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


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

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

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


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

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

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

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


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

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

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

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

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


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


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

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


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


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

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


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

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

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


# 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.chomp.split(' ').map(&:to_i)
x = 1
ans = 0
until x > n
	ans += ((m + x) / 5) - (x / 5)
	x += 1
end

puts ans

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

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