n, x = gets.split.map(&:to_i)
a = n.times.map{gets.split.map(&:to_i)}
ans = 0
a.each{|x, y| ans += y + 1 - x}
j = 1
n.times do |i|
  ans += (a[i][0] - j) % x
  j = a[i][1] + 1
end
puts ans

n, x = gets.split.map(&:to_i)
t = 1
q = 0
n.times do
  l, r = gets.split.map(&:to_i)
  t += x while t + x <= l
  q += r - t + 1
  t = r + 1
end
puts q

#!/usr/bin/env ruby
n,x=gets.split.map(&:to_i)
cur=1
ret=0
n.times{
	l,r=gets.split.map(&:to_i)
	ret+=r+1-l+(l-cur)%x
	cur=r+1
}
p ret

class A
  def initialize(n, x)
    @n = n
    @x = x
    @fields = Array.new(10**5) { 0 }
  end

  def setup(movements)
    movements.each do |l, r|
      (l..r).each { |idx| @fields[idx - 1] = 1 }
    end
  end

  def minimum_number_of_minutes
    idx = 0
    minutes = []

    while (idx <= @fields.length)
      @fields[idx..(idx + @x - 1)].tap do |arr|
        if arr.any? { |a| a == 1 }
          minutes << arr.rindex(1) + 1
          idx += minutes.last
        else
          minutes << 0
          idx += @x
        end
      end
    end

    minutes.inject(0, :+)
  end
end

n, x = gets.chomp.split().map(&:to_i)
movements = [].tap do |arr|
  n.times { arr << gets.chomp.split().map(&:to_i) }
end

a = A.new(n, x)
a.setup(movements)
puts a.minimum_number_of_minutes

n,x=gets.chomp.split.map(&:to_i)
lol,i=0,1
n.times do
	l,r=gets.chomp.split.map(&:to_i)
	lol+= r-l + (l-i)%x + 1
	i=r+1
end
puts lol

n, x = gets.chomp.split(/ /).map(&:to_i)
watch = 0
skip = 1
n.times do
    l, r = gets.chomp.split(/ /).map(&:to_i)
    skip += x while skip + x <= l
    if skip <= l
        while skip <= r
            watch += 1
            skip += 1
        end
    end
end
puts watch

n,x=gets.split.map{|e| e.to_i}
t=1
ans=0
n.times{
	l,r=gets.split.map{|e| e.to_i}
	while t+x<=l
		t+=x
	end
	ans+=r-t+1
	t=r+1
}
puts ans

#!/usr/bin/ruby
tokens = STDIN.gets.chomp.split(" ").map(&:to_i)
n = tokens[0]
x = tokens[1]
minute = 1
ans = 0
n.times do
    l, r = STDIN.gets.chomp.split(" ").map(&:to_i)
    while minute + x <= l do
        minute += x
    end
    ans += r - minute + 1
    minute = r + 1
end

puts ans

n,x = gets.split(" ").map(&:to_i)
minutes_watched = 0
current = 1
n.times do
	l,r = gets.split(" ").map(&:to_i)
	while current + x <= l
		current += x
	end
	minutes_watched += l - current
	current = r + 1
	minutes_watched += r + 1 - l
	# puts "current #{current} minutes_watched #{minutes_watched}"
end

puts minutes_watched

n, x = gets.chomp.split(' ').map(&:to_i)
c = 1
total = 0
n.times do |i|
  l, r = gets.chomp.split(' ').map(&:to_i)
  while c + x <= l do c += x end
  total += r-l + 1
  total += l - c
  c = r + 1
end
puts total

n,x = gets.split.map &:to_i
puts n.times.map{gets.split.map &:to_i}.unshift([0,0]).each_cons(2).map{|p,n| (n[0]-p[1]-1)%x+n[1]-n[0]+1}.inject(:+)

n, x = gets.split.map(&:to_i)
a = n.times.map{gets.split.map(&:to_i)}
ans = 0
j = 1
a.each do |s, e|
  ans += (s - j) % x
  ans += e - s + 1
  j = e + 1
end
puts ans

read=lambda{ gets.split(" ").map{|x| x.to_i} }
n,x=read.call
#puts "#{n} #{x}"
ans=0
now=1
for _ in 0...n
  l,r=read.call
  ans+=(l-now)%x+r-l+1
  now=r+1
end
puts ans

n, x = gets.split(' ').map(&:to_i)
pre = 0
ans = 0
n.times do
    l, r = gets.split(' ').map(&:to_i)
    ans += (l - pre - 1) % x + r - l + 1
    pre = r
end
print ans, "\n";

class Stu
	attr_accessor:a
	attr_accessor:b
end

#File.open("2.txt","r") do|file|
data=Array.new
h=Array.new
#str=file.gets.to_s
str=gets.to_s
h=str.split
n=h[0].to_i
x=h[1].to_i
i=0
while i<n
	data[i]=Stu.new
	#str=file.gets.to_s
	str=gets.to_s
	h=str.split
	data[i].a=h[0].to_i
	data[i].b=h[1].to_i
	i+=1
end

t=1
i=s=0
while i<n
	s+=(data[i].a-t)%x+data[i].b-data[i].a+1
	t=data[i].b+1
	i+=1
end
puts s
#end

# 499A

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

n, x = read_next_line
t, sum = 1, 0
n.times do
  l, r = read_next_line
  sum += (l - t) % x + r - l + 1
  t = r + 1
end
puts sum

# f = File.open('input.txt', 'r')
f = STDIN
n, x = f.gets.chomp.split.map &:to_i
res = 0
r0 = 0
n.times do
  l, r = f.gets.chomp.split.map &:to_i
  res += (l - r0 - 1) - ((l - r0 - 1) % x)
  r0 = r
end
puts r0 - res

n, x = gets.split.map(&:to_i)
cur = 1
tot = 0
n.times do
	l, r = gets.split.map(&:to_i)
	cur += x while cur + x <= l
	tot += r - cur + 1
	cur = r + 1
end
print tot

n, x = gets.split.map(&:to_i)
t = 1
q = 0
n.times do
  l, r = gets.split.map(&:to_i)
  t += x while t + x <= l
  while t <= r
    q += x
    t += x
  end
end
puts q

#!/usr/bin/env ruby
n,x=gets.split.map(&:to_i)
cur=0
ret=0
n.times{
	l,r=gets.split.map(&:to_i)
	ret+=r+1-l+(l-cur)%x
	cur=r+1
}
p ret

#!/usr/bin/env ruby
n,x=gets.split.map(&:to_i)
cur=0
ret=0
n.times{
	l,r=gets.split.map(&:to_i)
	ret+=r-l+(l-cur)%x
	cur=r
}
p ret

n, x = gets.chomp.split(/ /).map(&:to_i)
watch = 1
skip = 1
n.times do
    l, r = gets.chomp.split(/ /).map(&:to_i)
    skip += x while skip + x <= (l - watch)
    watch += r - l + (skip % x)
end
puts watch

n, x = gets.chomp.split(/ /).map(&:to_i)
watch = 1
n.times do
    l, r = gets.chomp.split(/ /).map(&:to_i)
    skip = 1
    skip += x while skip + x <= l
    watch += r - l + (skip % x)
end
puts watch

n, x = gets.chomp.split(/ /).map(&:to_i)
watch = 1
n.times do
    l, r = gets.chomp.split(/ /).map(&:to_i)
    skip = 1
    skip += x while skip + x <= l
    watch += r - l + (skip % x)
    puts "#{watch} #{skip}"
end
puts watch

#!/usr/bin/ruby
tokens = STDIN.gets.chomp.split(" ").map(&:to_i)
n = tokens[0]
x = tokens[1]
minute = 1
ans = 0
n.times do
    range = STDIN.gets.chomp.split(" ").map(&:to_i)
    while minute + x <= range[0] do
        minute += x
    end
    ans += range[1] - minute + 1
end

puts ans

n, x = gets.chomp.split(' ').map(&:to_i)
c = 1
total = 0
n.times do |i|
  l, r = gets.chomp.split(' ').map(&:to_i)
  while c + x <= l do c += x end
  total += r-l + 1
  total += l - c
end
puts total

n, x = gets.chomp.split(' ').map(&:to_i)
c = 1
total = 1
n.times do |i|
  l, r = gets.chomp.split(' ').map(&:to_i)
  while c + x <= l do c += x end
  total += r-l
  total += l - c
  c = r
end
puts total

read=lambda{ gets.split(" ").map{|x| x.to_i} }
n,x=read.call
#puts "#{n} #{x}"
ans=0
finish=0
for _ in 0...n
  l,r=read.call
  a=(l-1)/x
  b=r/x
  if b*x<r
    b+=1
  end
  if finish>a*x
    ans+=finish-a*x
  else
    ans+=(b-a)*x
  end
  finish=b*x
end
puts ans

read=lambda{ gets.split(" ").map{|x| x.to_i} }
n,x=read.call
#puts "#{n} #{x}"
ans=0
now=1
for _ in 0...n
  l,r=read.call
  ans+=(l-now)%x+(r-l)+1
end
puts ans

n, x = gets.split(' ').map(&:to_i)
pre = 1
ans = 0
n.times do
    l, r = gets.split(' ').map(&:to_i)
    ans += (l - pre) % x + r - l
    pre = r
end
print ans + 1, "\n";


t = 0
t = gets.chomp.to_i
#puts "Value of t is ",t
while t > 0
    i = 0
    num = 0
    n = 0
    x = 0
    odd = 0
    n, x = gets.chomp.split(' ').map{|a| a.to_i}
    ar = gets.chomp.split(' ').map{|a| a.to_i}
    for i in 0..n-1
        #puts ar[i]
        if ar[i]%2 != 0
            odd = odd + 1
        end
    end
    #puts "The value of odd is", odd
    if odd >= 1 && !(x==n && odd%2 ==0) && !(n==odd && x%2==0 )
        puts "YES"
    else
        puts  "NO"
    end
t = t-1
end

# cook your code here
t = gets.to_i
until t == 0
    x,y = gets.chomp.split().map { |e| e.to_i  }
    arr = gets.chomp.split().map { |e| e.to_i  }
    odd = 0;even = 0;
    for i in 0...arr.length()
        if(arr[i]%2 == 1)
            odd+=1;
        else
            even+=1;
        end
    end
    #print "%d %d" % [odd,even]
    #puts
    if(odd == 0)
        puts "No"
    else
        y-=1;
        odd-=1;
        if(y%2 == 0)
            if(odd%2 == 0)
                odd = odd
            else
                odd-=1
            end
            if(odd >= y or even >= (y-odd))
                puts "Yes"
            else
                puts "No"
            end
        else
            if(even == 0)
                puts "No"
            else
                y-=1
                even-=1
                if(odd%2 == 0)
                    odd = odd
                else
                    odd-=1
                end
                if(odd >= y or even >= (y-odd))
                    puts "Yes"
                else
                    puts "No"
                end
            end
        end
    end
    t-=1
end

num_tests = gets.chomp.to_i

def odd_selection(n,arr)
  odds = evens = 0
  arr.each { |el| el.odd? ? odds += 1 : evens += 1 }
  return "No" if evens == 0 && n.even?
  (n - max_odd_num(odds) <= evens && odds > 0) ? "Yes" : "No"
end

def max_odd_num(num)
  return num.odd? ? num : num - 1
end

i = 0
while i < num_tests
  a,b = gets.chomp.split(" ").map(&:to_i)
  arr = gets.chomp.split(" ").map(&:to_i)
  puts odd_selection(b,arr)
  i += 1
end

gets.to_i.times{
  n,x=gets.split.map(&:to_i)
  a=b=0
  gets.split.map{|x|x.to_i%2==0 ? b+=1:a+=1}
  puts 1.step(a, 2).any?{|i| x-i>=0 and x-i<=b} ? "Yes" : "No"
}

def analyze(a, x)
  even_count = 0
  odd_count = 0
  a.each do |item|
    if item % 2 == 0
      even_count +=1
    else
      odd_count +=1
    end
  end

  return odd_count > 0 if x <= 1

  return false if odd_count < 1

  if x % 2 == 0
    return false if even_count < 1

    odd_count -= 1
    even_count -= 1

    odd_count / 2 + even_count / 2  >= (x -2) / 2
  else
    odd_count -= 1

    odd_count / 2 + even_count / 2  >= (x -1) / 2
  end
end

t = gets.chomp.to_i
res = []
(1..t).each do |_t|
  n, x = gets.chomp.split(' ').map{|a| a.to_i}
  ar = gets.chomp.split(' ').map{|a| a.to_i}
  res << analyze(ar, x)
end

res.each do |r|
  puts (r ? 'YES' : 'NO')
end

def solve
  _, k = gets.strip.split(' ').map(&:to_i)
  arr = gets.strip.split(' ').map(&:to_i)
  ev = arr.select { |e| e.even? }
  od = arr.select { |e| e.odd? }
  while !od.empty?
    if od.size.odd? && od.size <= k
      break
    end
    od.pop
  end
  if od.size + ev.size >= k && !od.size.zero?
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times { solve  }

# cook your code here
t = gets.to_i
until t == 0
    x,y = gets.chomp.split().map { |e| e.to_i  }
    arr = gets.chomp.split().map { |e| e.to_i  }
    odd = 0;even = 0;
    for i in 0...arr.length()
        if(arr[i]%2 == 1)
            odd+=1;
        else
            even+=1;
        end
    end
    #print "%d %d" % [odd,even]
    #puts
    if(odd == 0)
        puts "No"
    else
        y-=1;
        odd-=1;
        if(even >= y)
            puts "Yes"
        else
            if((y-even)%2)
                y = y - even + 1;
            else
                y = y - even;
            end
            if(odd >= y and y%2 == 0)
                puts "Yes"
            else
                puts "No"
            end
        end
    end
    t-=1
end

# cook your code here
t = gets.to_i
until t == 0
    x,y = gets.chomp.split().map { |e| e.to_i  }
    arr = gets.chomp.split().map { |e| e.to_i  }
    odd = 0;even = 0;
    for i in 0...arr.length()
        if(arr[i]%2 == 1)
            odd+=1;
        else
            even+=1;
        end
    end
    #print "%d %d" % [odd,even]
    #puts
    if(odd == 0)
        puts "No"
    else
        y-=1;
        odd-=1;
        if(even >= y)
            puts "Yes"
        else
            if((y-even)%2 == 0)
                y = y - even + 1;
            else
                y = y - even;
            end
            if(odd >= y and y%2 == 0)
                puts "Yes"
            else
                puts "No"
            end
        end
    end
    t-=1
end

# cook your code here
t = gets.to_i
until t == 0
    x,y = gets.chomp.split().map { |e| e.to_i  }
    arr = gets.chomp.split().map { |e| e.to_i  }
    odd = 0;even = 0;
    for i in 0...arr.length()
        if(arr[i]%2 == 1)
            odd+=1;
        else
            even+=1;
        end
    end
    #print "%d %d" % [odd,even]
    #puts
    if(odd == 0)
        puts "No"
    else
        y-=1;
        odd-=1;
        if(even >= y)
            puts "Yes"
        else
            if((y-even)%2 == 1)
                y = y - even + 1;
            else
                y = y - even;
            end
            # puts y
            if(odd >= y and y%2 == 0)
                puts "Yes"
            else
                puts "No"
            end
        end
    end
    t-=1
end

# cook your code here
t = gets.to_i
until t == 0
    x,y = gets.chomp.split().map { |e| e.to_i  }
    arr = gets.chomp.split().map { |e| e.to_i  }
    odd = 0;even = 0;
    for i in 0...arr.length()
        if(arr[i]%2 == 1)
            odd+=1;
        else
            even+=1;
        end
    end
    #print "%d %d" % [odd,even]
    #puts
    if(odd == 0)
        puts "No"
    else
        y-=1;
        odd-=1;
        if(even >= y)
            puts "Yes"
        else
            if((y-even)%2)
                y = y - even + 1;
            else
                y = y - even;
            end
            if(odd >= y)
                puts "Yes"
            else
                puts "No"
            end
        end
    end
    t-=1
end

num_tests = gets.chomp.to_i

def odd_selection(n,arr)
  odds = evens = 0
  arr.each { |el| el.odd? ? odds += 1 : evens += 1 }
  (n - max_odd_num(odds) <= evens && odds > 0) ? "Yes" : "No"
end

def max_odd_num(num)
  return num.odd? ? num : num - 1
end

i = 0
while i < num_tests
  a,b = gets.chomp.split(" ").map(&:to_i)
  arr = gets.chomp.split(" ").map(&:to_i)
  puts odd_selection(b,arr)
  i += 1
end

num_tests = gets.chomp.to_i

def odd_selection(n,arr)
  odds = evens = 0
  arr.each { |el| el.odd? ? odds += 1 : evens += 1 }
  (n - max_odd_num(odds) <= evens && odds > 0) ? "Yes" : "No"
end

def max_odd_num(num)
  return num.odd? ? num : num - 1
end

i = 0
while i < num_tests
  a,b = gets.chomp.split(" ").map(&:to_i)
  arr = gets.chomp.split(" ").map(&:to_i)
  p odd_selection(b,arr)
  i += 1
end

t = 0
t = gets.to_i

while t > 0
    odd = 0
    n = 0
    x = 0
    n = gets.to_i
    x = gets.to_i
    for i in 1..n
        a = gets.to_i
        if a%2 != 0
            odd = odd + 1
        end
    end
    if odd >= 1 && !(x==n && odd%2 ==0) && !(n==odd && x%2==0 )
        puts "YES"
    else
        puts  "NO"
    end
t = t-1
end

gets.to_i.times{
  n,x=gets.split.map(&:to_i)
  a=b=0
  gets.split.map{|x|x.to_i%2==0 ? b+=1:a+=1}
  puts 1.step(a, 2).any?{|i| x-i<=b} ? "Yes" : "No"
}

def analyze(a, x)
  even_count = 0
  odd_count = 0
  a.each do |item|
    if item % 2 == 0
      even_count +=1
    else
      odd_count +=1
    end
  end

  return odd_count > 0 if x <= 1

  if x % 2 == 1
    # x is odd
    even_count -= 1 if even_count % 2 == 1

    even_count + odd_count >= x
  else

    # x is even

    return false if odd_count < 1 || even_count < 1

    # one pair
    odd_count -= 1
    even_count -= 1

    # rest pairs

    even_count -= 1 if even_count % 2 == 1
    odd_count -= 1 if odd_count % 2 == 1

    even_count + odd_count >= x - 2
  end
end

t = gets.chomp.to_i
res = []
(1..t).each do |_t|
  n, x = gets.chomp.split(' ').map{|a| a.to_i}
  ar = gets.chomp.split(' ').map{|a| a.to_i}
  res << analyze(ar, x)
end

res.each do |r|
  puts (r ? 'YES' : 'NO')
end

def analyze(a, x)
  even_count = 0
  odd_count = 0
  a.each do |item|
    if item % 2 == 0
      even_count +=1
    else
      odd_count +=1
    end
  end

  return odd_count > 0 if x <= 1

  return false if odd_count < 1

  if x % 2 == 0
    odd_count -= 1
    even_count -= 1

    odd_count / 2 + even_count / 2  >= (x -2) / 2
  else
    odd_count -= 1

    odd_count / 2 + even_count / 2  >= (x -1) / 2
  end
end

t = gets.chomp.to_i
res = []
(1..t).each do |_t|
  n, x = gets.chomp.split(' ').map{|a| a.to_i}
  ar = gets.chomp.split(' ').map{|a| a.to_i}
  res << analyze(ar, x)
end

res.each do |r|
  puts (r ? 'YES' : 'NO')
end

def analyze(a, x)
  even_count = 0
  odd_count = 0
  a.each do |item|
    if item % 2 == 0
      even_count +=1
    else
      odd_count +=1
    end
  end

  return odd_count > 0 if x <= 1

  return false if odd_count < 1

  odd_count -= 1;

  odd_count / 2 + even_count / 2 + even_count % 2 >= x -1
end

t = gets.chomp.to_i
res = []
(1..t).each do |_t|
  n, x = gets.chomp.split(' ').map{|a| a.to_i}
  ar = gets.chomp.split(' ').map{|a| a.to_i}
  res << analyze(ar, x)
end

res.each do |r|
  puts (r ? 'YES' : 'NO')
end

def solve
  _, k = gets.strip.split(' ').map(&:to_i)
  arr = gets.strip.split(' ').map(&:to_i)
  ev = arr.select { |e| e.even? }
  od = arr.select { |e| e.odd? }
  od.pop if od.size.even? && !od.size.zero?
  if od.size + ev.size >= k && !od.size.zero?
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times { solve  }


N, X, Y = gets.split.map(&:to_i)
num = gets.chomp.split('1').count{|s| s.length>0}
if num == 0
    puts 0
else
    puts Y + (num-1) * [X, Y].min
end

n, x, y = gets.strip.split.map(&:to_i)
str = gets.strip
cstr = str[0]
str.each_char do |c|
  cstr << c if c != cstr[-1]
end

zc = [y]
oc = [0]
n = cstr.size
1.upto(n).each do |i|
  oc << zc[i-1]
  if i%2==1
    zc << zc[i-1]
  else
    zc << [(y+zc[i-2]), (x+zc[i-2])].min
  end
end

puts (str[0]=='0' ? zc[n-1] : oc[n-1])

n, x, y = gets.strip.split.map(&:to_i)
str = gets.strip

nzb = 0
0.upto(n-2).each do |i|
  nzb += 1 if str[i] == '0' && str[i+1]=='1'
end
nzb += 1 if n>1 && str[-1] =='0'

ans = nzb * y
1.upto(nzb-1).each do |nx|
  ans = [ans, (x*nx) + ((nzb-nx)*y)].min
end
puts ans


a = gets
b = gets
ea = a.each_char

h = {:aa => 0, :bb => 0}
b.each_char do |bb|
  aa = ea.next
  next if aa == bb
  aa == "4" ?  h[:aa] += 1 : h[:bb] += 1
end

puts [h[:aa], h[:bb]].max

input = STDIN.read.split("\n")
str1, str2 = input[0].split(//).map(&:to_i), input[1].split(//).map(&:to_i)

str1.length.times {|x|
	if str1[x] == str2[x]
		str1[x] = nil
		str2[x] = nil
	end
}

n4 = str1.count(4)
n7 = str1.count(7)

moves = n4 <= n7 ? n4 : n7
STDOUT.puts moves += (n4 - n7).abs

a=gets.bytes.to_a
b=gets.bytes.to_a
cnt=[0,0]
a.length.times{|i| cnt[a[i]==52?1:0]+=1 if a[i]!=b[i]}
p cnt[0]+cnt[1]-cnt.min

a=gets.split[0].bytes.to_a
b=gets.split[0].bytes.to_a
n=a.length
input=Array.new
n.times{|i|
        input.push([a[i]==52?1:0,b[i]==52?1:0])
}
cnt1=0
cnt2=0
input.each{|t|
        cnt1+=1 if t[0]!=t[1] && t[0]==1
        cnt2+=1 if t[0]!=t[1] && t[0]==0
}
p [cnt1,cnt2].min+(cnt1+cnt2)-[cnt1,cnt2].min*2

a=gets
b=gets
x = a.split('').zip(b.split('')).inject([]) { |m, (x, y)| x != y ? m << x : m }
p x.length - %w[4 7].map { |d| x.count(d) }.min

a=gets.chomp.split("").map{|i|i.to_i}
b=gets.chomp.split("").map{|i|i.to_i}
c=(a.count(4)-b.count(4)).abs
i=0
l=0

if c>0
loop do
if a[i]!=b[i]
a[i]=b[i]
l+=1
end
i+=1
break if l>=c
end
end
length=a.length
swap=0
loop do
if a[i]!=b[i]
a[i]=b[i]
swap+=1
end
i+=1
break if i>=length
end

puts"#{c+swap/2}"

a, b = 2.times.map{gets.chars.to_a}
c = 0
a.zip(b){|i, j| c += 1 if i != j}
p c + (a.count('4') - b.count('4')).abs >> 1
__END__

a=gets.chomp
b=gets.chomp
counts=[0,0]
a.split("").each_index do |i|
 if a[i]=="4" && b[i]=="7"
   counts[0]=counts[0]+1
 elsif a[i]=="7" && b[i]=="4"
   counts[1]=counts[1]+1
 end
end
puts counts.max

a = gets.chomp
b = gets.chomp
x = 0
y = 0
(0...a.size).each do |i|
    if a[i] == '7' and b[i] == '4'
        x += 1
    elsif a[i] == '4' and b[i] == '7'
        y += 1
    end
end
puts [x,y].max

n = m = 0
a = gets.chomp.chars
b = gets.chomp.chars
(0...a.size).each { |i|
  if a[i] != b[i]
    if a[i] == '4'
      n += 1
    else
      m += 1
    end
  end
}
puts [n, m].max

a, b = gets.chomp.split("").map(&:to_i), gets.split("").map(&:to_i)
f, s = 0, 0
a.length.times{ |i|
 next if a[i] == b[i]
 if a[i]!=4
  f += 1
 else
  s += 1
 end
}
p [f, s].min + (f-s).abs

$a = gets.chomp
$b = gets.chomp
plmi = 0
diffcnt = 0
for i in 0..$a.length
  ac = $a.slice(i,1)
  bc = $b.slice(i,1)
  next if ac == bc
  diffcnt += 1
  if ac == "4"
    plmi += 1
  else
    plmi -= 1
  end
end
puts (diffcnt-plmi.abs)/2+plmi.abs

a,b=2.times.map{gets.chomp.chars.to_a}
c4=c7=0
a.size.times{|i|
	unless a[i]==b[i]
		a[i]==?4 ? c4+=1 : c7+=1
	end
}
p [c4,c7].max

$a = gets.chomp
$b = gets.chomp
plmi = 0
diffcnt = 0
for i in 0..$a.length
  ac = $a.slice(i,1)
  bc = $b.slice(i,1)
  next if ac == bc
  diffcnt += 1
  if ac == "4"
    plmi += 1
  else
    plmi -= 1
  end
end
puts diffcnt/2+plmi.abs

a = gets
b = gets
ea = a.each_char

ret = 0
b.each_char do |c|
  l = ea.next
  ret += 1 unless c == l
end

puts ret

a=gets.bytes.to_a
b=gets.bytes.to_a
cnt=[0,0]
a.length.times{|i| cnt[a[i]=="4"?1:0]+=1 if a[i]!=b[i]}
p cnt[0]+cnt[1]-cnt.min

input = STDIN.read.split("\n")
str1, str2 = input[0].split(//).map(&:to_i), input[1].split(//).map(&:to_i)

count = 0

str1.length.times {|x| str1.delete_at x if str1[x] == str2[x] }

n4 = str1.count(4)
n7 = str1.count(7)

moves = n4 <= n7 ? n4 : n7
STDOUT.puts moves += (n4 - n7).abs

input = STDIN.read.split("\n")
str1, str2 = input[0], input[1]

count = 0
skip = false

str1.length.times do |i|

	if str1[i] == str2[i] or skip
		skip = false
		next
	end

	if str1[i + 1].nil?
		count += 1
		break
	end

	if str1[i + 1] != str2[i + 1] and str1[i] != str1[i + 1]
		count += 1
		skip = true
	else
		count += 1
	end

end

STDOUT.puts count


n,l,a = gets.strip.split.map(&:to_i)
intervals = [0]

n.times do
  ti, li = gets.strip.split.map(&:to_i)
  intervals << ti
  intervals << ti + li
end
intervals << l

ans = 0
im = intervals.size / 2
i = 0
im.times do
  s = intervals[i]
  e = intervals[i+1]
  ans += (e-s)/a
  i+=2
end
puts ans

n,l,a = gets.split.map(&:to_i)
ans = 0
t = [[0,0]] + n.times.map{gets.split.map(&:to_i)} + [[l,0]]

for i in 0..n
  ans += (t[i+1][0]-t[i][0]-t[i][1])/a
end

puts ans

nla = gets.chomp.split(" ").map(&:to_i)

n = nla[0]
l = nla[1]
a = nla[2]

arr = []
n.times do
  arr.push gets.chomp.split(" ").map(&:to_i)
end

arr.sort_by{|x| x[0]}

if n==0
  puts l/a
  exit
end

first = arr[0][0]/a
last = (l-(arr[n-1][0]+arr[n-1][1]))/a

ans = 0

arr.each_with_index do |x,i|
  next if i==n-1
  ans += (arr[i+1][0]-(x[0]+x[1]))/a
end

ans += (first+last)
puts ans

nla = gets.chomp.split(" ").map(&:to_i)

n = nla[0]
l = nla[1]
a = nla[2]

arr = []
n.times do
  arr.push gets.chomp.split(" ").map(&:to_i)
end

arr.sort_by{|x| x[0]}

if n==0
  puts l/a
  exit
end

first = arr[0][0]/a
last = (l-arr[n-1][1])/a

ans = 0

arr.each_with_index do |x,i|
  next if i==n-1
  ans += (arr[i+1][0]-x[1])/a
end

ans += (first+last)
puts ans


n=gets.to_i
a=gets.split.map &:to_i;
z=i=j=0;
while i<n
	j+=1 while j<n&&a[i]==a[j]
	z+=(j-i)*(j-i+1)/2
	i=j
end
p z

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

count = arr.length

arr.chunk{|x| x}.each do |r|
	l = r[1].length
 	if l > 1
 		count += (l-1).downto(1).inject(0,:+)
 	end
end
puts count

n = gets.to_i
a = gets.split

prev = 2 * 1e9.to_i
sz = 0
ans = 0
a.each do |x|
  if x == prev
    sz += 1
  else
    ans += sz * (sz + 1) / 2
    sz = 1
    prev = x
  end
end
puts ans + sz * (sz + 1) / 2

n = gets.chomp.to_i
ar = gets.chomp.split(/ /).map!{|x| x.to_i}
ret = n
samecnt = 0
for i in 1..n-1
	if (ar[i] == ar[i-1])
		samecnt += 1
	else
		ret += ((samecnt+1)*samecnt)/2
		samecnt = 0
	end
end
ret += ((samecnt+1)*samecnt)/2
puts ret

gets
a=gets.split.map{|x| x.to_i}
last, cnt = -(10**10), 0
ans = 0

a.each do |x|
	if last != x then
		ans += cnt*(cnt+1)
		last = x
		cnt = 1
	else
		cnt += 1
	end
end

p (ans+cnt*(cnt+1))/2

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

def getis;gets.split.map(&:to_i);end
def log(m); warn(m); end

gets
nums = getis

i=0
pre=nil
c=1
log nums
nums.each do |n|
  log [pre, n]
  if pre == n
    c+=1
    i+=c
  else
    pre=n
    c=1
    i+=1
  end
end
puts i

n = gets.chomp.to_i
ar = gets.chomp.split(/ /).map!{|x| x.to_i}
ar.sort!
ret = n
samecnt = 0
for i in 1..n-1
	if (ar[i] == ar[i-1])
		samecnt += 1
	else
		ret += ((samecnt+1)*samecnt)/2
		samecnt = 0
	end
end
ret += ((samecnt+1)*samecnt)/2
puts ret


n,v=gets.split.map(&:to_i)
d=[0]*3001
n.times do
  a,b=gets.split.map(&:to_i)
  d[a-1]+=b
end
s=r=0
3001.times do |i|
  e=[r,v].min
  f=[v-e,d[i]].min
  s+=e+f
  r=d[i]-f
end
p s

n, v = gets.split.map(&:to_i)
x = [0] * 3001
n.times do
  pos, value = gets.split.map(&:to_i)
  x[pos - 1] += value
end

s = r = 0
3001.times do |i|
  e = [r, v].min
  f = [x[i], v - e].min
  s += e + f
  r = x[i] - f
end
p s

n,v = gets.split.map(&:to_i)
ripe_on_day = Array.new(3010, 0)
(0...n).each { |_|
  day,amount = gets.split.map(&:to_i)
  ripe_on_day[day] += amount
}
puts ripe_on_day.inject(0) { |memo, value|
  harvest = [memo[1] + value, v].min
  take_new_fruits = [0, v - memo[1]].max
  [memo[0] + harvest, [0, value - take_new_fruits].max]
}[0]

n, v = gets.split.collect{|i| i.to_i}
num = Hash.new(0)
(0...n).each do
    tmp = gets.split.collect{|i| i.to_i}
    num[tmp[0]] += tmp[1]
end
ans = 0
(1..3001).each do |i|
    tmp = [num[i-1], v].min
    ans += tmp
    tmp = v-tmp
    if tmp > 0
        if tmp >= num[i]
            ans += num[i]
            num[i] = 0
        else
            ans += tmp
            num[i] -= tmp
        end
    end
end
puts ans

#!/usr/bin/ruby
n,v=gets.split.map(&:to_i)
a=n.times.map{gets.split.map(&:to_i)}
ma=a.max[0]
m=[0]*(ma+2)
a.each{|x,y|m[x]+=y}
r=0
1.step(ma+1){|i|
	avail=m[i-1]+m[i]
	gather=[avail,v].min
	r+=gather
	x=[m[i-1],gather].min
	m[i-1]-=x
	gather-=x
	m[i]-=gather
}
p r


n = gets.chomp.to_i
s = gets.chomp.split('')
ans = 0
n.times do |i|
    if i.odd?
        c = s[i - 1]
        if c == 'a'
            c = 'b'
        else
            c = 'a'
        end
        if s[i] != c
            ans += 1
        end
        s[i] = c
    end
end
puts ans
puts s.join('')

n, s = gets.to_i, gets.chomp
i, nsubs, a = 0, 0, [nil]*s.size
while i < n
  if i%2 == 0 && s[i] == s[i + 1]
    nsubs += 1
    a[i] = s[i] == 'a' ? 'b' : 'a'
  else
    a[i] = s[i]
  end
  i += 1
end
p nsubs
puts a.join

def anti(c)
  return 'b' if c == 'a'

  'a'
end
_ = gets.chomp
s = gets.chomp
n = 0
tmp = s.split('').each_slice(2).to_a
tmp.each_with_index do |el, ind|
  if el[0] == el[1]
    tmp[ind] = [el[0], anti(el[1])]
    n += 1
  end
end
puts(n)
puts(tmp.flatten.join(''))

#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

n = inp[0]
s = gets.chomp.split("")
f = false
ans = 0
s.each.with_index do |c, i|
  if(i.odd?)
    if(c == "a")
      if(f)
        s[i] = "b"
        ans += 1
      end
    else
      if(!f)
        s[i] = "a"
        ans += 1
      end
    end
    f = false
  else
    f = true if c == "a"
  end
end
p ans
puts s.join

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
########### ( ˘ω˘ )ｽﾔｧ… ###########
n=gi
s=gs
ans=''
(n/2).times do |i|
  c1=s[2*i]
  c2=s[2*i+1]
  if c1==c2
    cnt+=1
    ans<<'ab'
  else
    ans<<(c1<<c2)
  end
end

puts "#{cnt}\n#{ans}"

def anti(c)
  return 'b' if c == 'a'

  'a'
end
_ = gets.chomp
s = 'b'
n = 0
prev = '_'
tmp = s.split('')
tmp.each_with_index do |el, ind|
  if el == prev
    s[ind] = anti(el)
    prev = anti(el)
    n += 1
  else
    s[ind] = el
    prev = el
  end
end
puts(n)
puts(s)

#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

n = inp[0]
s = gets.chomp.split("")
f = false
ans = 0
s.each.with_index do |c, i|
  if(i.odd?)
    if(c == "a")
      if(f)
        s[i] = "b"
        ans += 1
      end
    else
      if(!f)
        s[i] = "a"
        ans += 1
      end
    end
  else
    f = true if c == "a"
  end
end
p ans
puts s.join


gets.to_i.times do
  gets
  xs = gets.split.map(&:to_i)
  ys = xs.uniq
  diff = xs.size - ys.size
  p ys.size - (diff & 1)
end

n = gets.to_i

n.times do
  m = gets.to_i

  hashmap = Hash.new(0)
  initial_size = 0
  gets.split(" ").each do |chunk|
    initial_size += 1
    hashmap[chunk] += 1
  end

  result = 0
  hashmap.each_value do |value|
    result += value - 1 if value > 1
  end

  result += 1 if result % 2 == 1

  p (initial_size - result)
end

t = gets.to_i
t.times do
    n=gets.to_i
    d=gets.split.map &:to_i
    uniques = d.uniq.size
    if uniques%2==n%2
        puts uniques
    else
        puts uniques - 1
    end
end

$cnt = []

def solve
    n = gets.to_i
    a = gets.split.map(&:to_i)
    a.each do |x|
        $cnt[x] = $cnt[x] + 1
    end
    ans = 0
    even = 0
    a.each do |x|
        if $cnt[x] % 2 == 1
            ans = ans + 1
        elsif $cnt[x] > 0
            even = even + 1
        end
        $cnt[x] = 0
        if even == 2
            ans = ans + 2
            even = 0
        end
    end
    puts ans
end

maxn = 10005
maxn.times do
    $cnt.append(0)
end
gets.to_i.times do
    solve
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  at = a.tally
  e = a.uniq.size
  if n.even?
    if e.even?
      puts e
    else
      puts e-1
    end
  else
    if e.odd?
      puts e
    else
      puts e-1
    end
  end
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i).tally
  puts a.size - (n - a.size) % 2
end

main = -> {
  t = int
  t.times do
      n = int
      a = ints

      double_count = 0
      a.tally.each_value do |c|
          double_count += c - 1
      end

      puts n - double_count.div_ceil(2) * 2
  end
}

DEBUG = false
MOD = 10**9+7
INF = 10**18
$yesno = %w(No Yes)
$YesNo = %w(No Yes)
$YESNO = %w(NO YES)

require "set"
require "prime"

def int; gets.to_s.to_i end
def ints; gets.to_s.split.map { |s| s.to_i } end
def int1s; gets.to_s.split.map { |s| s.to_i - 1 } end
def float; gets.to_s.to_f end
def floats; gets.to_s.split.map { |s| s.to_f } end
def array_of(&convert); gets.to_s.split.map(&convert) end
def string; gets.to_s.chomp end
def strings; gets.to_s.split end
def rep(n, &b); Array.new(n, &b) end
def yes; puts $yesno[1] end
def no; puts $yesno[0] end
def yesno t; puts $yesno[t ? 1 : 0] end
def YesNo t; puts $YesNo[t ? 1 : 0] end
def YESNO t; puts $YESNO[t ? 1 : 0] end
def zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end
def max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end
def min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end
def minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end
def gcd(*xs); xs.inject(0, :gcd) end
def matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end
def debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end
def debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join("") } if DEBUG; grid end
def if_debug; yield if DEBUG end

module Boolean
  def coerce(other); [other, to_i] end
  def +@; to_i end
  def to_int; to_i end
  def *(other); to_i * other end
end

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

class Integer
  def div_ceil(y); (self + y - 1) / y end
  def mod_inv(mod = MOD); pow(mod-2, mod) end
  def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end
  def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end
  def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end
  def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster
  def bitbrute(&block); (1<<self).times(&block) end
  def nCr(r); x = 1; (1..r).each { |i| x *= self + 1 - i; x /= i }; x; end
  def each_divisor; return Enumerator.new { |y| each_divisor { |k| y << k } } unless block_given?; k = 1; while k * k < self; if self % k == 0; yield k; yield self / k end; k += 1; end; yield k if k * k == self end
  def divisors; each_divisor.to_a end
end

class Range
  def end_open; exclude_end? ? self.end : self.end + 1 end
  def end_close; exclude_end? ? self.end - 1 : self.end end
  def upper_bound; ac, wa = self.begin, self.end_open; while wa - ac > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end
  def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end
end

class Array
  def power(&block); (0 ... 1 << size).each(&block) end
  def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end
  def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end
  def cumdiff(range); self[range.end_open] - self[range.begin]; end
  def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end
end

module Enumerable
  def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end
  def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end
end

main[]


n = gets.to_i
a = gets.split(' ').map(&:to_i)
ans = 0
n.times do |i|
  ans += a[i] * (1+i)
  ans -= i
end
puts ans

gets
s = 0
gets.split.map(&:to_i).each_with_index do |a, i|
	s += (a - 1) * (i + 1) + 1
end
puts s

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

ans=0
n.times{|i|
        ans+=a[i]*(1+i)
        ans-=i
}
p ans

n = gets.to_i
arr = gets.split(' ').map(&:to_i)
ans = 0
n.times do |i|
    ans += (arr[i] - 1) * (1 + i)
end
ans += n
print (ans)

n = gets.to_i
arr = gets.split(' ').map(&:to_i)
ans = 0
n.times do |i|
    ans += (arr[i] - 1) * (1 + i)
end
ans += n
print ans

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
dp = [1]
ans = 0
arr.reverse_each do |e|
	dp << e + dp[-1] - 1
	ans += dp[-1]
end
puts ans

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
ans = 0
n.times do |i|
    ans += (i+1)*(a[i]-1)+1
end
puts ans

n = gets.chomp.to_i
a = gets.chomp.split(/ /).map!{|x| x.to_i}
sum = 0
for i in 0..n-1
  sum += a[i]
  sum += (i * (a[i]-1))
end
puts sum

#!/usr/bin/ruby
noq = gets.chop.to_i
qp = gets.chop.split(' ');
clicks = 0
for i in 0..qp.count-1
        clicks += qp[i].to_i*(i+1) - i;
end
puts clicks.to_s

#!/usr/bin/ruby
noq = gets.chop.to_i
qp = gets.chop.split(' ');
clicks = 0
for i in 0..qp.count-1
	clicks += qp[i].to_i
	clicks += qp[i].to_i*i - i;
end
puts clicks.to_s

n=gets.to_i
res,x=0,0
gets.split.map(&:to_i).reverse.each {|a|
    res+=a+x
    x+=a-1
}
p res

#!/usr/bin/env ruby
#

def solve ai
  ret = ai[0]
  (1..ai.length-1).each do |i|
    ret += i*(ai[i]-1)
    ret += ai[i]
  end
  ret
end

n=STDIN.gets.to_i
arr = STDIN.gets.split(" ").map {|i| i.to_i}
p solve(arr)

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

ans=0
n.times{|i|
        ans+=a[i]*(1+i)
        ans-=1
}
p ans+1

#!/usr/bin/ruby
noq = gets.chop.to_i
qp = gets.chop.split(' ');
clicks = 0
for i in 0..qp.count-1
        clicks += qp[i].to_i*i;
end
puts clicks.to_s

#!/usr/bin/ruby
noq = gets.chop.to_i
qp = gets.chop.split(' ');
clicks = 0
for i in 0..qp.count-1
	clicks += qp[i].to_i
end
if qp.count > 1
	for i in 1..qp.count-1
		clicks+= qp[i].to_i
	end
end
clicks -= noq-1
puts clicks.to_s

n=gets.to_i
res=0
gets.split.map(&:to_i).each {|a|
    n-=1
    res+=a
    res+=n if a>1
}
p res

#!/usr/bin/env ruby
#

def solve ai
  ret = ai[0]
  (1..ai.length-1).each do |i|
    ret += i #if ai[i] > 1
    ret += ai[i]
  end
  ret
end

n=STDIN.gets.to_i
arr = STDIN.gets.split(" ").map {|i| i.to_i}
p solve(arr)

#!/usr/bin/env ruby
#

def solve ai
  ret = ai[0]
  (1..ai.length-1).each do |i|
    ret += i if ai[i] > 1
    ret += ai[i]
  end
  ret
end

n=STDIN.gets.to_i
arr = STDIN.gets.split(" ").map {|i| i.to_i}
p solve(arr)

#!/usr/bin/env ruby
#

def solve ai
  ret = ai[0]
  (1..ai.length-1).each do |i|
    ret *= ai[i]-1
    ret += ai[i]
  end
  ret
end

n=STDIN.gets.to_i
arr = STDIN.gets.split(" ").map {|i| i.to_i}
p solve(arr)


# 2
# 4
# 1 0 0 1 0
# 0 1 0 0 1
# 0 0 0 1 0
# 0 1 0 1 0
# 2
# 0 0 0 1 0
# 0 0 0 1 0

require 'set'

n = gets.to_i

def result
  n = gets.to_i
  arrs = n.times.map { gets.split.map(&:to_i) }
  counts = Array.new(5) { Set.new }
  arrs.each.with_index do |arr, j|
    5.times do |i|
      counts[i].add(j) if arr[i] == 1
    end
  end

  counts = counts.select { |c| c.size >= n / 2 }
  counts.size.times do |i|
    first_group = counts[i]
    (i + 1).upto(counts.size - 1) do |j|
      second_group = counts[j]
      big_group, small_group = if second_group.size < first_group.size
                                 [first_group,
                                  second_group]
                               else
                                 [second_group, first_group]
                               end
      f = 0
      s = 0
      b = 0
      big_group.each do |stu|
        if small_group === stu
          b += 1
        else
          f += 1
        end
      end
      small_group.each do |stu|
        next if big_group === stu

        s += 1
      end
      return 'YES' if n / 2 - f >= 0 && n / 2 - s >= 0 && b == n - f - s
    end
  end
  'NO'
end

n.times do
  puts result
end

days = [*0..4]

gets.to_i.times do
  n = gets.to_i
  xxs = n.times.map { gets.split.map(&:to_i) }
  ok = days.any? do |i|
    (days - [i]).any? do |j|
      ii = xxs.count { |xs| xs[i] == 1 && xs[j] == 0 }
      jj = xxs.count { |xs| xs[j] == 1 && xs[i] == 0 }
      ij = xxs.count { |xs| xs[i] == 1 && xs[j] == 1 }
      ni = [0, n / 2 - ii].max
      nj = [0, n / 2 - jj].max
      ni + nj <= ij
    end
  end
  puts ok ? "YES" : "NO"
end

gets.to_i.times do
  n=gets.to_i
  n2=n/2
  l=[]
  5.times do
    l << [0]*5
  end
  n.times do
    s=gets.split.map(&:to_i)
    5.times do |i|
      5.times do |j|
        if s[i]+s[j]==2
          l[i][j]+=1
        end
      end
    end
  end
  f=false
  5.times do |j|
    j.times do |i|
      if l[i][i]+l[j][j]-l[i][j]==n&&l[i][i]>=n2&&l[j][j]>=n2
        f=true
        break
      end
    end
    break if f
  end
  if f
    puts "YES"
  else
    puts "NO"
  end

end

t = gets.chomp.to_i

t.times do
  n = gets.chomp.to_i

  week = Array.new(5) { [] }

  n.times do
    v = gets.chomp.split.map(&:to_i)

    week[0] << v[0]
    week[1] << v[1]
    week[2] << v[2]
    week[3] << v[3]
    week[4] << v[4]
  end

  good = false

  5.times do |i|
    for j in i+1..4
      l, r, no = 0, 0, 0

      [week[i], week[j]].transpose.each do |a,b|
        if a == 1
          l += 1
        end

        if b == 1
          r += 1
        end

        if a == 0 and b == 0
          no += 1
        end
      end

      if l >= n / 2 and r >= n / 2 and no == 0
        good = true
        break
      else
        next
      end
    end
  end

  if good
    puts "YES"
  else
    puts "NO"
  end
end

# 2
# 4
# 1 0 0 1 0
# 0 1 0 0 1
# 0 0 0 1 0
# 0 1 0 1 0
# 2
# 0 0 0 1 0
# 0 0 0 1 0

require 'set'

n = gets.to_i

def result
  n = gets.to_i
  arrs = n.times.map { gets.split.map(&:to_i) }
  counts = Array.new(5) { Set.new }
  arrs.each.with_index do |arr, j|
    5.times do |i|
      counts[i].add(j) if arr[i] == 1
    end
  end

  counts = counts.select { |c| c.size >= n / 2 }
  counts.size.times do |i|
    first_group = counts[i]
    (i + 1).upto(counts.size - 1) do |j|
      second_group = counts[j]
      big_group, small_group = if second_group.size < first_group.size
                                 [first_group,
                                  second_group]
                               else
                                 [second_group, first_group]
                               end
      changed = 0
      big_group.each do |stu|
        changed += 1 if small_group === stu
      end
      if n / 2 - big_group.size + changed >= 0 && n / 2 - small_group.size + changed >= 0 && 0 == n - big_group.size + small_group.size - (3 * changed)
        return 'YES'
      end
    end
  end
  'NO'
end

n.times do
  puts result
end

# 2
# 4
# 1 0 0 1 0
# 0 1 0 0 1
# 0 0 0 1 0
# 0 1 0 1 0
# 2
# 0 0 0 1 0
# 0 0 0 1 0

require 'set'

n = gets.to_i

def result
  n = gets.to_i
  arrs = n.times.map { gets.split.map(&:to_i) }
  counts = Array.new(5) { Set.new }
  arrs.each.with_index do |arr, j|
    5.times do |i|
      counts[i].add(j) if arr[i] == 1
    end
  end

  counts = counts.select { |c| c.size >= n / 2 }
  counts.size.times do |i|
    first_group = counts[i]
    (i + 1).upto(counts.size - 1) do |j|
      second_group = counts[j]
      big_group, small_group = if second_group.size < first_group.size
                                 [first_group,
                                  second_group]
                               else
                                 [second_group, first_group]
                               end
      changed = 0
      big_group.each do |stu|
        changed += 1 if small_group === stu
      end
      if n / 2 - big_group.size + changed >= 0 && n / 2 - small_group.size + changed >= 0 && 0 == n - big_group.size + small_group.size - changed
        return 'YES'
      end
    end
  end
  'NO'
end

n.times do
  puts result
end

# 2
# 4
# 1 0 0 1 0
# 0 1 0 0 1
# 0 0 0 1 0
# 0 1 0 1 0
# 2
# 0 0 0 1 0
# 0 0 0 1 0

require 'set'

n = gets.to_i

def result
  n = gets.to_i
  arrs = n.times.map { gets.split.map(&:to_i) }
  counts = Array.new(5) { Set.new }
  arrs.each.with_index do |arr, j|
    5.times do |i|
      counts[i].add(j) if arr[i] == 1
    end
  end

  counts = counts.select { |c| c.size >= n / 2 }
  counts.size.times do |i|
    first_group = counts[i]
    (i + 1).upto(counts.size - 1) do |j|
      second_group = counts[j]
      big_group, small_group = if second_group.size < first_group.size
                                 [first_group,
                                  second_group]
                               else
                                 [second_group, first_group]
                               end
      changed = 0
      big_group.each do |stu|
        changed += 1 if small_group === stu
      end
      0.upto(changed) do |z|
        return 'YES' if z + small_group.size == n / 2 && big_group.size - changed + z == n / 2
      end
    end
  end
  'NO'
end

n.times do
  puts result
end

# 2
# 4
# 1 0 0 1 0
# 0 1 0 0 1
# 0 0 0 1 0
# 0 1 0 1 0
# 2
# 0 0 0 1 0
# 0 0 0 1 0

require 'set'

n = gets.to_i

def result
  n = gets.to_i
  arrs = n.times.map { gets.split.map(&:to_i) }
  counts = Array.new(5) { Set.new }
  arrs.each.with_index do |arr, j|
    5.times do |i|
      counts[i].add(j) if arr[i] == 1
    end
  end

  counts = counts.select { |c| c.size >= n / 2 }
  result = true
  counts.size.times do |i|
    first_group = counts[i]
    (i + 1).upto(counts.size - 1) do |j|
      second_group = counts[j]
      big_group, small_group = if second_group.size < first_group.size
                                 [first_group,
                                  second_group]
                               else
                                 [second_group, first_group]
                               end
      changed = 0
      big_group.each do |stu|
        changed += 1 if small_group === stu
      end
      result = false if small_group.size + big_group.size - changed == n || big_group.size + small_group.size == n
    end
  end
  !result ? 'YES' : 'NO'
end

n.times do
  puts result
end

# 2
# 4
# 1 0 0 1 0
# 0 1 0 0 1
# 0 0 0 1 0
# 0 1 0 1 0
# 2
# 0 0 0 1 0
# 0 0 0 1 0

require 'set'

n = gets.to_i

def result
  n = gets.to_i
  arrs = n.times.map { gets.split.map(&:to_i) }
  counts = Array.new(5) { Set.new }
  arrs.each.with_index do |arr, j|
    5.times do |i|
      counts[i].add(j) if arr[i] == 1
    end
  end

  counts = counts.select { |c| c.size >= n / 2 }
  result = true
  counts.size.times do |i|
    first_group = counts[i]
    (i + 1).upto(counts.size - 1) do |j|
      second_group = counts[j]
      big_group, small_group = if second_group.size < first_group.size
                                 [first_group,
                                  second_group]
                               else
                                 [second_group, first_group]
                               end
      changed = 0
      big_group.each do |stu|
        changed += 1 if small_group === stu
      end
      if (changed + small_group.size >= n / 2 && small_group.size - (n / 2) <= changed && big_group.size - small_group.size + changed / 2 >= n / 2) || big_group.size - changed + small_group.size == n
        result = false
      end
    end
  end
  !result ? 'YES' : 'NO'
end

n.times do
  puts result
end

# 2
# 4
# 1 0 0 1 0
# 0 1 0 0 1
# 0 0 0 1 0
# 0 1 0 1 0
# 2
# 0 0 0 1 0
# 0 0 0 1 0

require 'set'

n = gets.to_i

def result
  n = gets.to_i
  arrs = n.times.map { gets.split.map(&:to_i) }
  counts = Array.new(5) { Set.new }
  arrs.each.with_index do |arr, j|
    5.times do |i|
      counts[i].add(j) if arr[i] == 1
    end
  end

  result = true
  counts.size.times do |i|
    first_group = counts[i]
    (i + 1).upto(4) do |j|
      second_group = counts[j]
      big_group, small_group = if second_group.size < first_group.size
                                 [first_group,
                                  second_group]
                               else
                                 [second_group, first_group]
                               end
      changed = 0
      big_group.each do |stu|
        changed += 1 if small_group === stu
      end
      if (changed + small_group.size >= n / 2 && small_group.size - (n / 2) <= changed && big_group.size - small_group.size + changed / 2 >= n / 2) || big_group.size - changed + small_group.size == n
        result = false
      end
    end
  end
  !result ? 'YES' : 'NO'
end

n.times do
  puts result
end

# 2
# 4
# 1 0 0 1 0
# 0 1 0 0 1
# 0 0 0 1 0
# 0 1 0 1 0
# 2
# 0 0 0 1 0
# 0 0 0 1 0

require 'set'

n = gets.to_i

def result
  n = gets.to_i
  arrs = n.times.map { gets.split.map(&:to_i) }
  counts = Array.new(5) { Set.new }
  arrs.each.with_index do |arr, j|
    5.times do |i|
      counts[i].add(j) if arr[i] == 1
    end
  end

  result = true
  counts.size.times do |i|
    first_group = counts[i]
    (i + 1).upto(4) do |j|
      second_group = counts[j]
      big_group, small_group = if second_group.size < first_group.size
                                 [first_group,
                                  second_group]
                               else
                                 [second_group, first_group]
                               end
      changed = 0
      big_group.each do |stu|
        changed += 1 if small_group === stu
      end
      if changed + small_group.size >= n / 2 && small_group.size - (n / 2) <= changed && big_group.size - small_group.size + changed / 2 >= n / 2
        result = false
      end
    end
  end
  !result ? 'YES' : 'NO'
end

n.times do
  puts result
end

# 2
# 4
# 1 0 0 1 0
# 0 1 0 0 1
# 0 0 0 1 0
# 0 1 0 1 0
# 2
# 0 0 0 1 0
# 0 0 0 1 0

require 'set'

n = gets.to_i

def result
  n = gets.to_i
  arrs = n.times.map { gets.split.map(&:to_i) }
  counts = Array.new(5) { Set.new }
  arrs.each.with_index do |arr, j|
    5.times do |i|
      counts[i].add(j) if arr[i] == 1
    end
  end

  data = counts.select do |count|
    count.size >= n / 2
  end

  result = true
  data.size.times do |i|
    first_group = data[i]
    (i + 1).upto(data.size - 1) do |j|
      second_group = data[j]
      big_group, small_group = if second_group.size < first_group.size
                                 [first_group,
                                  second_group]
                               else
                                 [second_group, first_group]
                               end
      changed = 0
      big_group.each do |stu|
        changed += 1 if small_group === stu
      end
      if (small_group.size + changed) >= n / 2 && big_group.size - changed + (small_group.size + changed - n / 2)
        result = false
      end
    end
  end
  !result ? 'YES' : 'NO'
end

n.times do
  puts result
end

# 2
# 4
# 1 0 0 1 0
# 0 1 0 0 1
# 0 0 0 1 0
# 0 1 0 1 0
# 2
# 0 0 0 1 0
# 0 0 0 1 0

require 'set'

n = gets.to_i

def result
  n = gets.to_i
  arrs = n.times.map { gets.split.map(&:to_i) }
  counts = Array.new(5) { Set.new }
  arrs.each.with_index do |arr, j|
    5.times do |i|
      counts[i].add(j) if arr[i] == 1
    end
  end

  data = counts.select do |count|
    count.size >= n / 2
  end

  result = true
  data.size.times do |i|
    first_group = data[i]
    (i + 1).upto(data.size - 1) do |j|
      second_group = data[j]
      big_group, small_group = if second_group.size < first_group.size
                                 [first_group,
                                  second_group]
                               else
                                 [second_group, first_group]
                               end
      changed = 0
      big_group.each do |stu|
        changed += 1 if small_group === stu
      end
      result = false if (small_group.size + changed) >= n / 2
    end
  end
  !result ? 'YES' : 'NO'
end

n.times do
  puts result
end

t = gets.chomp.to_i

t.times do
  n = gets.chomp.to_i

  week = Array.new(5) { [] }

  n.times do
    v = gets.chomp.split.map(&:to_i)

    week[0] << v[0]
    week[1] << v[1]
    week[2] << v[2]
    week[3] << v[3]
    week[4] << v[4]
  end

  good = false

  5.times do |i|
    for j in (i+1)..4
      l, r = 0, 0

      [week[i], week[j]].transpose.each do |a,b|
        if a == 1 and b == 1
          l += 1
        end

        if a == 1
          l += 1
        end

        if b == 1
          r += 1
        end
      end

      if l+r == n and l != 0 and r != 0
        good = true
        break
      else
        next
      end
    end
  end

  if good
    puts "YES"
  else
    puts "NO"
  end
end

t = gets.chomp.to_i

t.times do
  n = gets.chomp.to_i

  week = Array.new(5) { [] }

  n.times do
    v = gets.chomp.split.map(&:to_i)

    week[0] << v[0]
    week[1] << v[1]
    week[2] << v[2]
    week[3] << v[3]
    week[4] << v[4]
  end

  good = false

  5.times do |i|
    for j in i+1..4
      ok = true

      [week[i], week[j]].transpose.each do |a,b|
        if a == 0 and b == 0
          ok = false
          break
        end
      end

      if ok
        good = true
        break
      else
        next
      end
    end
  end

  if good
    puts "YES"
  else
    puts "NO"
  end
end

t = gets.chomp.to_i

t.times do
  n = gets.chomp.to_i

  week = Array.new(5) { [] }

  n.times do
    v = gets.chomp.split.map(&:to_i)

    week[0] << v[0]
    week[1] << v[1]
    week[2] << v[2]
    week[3] << v[3]
    week[4] << v[4]
  end

  good = false

  n.times do |i|
    for j in i..n
      ok = true

      [week[i], week[j]].transpose.each do |a, b|
        if a == 0 and b == 0
          ok = false
          break
        end
      end

      if ok
        good = true
        break
      else
        next
      end
    end
  end

  if good
    puts("YES")
  else
    puts "NO"
  end
end

t = gets.chomp.to_i

t.times do
  n = gets.chomp.to_i

  week = Array.new(5) { [] }

  n.times do
    v = gets.chomp.split.map(&:to_i)

    week[0] << v[0]
    week[1] << v[1]
    week[2] << v[2]
    week[3] << v[3]
    week[4] << v[4]
  end

  good = false

  n.times do |i|
    for j in i..n
      ok = true

      [week[i], week[j]].transpose.each do |a, b|
        if a == 0 and b == 0
          ok = false
          break
        end
      end

      if ok
        good = true
        break
      else
        next
      end
    end
  end

  if good
    p "YES"
  else
    p "NO"
  end
end


n = gets.to_i
val = nil
5.upto(n**0.5) do |i|
  if n % i == 0
    val = i
    break
  end
end
vow = "aeiou"
if val == nil
  p -1
  exit
else
  row = vow * (val*2/5 + 1)
  (n/val).times do |i|
    print row[i%val,val]
  end
  print "\n"
end

n = gets.to_i
val = nil
5.upto(n**0.5) do |i|
  if n % i == 0
    val = i
    break
  end
end
vow = "aeiou"
if val == nil
  p -1
  exit
else
  row = vow * (val/5) + vow[0,val%5]
  row *= 2
  (n/val).times do |i|
    print row[i%val,val]
  end
  print "\n"
end

n = gets.to_i
f = false
u = v = 0
for i in 5..n
    if n%i == 0 && n/i >= 5
        f = true
        u = i
        v = n/i
        break
    end
end
if !f
    puts "-1"
    exit
end
s = "aiueo"*(u/5+2)
ans = ""
v.times do |i|
    ans += s[i%5,u]
end
puts ans

def tempfun(k)
	temp = ['a','e','i','o','u']
	n = k - 5
	n.to_i.times do
		temp << 'g'
	end
	ans = []
	for b in 0...temp.length
		ans << temp.rotate(b)
	end
	return ans
end
mark = false
i = gets.chomp.to_i
k = Math.sqrt(i)
ans = Array.new
if(k >= 5)
	t = k.to_i
	for z in 5..t
		if(i % z == 0 )
			mark  = true
			m = (i - z*z)
			ans = tempfun(z)
			#print(ans)
			for i in 0...z
				for j in 0... (m/z)
					ans[i] << ans[i][j]
				end
			end
			break
		end
	end
	if(mark)
		for i in ans
			for m in i
				print(m)
			end
			#puts()
		end
	else
		print(-1)
	end
else
	print(-1)
end

def tempfun(k)
	temp = ['a','e','i','o','u']
	n = k - 5
	n.to_i.times do
		temp << 'g'
	end
	ans = []
	for b in 0...temp.length
		ans << temp.rotate(b)
	end
	return ans
end

i = gets.chomp.to_i
k = Math.sqrt(i)
ans = Array.new
if(k >= 5)
	t = k.to_i
	if((i - t*t) % t == 0 )
		m = (i - t*t)
		ans = tempfun(t)
		#print(ans)
		for i in 0...t
			for j in 0... (m/t)
				ans[i] << ans[i][j]
			end
		end
	else
		print(-1)
		exit
	end
	for i in ans
		for m in i
			print(m)
		end
		puts()
	end
else
	print(-1)
	exit
end

def tempfun(k)
	temp = ['a','e','i','o','u']
	n = k - 5
	n.to_i.times do
		temp << 'g'
	end
	ans = []
	for b in 0...temp.length
		ans << temp.rotate(b)
	end
	return ans
end

i = gets.chomp.to_i
k = Math.sqrt(i)
ans = Array.new
if(k >= 5)
	t = k.to_i
	if((i - t*t) % t == 0 )
		m = (i - t*t)
		ans = tempfun(t)
		#print(ans)
		for i in 0...t
			for j in 0... (m/t)
				ans[i] << ans[i][j]
			end
		end
	else
		print(-1)
		exit
	end
	for i in ans
		for m in i
			print(m)
		end

	end
else
	print(-1)
	exit
end

n = gets.to_i
val = nil
5.upto(n**0.5) do |i|
  if n % i == 0
    val = i
    break
  end
end
if val == nil
  p -1
  exit
else
  row = "aeiou" * (val/5) + "x" * (val % 5)
  (n/val).times do
    print row
  end
  print "\n"
end

n = gets.to_i
f = false
u = v = 0
for i in 5..n
    if n%i == 0 && n/i >= 5
        f = true
        u = i
        v = n/i
        break
    end
end
if !f
    puts "-1"
    exit
end
s = "aiueo"*(u/5+1)
ans = ""
v.times do |i|
    ans += s[i%5,u]
end
puts ans


def build(n, m)
  'RGB'.chars.permutation.map do |cs|
    cs.flat_map{|c| n.times.map{(c * m).chars}}
  end
end

n, m = gets.split.map(&:to_i)
s = n.times.map{gets.strip.chars}
ts = build(n / 3, m) + build(m / 3, n).map(&:transpose)

if ts.include?(s)
  puts 'YES'
else
  puts 'NO'
end

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

a = []
n.times do
  a.push << (gets.strip.split(''))
end

rc = true
temp = []
n.times do |i|
  first = nil
  bool = true
  m.times do |j|
    if j == 0
      first = a[i][j]
      temp.push(a[i][j])
      next
    end
    if first != a[i][j]
      bool = false
      break
    end
  end
  if !bool
    rc = false
    break
  end
end

if rc
    if temp.count('R') == temp.count('G') && temp.count('G')  == temp.count('B')
        cnt = temp.count('R')
        if ((temp.rindex('R') - temp.index('R') + 1) == cnt) && ((temp.rindex('G') - temp.index('G') + 1) == cnt) && ((temp.rindex('B') - temp.index('B') + 1) == cnt)
            rc = true
        else
            rc = false
        end
    else
        rc = false
    end
end

rc1 = true
temp = []
m.times do |i|
  first = nil
  bool = true
  n.times do |j|
    if j == 0
      first = a[j][i]
      temp.push(a[j][i])
      next
    end
    if first != a[j][i]
      bool = false
      break
    end
  end
  if !bool
    rc1 = false
    break
  end
end

if rc1
    if temp.count('R') == temp.count('G') && temp.count('G')  == temp.count('B')
        cnt = temp.count('R')
        if ((temp.rindex('R') - temp.index('R') + 1) == cnt) && ((temp.rindex('G') - temp.index('G') + 1) == cnt) && ((temp.rindex('B') - temp.index('B') + 1) == cnt)
            rc1 = true
        else
            rc1 = false
        end
    else
        rc1 = false
    end
end

if rc || rc1
  puts 'YES'
else
  puts 'NO'
end

n, m = gets.split.map(&:to_i)
a, res = [], "NO"
n.times {a << gets.chomp.chars}
c = [a[0][0], a[n / 2][m / 2], a[n - 1][m - 1]].uniq
if c.size == 3
  n = n * m / 3
  s = [c[0] * n, c[1] * n, c[2] * n].join
  [a, a.transpose].each {|b| res = "YES" if b.flatten.join == s}
end
puts res

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

a = []
n.times do
  a.push << (gets.strip.split(''))
end

rc = true
temp = []
n.times do |i|
  first = nil
  bool = true
  m.times do |j|
    if j == 0
      first = a[i][j]
      temp.push(a[i][j])
      next
    end
    if first != a[i][j]
      bool = false
      break
    end
  end
  if !bool
    rc = false
    break
  end
end

if rc
    if temp.count('R') == temp.count('G') && temp.count('G')  == temp.count('B')
        rc = true
    else
        rc = false
    end
end

rc1 = true
temp = []
m.times do |i|
  first = nil
  bool = true
  n.times do |j|
    if j == 0
      first = a[j][i]
      temp.push(a[j][i])
      next
    end
    if first != a[j][i]
      bool = false
      break
    end
  end
  if !bool
    rc1 = false
    break
  end
end

if rc1
    if temp.count('R') == temp.count('G') && temp.count('G')  == temp.count('B')
        rc1 = true
    else
        rc1 = false
    end
end

if rc || rc1
  puts 'YES'
else
  puts 'NO'
end


n = gets.chomp.to_i
a = gets.chomp.split(/ /).map(&:to_i)
neighbor = {}
i = j = 0
n1 = n2 = 0
diff = 0
while true
    n1 = a[i]
    if i + 1 == n
        j = 0
    else
        j = i + 1
    end
    n2 = a[j]
    diff = (n1 - n2).abs
    pair = [i + 1, j + 1]
    if !neighbor.has_key? pair
        neighbor.store pair, diff
    else
        neighbor[pair] = diff
    end
    i += 1
    break if j == 0
end
min_h = neighbor.values.min
recon = neighbor.select { |k, v| v == min_h }.to_a[0][0]
puts recon.join(" ")

n=gets.to_i
data=gets.split.map(&:to_i)
data << data[0]

dif=999999
a=b=0
for i in 0..data.size-2
	tmp=(data[i]-data[i+1]).abs
	if dif>tmp
		a,b=i,((i+1)%n==0? 1 : i+2)
		dif=tmp
	end
end

puts "#{a+1} #{b}"

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
diff_arr = []
(1..n-1).each do |i|
	diff_arr << (arr[i] - arr[i-1]).abs
end
diff_arr << (arr[0]-arr[-1]).abs
min,min_ind = diff_arr.each_with_index.min

if min_ind == n -1
	a,b = n,1
else
	a,b = min_ind+1,min_ind + 2
end
puts "#{a} #{b}"

tot, min, min_index, input = gets.to_i, 20000, -1, gets.split(' ').map{|n| n.to_i}
input.each_cons(2).to_a.each_with_index { |(a, b), index| min, min_index = (a - b).abs, index if (((a - b).abs) < min) }
min_index = tot - 1 if (min > (input[0] - input[input.length - 1]).abs)
max_index = (min_index == tot - 1) ? 0 : min_index + 1
print (min_index + 1).to_s + ' ' + (max_index + 1).to_s

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
ans = 0
(1...n-1).each{|i| ans = i if (a[i]-a[i+1]).abs < (a[ans]-a[ans+1]).abs}
if (a[0]-a[n-1]).abs < (a[ans]-a[ans+1]).abs
    print n, " ", 1, "\n"
else
    print ans+1, " ", ans+2, "\n"
end

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
ans = 0
for i in 1...n-1
    ans = i if (a[i]-a[i+1]).abs < (a[ans]-a[ans+1]).abs
end
if (a[0]-a[n-1]).abs < (a[ans]-a[ans+1]).abs
    print n, " ", 1, "\n"
else
    print ans+1, " ", ans+2, "\n"
end

gets # ignore n

soldiers = gets.chomp.split.map(&:to_i)

soldiers << soldiers[0]
diff, pos = 1_001, 0

soldiers[0..-2].each_with_index do |s, i|
  tmp_diff = (s - soldiers[i + 1]).abs
  if tmp_diff < diff
    diff = tmp_diff
    pos = i
  end
end

puts pos + 1, (pos + 2 > soldiers.size - 1 ? 1 : pos + 2)

gets
h = gets.split(" ").map{|s| s.to_i}
n = h.length
h << h[0]
best = 0
(0...n).each do |i|
  if (h[i] - h[i+1]).abs < (h[best] - h[best+1]).abs
    best = i
  end
end

print (best%n)+1, " ", ((best+1)%n) + 1

n = gets.to_i
a = gets.split.map { |x| x.to_i }

min = 1_000
min_ind = nil
for i in 1...n
  diff = (a[i] - a[i - 1]).abs
  if diff < min
    min = diff
    min_ind = i
  end
end

if (a.last - a.first).abs < min
  min_ind = n
  next_min_ind = 1
else
  next_min_ind = min_ind + 1
end
puts "#{min_ind} #{next_min_ind}"

n, s, min, idx = gets.to_i, gets.strip.split.map(&:to_i), 0, idx

(print [1, n].join " "; exit) if s[0] == s[-1]
min, idx = (s[0] - s[-1]).abs, [1, n]

(0...n - 1).each do |i|
  a, b = i, i + 1
  (print [a + 1, b + 1].join " "; exit) if s[a] == s[b]
  m = (s[a] - s[b]).abs
  if m < min
    min = m
    idx = [a + 1, b + 1]
  end
end

print idx.join " "

n = gets.to_i

solds = gets.split.map{|i| i.to_i}

min = (solds[0] - solds[-1]).abs
q = n
w = 1
i = 1
while i < n do
    if min > (solds[i-1] - solds[i]).abs then
        min = (solds[i-1] - solds[i]).abs
        q = i
        w = i + 1
    end
    i += 1
end

puts [q,w].join(" ")

n = gets.chomp.to_i
s = gets.chomp
sa = s.split(/ /)
sa.map!{|x| x.to_i}

mindiff = (sa[1]-sa[0]).abs
minidx1 = 1
minidx2 = 2

for i in 1..n-1
	j = i+1
	j = 0 if j >= n
	wk = (sa[i]-sa[j]).abs
	if wk < mindiff
		mindiff = wk
		minidx1 = i+1
		minidx2 = j+1
	end
end

puts minidx1.to_s + " " + minidx2.to_s

n=gets.to_i
v=gets.split.map{|i|i.to_i}
v<<v[0]
m,r=1e9,0
(1..n).each{|i|
	t=(v[i]-v[i-1]).abs
	if m>t then
		m=t
		r=i
	end
}
print r," ",r%n+1

#!/usr/bin/ruby
gets;a=gets.split.map(&:to_i)
r=a.size.times.map{|i|[(a[i]-a[(i+1)%a.size]).abs,i]}.min.pop
puts "#{r+1} #{(r+1)%a.size+1}"

num_soldiers = gets.to_i
soldiers = gets.split.collect {|i| i.to_i}
best = (soldiers[0] - soldiers[-1]).abs
best_index = [1,num_soldiers]
for i in 0...num_soldiers-1
	temp = (soldiers[i+1] - soldiers[i]).abs
	if temp < best
		best = temp
		best_index = [i+1,i+2]
	end
end
puts best_index[0].to_s + " " + best_index[1].to_s

n=gets.to_i
a=gets.chomp.split.map(&:to_i)
x,y=1,1
z=[(a[0]-a[n-1]).abs]
for i in 0..n-2
	z.push((a[i]-a[i+1]).abs)
end
for i in 0..z.length-1
	if z[i]==z.min
		if i==0
			puts "#{n} 1"
			exit
		else
			puts "#{i} #{i+1}"
			exit
		end
	end
end

tot, min, min_index, input = gets.to_i, 20000, -1, gets.split(' ').map{|n| n.to_i}
input.each_cons(2).to_a.each_with_index { |(a, b), index| min, min_index = (a - b).abs, index if (((a - b).abs) < min) }
min_index = tot - 1 if (min > (input[0] - input[input.length - 1]).abs)
max_index = (min_index == tot - 1) ? 0 : min_index + 1
p (min_index + 1).to_s + ' ' + (max_index + 1).to_s


def read_array(to)
  gets.strip.split(' ').map(&to)
end

# [1,2], [3,4], (2,3)

gets.strip.to_i.times do
  _ = read_array(:to_i)
  a = read_array(:to_i)
  p = read_array(:to_i).map { |e| e - 1 }.sort
  ranges = []
  p.each_with_index do |e, i|
    if ranges.all? { |r| (r & [e,e+1]).empty? }
      ranges << [e,e+1]
    else
      ranges = ranges.select { |r| (r & [e,e+1]).empty? } +
        [  [[e,e+1], ranges.select { |r| !(r & [e,e+1]).empty? }].flatten.uniq.sort ]
    end
  end
  ranges.each do |r|
    a[r[0]..r[-1]] = a[r[0]..r[-1]].sort
  end
  puts a == a.sort ? "YES" : "NO"
end

n=gets.to_i
array = sets = []
(1..n).each do |x|
  a,b=gets.chomp.split(" ")
  array=gets.chomp.split(" ").map(&:to_i)
  sets=gets.chomp.split(" ").map(&:to_i)
  sets = sets.sort
  s = e = sets[0]
  sets.each do |set|
    if e != set
      array[s-1..e-1] = array[s-1..e-1].sort!
      s = set
    end
    e = set + 1
  end
  array[s-1..e-1] = array[s-1..e-1].sort!
  c = array[0]
  check = false
  array.each do |arr|
    if arr >= c
      c = arr
    else
      print("NO\n")
      check = true
      break
    end
  end
  print("YES\n") unless check
end

def read_array(to)
  gets.strip.split(' ').map(&to)
end

gets.strip.to_i.times do
  _ = read_array(:to_i)
  a = read_array(:to_i)
  p = read_array(:to_i).map { |e| e - 1 }
  ranges = []
  p.each do |e|
    found = false
    ranges.map! do |r|
      unless (r & [e, e + 1]).empty?
        found = true
        r = (r + [e, e + 1]).uniq.sort
      else
        r
      end
    end
    unless found
      ranges << [e, e+1]
    end
  end
  ranges.each do |r|
    a[r[0]..r[-1]] = a[r[0]..r[-1]].sort
  end
  puts a == a.sort ? "YES" : "NO"
end


n = gets.to_i
a = gets.split.map(&:to_i)
b = []
sum = 0
a.reverse_each do |val|
	b.unshift val + sum
	sum = b[0] - sum
end

puts b * " "

n = gets.chomp.to_i
arr = gets.chomp.split.map(&:to_i)
puts (arr.each_cons(2).map { |i, j| i + j } << arr.last).join(' ')

n=gets.to_i
x=gets.split.map{|e| e.to_i}
bt=0
ans=[]
x.reverse.each{|e|
	bi=e+bt
	ans<<bi
	bt=bi-bt
}
puts ans.reverse*" "

n = gets.strip.to_i
arr = gets.strip.split(" ").map(&:to_i)
ans = []
for i in 0..(arr.length-2) do
  ans << arr[i] + arr[i+1]
end
ans << arr.last
for i in 0..(ans.length-2) do
  print ans[i].to_s + " "
end
print ans.last

n=gets.to_i
a=gets.split.map &:to_i
a<<0
for i in 1..n
    print "#{a[i]+a[i-1]} "
end

#!/usr/local/rvm/rubies/ruby-2.3.0/bin/ruby

class Task1
  attr_accessor :crows, :n, :result

  def initialize()
    @n = 0
    @crows = []
    @result = []
  end

  def getData()
    tmp = []
    $stdin.each { |x| (x!="\n") ? tmp.push(x.chomp.split.map { |y| y.to_i }) : nil }
    @n = tmp[0][0]
    @crows = tmp[1]
  end

  def getResult()
    (0..@n-1).each do |x|
      if x == @n-1
        @result.push(@crows[x])
      else
        @result.push(@crows[x]+@crows[x+1])
      end
    end
  end

  def printResult()
    puts @result.join(' ')
  end

  def run()
    getData
    getResult
    printResult
  end

end
if __FILE__ == $0
  pc = Task1.new
  pc.run
end

def read_line
  gets.chomp.split.map &:to_i
end

n = read_line
a = read_line
b = []
y = 0

a.reverse_each do |x|
  b << x+y
  y = x
end

puts b.reverse

n = gets.to_i
a = gets.split.map(&:to_i)
puts n.times.map{|i| i == n - 1 ? a[i] : a[i] + a[i + 1]}.join(' ')

n=readline.to_i
a=readline.split.map(&:to_i)
a << 0
b = []
(0...n).each do |i|
  b << a[i]+a[i+1]
end
puts b.join(" ")

totalNumber = gets.chomp.to_i
numbers = gets.chomp.split(' ').map(&:to_i)

ans = [numbers[totalNumber - 1]]

(totalNumber - 2).downto(0).each do |i|
    ans << (numbers[i] + numbers[i + 1])
end

ans.reverse_each do |a|
    print "#{a} "
end


def solve(n, m, v)
    if m < n-1
        puts -1
        return
    end
    if m > (n-1)*(n-2)/2 + 1
        puts -1
        return
    end
    other = v % n + 1
    a = (1..n).to_a - [v, other]
    a = [v] + a + [other]
    puts "#{v} #{other}"
    m -= 1
    0.upto(n-2) do |i|
        break if m == 0
        (i+1).upto(n-2) do |j|
            break if m == 0
            puts "#{a[i]} #{a[j]}"
            m -= 1
        end
    end
end

n, m, v = *gets.split(' ').map { |s| s.to_i }
solve(n, m, v)


t = gets.to_i
t.times do
  n = gets.to_i
  k = []
  g = []
  da = Array.new(n, nil)
  pr = Array.new(n, nil)
  married = 0
  n.times do |i|
    k[i], *g[i] = gets.split.map(&:to_i)
    ok = false
    for j in 0...k[i] do
      if pr[g[i][j] - 1].nil?
        da[i] = g[i][j]
        pr[g[i][j] - 1] = i + 1
        ok = true
        break
      end
    end
    married += 1 if ok
  end
  if married < n
    puts "IMPROVE"
    puts "#{da.index(nil) + 1} #{pr.index(nil) + 1}"
  else
    puts "OPTIMAL"
  end
end

#!usr/bin/env ruby

# https://codeforces.com/problemset/problem/1327/B

require 'set'

t = gets.to_i

t.times do
  n = gets.to_i

  options = []

  n.times do
    options << gets.split(' ').map(&:to_i).drop(1)
  end

  choices = Set.new
  first_untaken_princess = nil

  options.each_with_index do |op, num|
    first_untaken_princess ||= num + 1 if op.size == 0

    op.each_with_index do |prince, n|
      unless choices.include? prince
        choices << prince
        break
      end
      if n + 1 == op.size
        first_untaken_princess ||= num + 1
      end
    end
  end

  if first_untaken_princess
    first_untaken_prince = 1

    while true do
      if choices.include? first_untaken_prince
        first_untaken_prince += 1
      else
        break
      end
    end
    puts "IMPROVE\n#{first_untaken_princess} #{first_untaken_prince}"
  else
    puts "OPTIMAL"
  end
end


n , m = gets.split.map(&:to_i)
s = ""
n.times do
    s = gets.strip
end
arr = s.split(".")

ans = 0
for i in arr
    if i.include? "B"
        ans +=1
    end
end
puts ans

rows, cols = gets.split(' ').map(&:to_i)
(rows-1).times { gets }

parts = 0
part = false
gets.chomp.each_char do |ch|
  if ch == 'B'
    part = true if !part
  else
    parts += 1 if part
    part = false
  end
end

parts += 1 if part

puts parts

n , m = gets.split.map(&:to_i)
s = ""
n.times do
    s = gets.strip
end
arr = s.split(".")
puts arr
ans = 0
for i in arr
    if i.include? "B"
        ans +=1
    end
end
puts ans


n = gets.chomp.to_i
c = gets.chomp.split(" ").map{|e| e.to_i}
tables = []
map = {}
1.upto(n) do |y|
  col = gets.chomp.split(" ").map{|e| e.to_i}
  tables << col

  col.each_with_index do |num, x|
    map[num] = [y, x + 1]
  end
end
ans = []
k = 0
idx = 1
for i in 1 .. n do
  for j in 1 .. c[i - 1] do
    k += 1
    next if tables[i - 1][j - 1] == k

    y = map[k].first
    x = map[k].last

    ans << [i, j, y, x]

    # swap
    tmp = tables[i - 1][j - 1]
    tables[i - 1][j - 1] = k
    tables[y - 1][x - 1] = tmp

    map[k] = [i, j]
    map[tmp] = [y, x]
  end
end

puts ans.length
ans.each do |line|
  puts line.join(" ")
end

n = gets.to_i
c = gets.chomp.split.map(&:to_i)
s = c.inject(&:+)
a = []
l = []
o = []
n.times do |i|
    a[i] = gets.chomp.split.map(&:to_i)
    0.upto(c[i]-1) do |j|
        l[a[i][j]] = [i,j]
        o[i+j] = [] if o[i+j].nil?
        o[i+j].push([i,j])
    end
end

c = 0
moves = []
curr = 0
1.upto(s) do |i|
    curr = curr + 1 if o[curr].empty?
    tr, tc = o[curr].pop
    fr, fc = l[i]
    unless tr == fr && tc == fc
        moves.push([tr+1,tc+1,fr+1,fc+1])
        l[a[fr][fc]] = [tr,tc]
        l[a[tr][tc]] = [fr,fc]
        a[fr][fc], a[tr][tc] = a[tr][tc], a[fr][fc]
        c += 1
    end
end
puts c
c.times do |i|
    puts moves[i].join(" ")
end

n = gets.to_i
gets

a = Array.new(n) {
  gets.split.map{|x| x.to_i - 1}
}

b = []
n.times {|i|
  a[i].each_index {|j|
    b << [i, j]
  }
}

c = []
50.times {
  n.times {|i|
  a[i].each_index {|j|
    t = a[i][j]
    next if b[t] == [i, j]
    x, y = *b[t]
    a[i][j], a[x][y] = a[x][y], a[i][j]
    c << [i, j, x, y].map(&:succ)
  }
}
}
p c.size
puts c.map{|i| i * ' '}

#!/usr/bin/env ruby
n = gets.to_i
gets
t = []
n.times do
  t << gets.split.map(&:to_i)
end
results = []
n.times do |i|
  t[i].length.times do |j|
    mi, mj = i, j
    (i..(n - 1)).each do |ni|
      (j..(t[ni].length - 1)).each do |nj|
        if t[ni][nj] < t[mi][mj]
          mi, mj = ni, nj
        end
      end
    end
    if mi != i || mj != j
      results.push([i, j, mi, mj])
      t[i][j], t[mi][mj] = t[mi][mj], t[i][j]
    end
  end
end
puts results.length
results.each do |r|
  puts r.map { |x| x + 1 }.join(" ")
end

n = gets.to_i
gets

a = Array.new(n) {
  gets.split.map{|x| x.to_i - 1}
}

b = []
n.times {|i|
  a[i].each_index {|j|
    b << [i, j]
  }
}

c = []
n.times {|i|
  a[i].each_index {|j|
    t = a[i][j]
    next if b[t] == [i, j]
    x, y = *b[t]
    a[i][j], a[x][y] = a[x][y], a[i][j]
    c << [i, j, x, y].map(&:succ)
  }
}
p c.size
puts c.map{|i| i * ' '}

n = gets.chomp.to_i
c = gets.chomp.split(" ").map{|e| e.to_i}
tables = []
map = {}
1.upto(n) do |y|
  col = gets.chomp.split(" ").map{|e| e.to_i}
  tables << col

  col.each_with_index do |num, x|
    map[num] = [y, x + 1]
  end
end
ans = []
p map
k = 0
idx = 1
for i in 1 .. n do
  for j in 1 .. c[i - 1] do
    k += 1
    next if tables[i - 1][j - 1] == k

    y = map[k].first
    x = map[k].last

    ans << [i, j, y, x]

    # swap
    tmp = tables[i - 1][j - 1]
    tables[i - 1][j - 1] = k
    tables[y - 1][x - 1] = tmp

    map[k] = [i, j]
    map[tmp] = [y, x]
  end
end

puts ans.length
ans.each do |line|
  puts line.join(" ")
end


n, d = gets.chomp.split.map(&:to_i)
x = gets.chomp.split.map(&:to_i)
a = {}
x.each do |i|
	a[i-d] =0; a[i+d] = 0;
end

ac = 0
a.keys.each do |i|
	flag = 1
	x.each do |j|
		flag = 0 if ((i-j).abs < d)
	end

	ac += flag
end

print ac

require 'set'

def solution
  m, d = read_ints
  cs = read_ints

  cities = Set.new

  cs.each.with_index do |c, i|
    before = c - d
    after = c + d

    cities << before if i == 0 || before - cs[i-1] >= d
    cities << after if i == cs.size-1 || cs[i+1] - after >= d
  end

  puts cities.size

end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

N, D = gets.split.map(&:to_i)
xs = gets.split.map(&:to_i)

ans = 2
(N-1).times do |i|
    if xs[i] < xs[i+1] - 2*D
        ans += 2
    elsif xs[i] == xs[i+1] - 2*D
        ans += 1
    end
end
puts ans

n, d = gets.strip.split.map(&:to_i)
ary = gets.strip.split.map(&:to_i)
ary.sort!

possible_locations = []
ary.each do |c|
  possible_locations << (c+d)
  possible_locations << (c-d)
end
possible_locations.uniq!
ans = 0
possible_locations.each do |nc|
  # puts nc
  # puts ary.map{|oc| (nc-oc).abs >= d }.inspect
  if ary.map{|oc| (nc-oc).abs }.min == d
    # puts nc
    ans += 1
  end
end
puts ans

$n,$d=gets.split
$n,$d=$n.to_i,$d.to_i
a=gets.split
i,ans,len=0,0,a.length
def test(val,id)
	yield id,val+$d
	yield id,val-$d
end
a.each do |c|
	test(c.to_i,i){|id,pos|
			flag=1
			if id>0 and pos-a[id-1].to_i<$d
			flag=0
			end
			if id<len-1 and a[id+1].to_i-pos<=$d
			flag=0
			end
			if flag!=0
			ans=ans+1
			end
			}
	i=i+1
end
puts ans
#a.each do |c| if c%2==0 then print c-1
#	   else print c
#	   end
#	   print " "
#	end

n,d = STDIN.readline.strip.split(/\s+/).map(&:to_i)
coordinates = STDIN.readline.strip.split(/\s+/).map(&:to_i)

count = 2
(0..coordinates.length - 2).each do |i|
  if coordinates[i] + d == coordinates[i+1] - d
    count += 1
  else
    if coordinates[i] + d < coordinates[i+1] - d
      count += 1
    end
    if coordinates[i+1] - d > coordinates[i] + d
      count += 1
    end
  end
end

puts count

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

count = 2

cities.each_cons(2) do |city1, city2|

  if city1 + d < city2 - d
    count += 2
  elsif city1 + d == city2 - d
    count += 1
  end

end

puts count

n,d = STDIN.readline.strip.split(/\s+/).map(&:to_i)
coordinates = STDIN.readline.strip.split(/\s+/).map(&:to_i)

count = 2
(0..coordinates.length - 2).each do |i|
  if coordinates[i] + d == coordinates[i+1] - d
    count += 1
  else
    if coordinates[i] + d < coordinates[i+1]
      count += 1
    end
    if coordinates[i-1] - d > coordinates[i]
      count += 1
    end
  end
end

puts count

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

count = 2

cities.each_cons(3) do |city1, city2, city3|

  if city2 - d > city1 + d
    count += 1
  end
  if city2 - d == city1 + d
    count += 1
  end
  if city2 + d < city3 - d
    count += 1
  end

end

if cities.count == 1
  puts '2'
else
  if cities[0] + d < cities[1] - d
    count += 1
  end
  if cities[n - 2] + d < cities[n - 1] - d
    count += 1
  end

  puts count
end


n = STDIN.readline.to_i
numbers = STDIN.readline.strip.split(/\s+/).map(&:to_i)

indices = []
(0..numbers.length - 1).each do |i|
  indices << i if numbers[i] == 1
end
indices << numbers.length

outputs = []
(0..indices.length - 2).each do |i|
  outputs << indices[i + 1] - indices[i]
end

puts outputs.length
puts outputs.join(' ')

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

z = 0
count = 0
ac = []
a.each_with_index do |x, i|
	if x == z + 1
		z += 1
		next
	else
		ac << z
		count +=1
		z = x
	end
end

ac << z
puts count + 1
puts ac.join(" ")

n = gets.to_i
a = gets.split.map(&:to_i)
ans = a.chunk_while { _1 < _2 }.map(&:size)
puts ans.size, ans.join(" ")

gets;a=(gets.split.map(&:to_i)+[1]).each_cons(2).select{|x,y|x>=y}.map{|x,y|x};p a.size;puts a*' '

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

stair_sizes = []
crt = 0

numbers.each do |n|
if n == crt + 1
  crt += 1
else
  stair_sizes.push crt
  crt = 1
end
end
stair_sizes.push crt

p stair_sizes.size
puts stair_sizes.join ' '

N = gets.to_i
as = gets.split.map(&:to_i)
ans = []
as.each do |a|
    if a == 1
        ans.push(a)
    else
        ans[-1] = a
    end
end
puts ans.length
puts ans.join(' ')

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

if t==1
  puts "1\n1"
else
  steps = []
  0.upto(t-2).each do |x|
    steps << ary[x] if ary[x+1]==1
  end
  steps << ary[-1]
  puts steps.size
  puts steps.join(" ")
end

number_of_elems = gets.strip.to_i
array = gets.strip.split.to_a.collect { |val| val.to_i }
answer = []
(1...array.size).each do |i|
  answer << array[i-1] if array[i] <= array[i-1]
end
answer << array[-1]
puts answer.size
puts answer.join(' ')

n = gets.to_i
steps = gets.split.map(&:to_i)
stepss = []
t = []
steps.each do |step|
  if step == 1
    stepss << t unless t.empty?
    t = [1]
  else
    t << step
  end
end
stepss << t
puts stepss.count
puts stepss.map { |a| a.max }.join(" ")

x = gets.chomp.to_i
stairways =  gets.strip.split.map(&:to_i)
res = 1
arr = Array.new
for i in 1 ... x
 if stairways[i] > stairways[i-1]
   res +=1
 else if !(stairways[i] > stairways[i-1])
        arr.push(res)
        res=1
      end
 end
end
arr.push(res)
puts arr.size
puts arr.join(' ')

number = gets().to_i
list = gets().chomp.split.map(&:to_i)

ans = []
count = 0
for i in 0...list.length

  if(list[i+1] == 1 || i == list.length-1)
    ans.push(list[i])
  end
  if(list[i] == 1)
    count = count+1
  end
end

puts count
for i in 0...ans.length
  print ans[i]
  print " "
end

gets
t = []
t << gets.split.map{|x| x.to_i}.inject(0) do |s, x|
    t << s if x == 1 && s > 0
    x
end
puts(t.size, t.join(' '))

n = gets
input = gets

#TEST INPUTS
# input = "1 2 3 1 2 3 4"
#2
#3 4

# input = "1 1 1 1"
# 4
# 1 1 1 1

# input = "1 2 3 4 5"
# 1
# 5

# input = "1 2 1 2 1"
# 3
# 2 2 1

counts = input.split(" ").map(&:to_i)

last = 0
output = []
counts.each.with_index do |a_count, index|
	if a_count <= last
		#a new max step is added
		output << counts[index - 1]
	end
	last = a_count
end

output << counts.last

puts output.count
puts output.join(" ")

t = gets.to_i
stairs = gets.chomp.split(" ").map(&:to_i)

no = 1
arr = []
prev = 0

stairs.each do |x|
  if x<=prev
    no+=1
    arr.push prev
  end

  prev = x
end

arr.push stairs[-1]
puts no
print arr*" "
puts

gets
n = gets.split.map { |x| x.to_i }
k = []
c = 0
n[1, n.size-1].each_with_index do |x,i|
  if x == 1
    k << n[i]
    c += 1
  end
end
k.push(n.last)
c += 1
puts c
puts(k.join ' ')

N = gets.to_i
as = gets.split.map(&:to_i)
ans = []
as.each do |a|
    if a == 1
        ans.push(a)
    else
        ans[-1] = a
    end
end
puts ans.join(' ')


def find_ugliness(heights, building_num)
  sum = heights.sum
  remainder = sum.modulo(building_num)

  # move makes no effect when difference is 0 or 1
  if remainder == 0
    return 0
  else
    return 1
  end

end

test_case_num = gets.chomp.to_i

ugliness_array = []

test_case_num.times do |idx|
  # read input
  building_num = gets.chomp.to_i
  heights = gets.chomp.strip.split(" ").map!(&:to_i)

  ugliness_array.append(find_ugliness(heights, building_num))

end

ugliness_array.each do |ugliness_single|
  puts ugliness_single
end

t = gets.chomp
for i in 0..t.to_i-1
    n = gets.chomp.to_i
    arr = gets.chomp.split(" ")
    arr = arr.map{|n| n.to_i}
    if arr.sum % n == 0
        puts 0
    else
        puts 1
    end
end


#idea_copied_From_ToErr_Vaia

def call(ind,a)
	r = 30**9
	l = -2
	for i in 0...a.size
		if(i != ind)
			r = [r,a[i].r].min
			l = [l,a[i].l].max
		end
	end
	return r-l
end

pair = Struct.new(:l,:r)
a = []
n = STDIN.readline.to_i
for i in 1..n
	p = STDIN.readline.split(" ")
	s = pair.new(p[0].to_i,p[1].to_i)
	a << s
end
a.sort! {|z,b| z.r <=> b.r}

r = 30**9
l = -1
for i in 0...n
	if a[i].r < r
		r = a[i].r
		ri = i
	end
	if a[i].l > l
		l = a[i].l
		li = i
	end
end

ans = 0
ans = [ans,call(li,a)].max
ans = [ans,call(ri,a)].max

print ans

#idea_copied_From_ToErr_Vaia
#but_not_sorted
def call(ind,a)
	r = 30**9
	l = -2
	for i in 0...a.size
		if(i != ind)
			r = [r,a[i].r].min
			l = [l,a[i].l].max
		end
	end
	return r-l
end

pair = Struct.new(:l,:r)
a = []
n = STDIN.readline.to_i
for i in 1..n
	p = STDIN.readline.split(" ")
	s = pair.new(p[0].to_i,p[1].to_i)
	a << s
end
# a.sort! {|z,b| z.r <=> b.r}

r = 30**9
l = -1
for i in 0...n
	if a[i].r < r
		r = a[i].r
		ri = i
	end
	if a[i].l > l
		l = a[i].l
		li = i
	end
end

ans = 0
ans = [ans,call(li,a)].max
ans = [ans,call(ri,a)].max

print ans


a,b,n = gets.split.collect{|t| t.to_i}

solved = false
for i in 0..9
	solved |= (10*a + i) % b == 0
	if solved
		print a, i
		break
	end
end

if solved
	(n-1).times do |i|
		print 0
	end
else
	print -1
end
puts

# your code goes here
a, b, n = gets.split(' ').map { |_| _.to_i }
for i in 0..9
    d=a*10+i
    if d%b == 0
        print "#{d}"
        for j in 0..n-2
            print 0
        end
        exit
    end
end
puts -1

st = gets.split
a = st[0].to_i
b = st[1].to_i
n = st[2].to_i

i = 1
while i <= n && (a != -1)
    if a % b == 0 then
        a = (a.to_s + "0"*(n-i+1)).to_i
        i = n + 1
    else
        t = true
        k = 0
        while k < 10 && t do
            q = (a.to_s + k.to_s).to_i
            if q % b  == 0 then
                t = false
                a = q
            end
            k += 1
        end
        if t then
            a = -1
        end
    end
    i += 1
end

puts a

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

def op(a, b)
  for i in 0 .. 9 do
    c = a * 10 + i
    return c if c % b == 0
  end

  -1
end

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

c = op(a, b)

if c == -1
  puts -1
else
  puts c.to_s + "0" * (a.to_s.length + n - c.to_s.length)
end

a, b, n = gets.split.map(&:to_i)
until n == 0
  a *= 10
  if a%b != 0
    if b-(a%b) > 9
      print -1
      exit
    else
      a += b-(a%b)
    end
  else
    a *= 10**(n-1)
    break
  end
  n -= 1
end
print a

c=gets.chomp.scan(/\w+/)
t=-1
for i in 0..9 do
    if (c[0].to_i*10+i)%c[1].to_i==0 then t=i end
end
if (t==-1) then puts "-1"
else
print c[0],t
2.upto(c[2].to_i) {print 0}
puts
end

#!/usr/bin/ruby1.9

in_file = STDIN

a, b, n = in_file.readline.split.collect {|s| s.to_i }

(0..9).each do |d|
	candidate = 10*a+d
	if candidate%b == 0
		padding = Array.new(n-1, '0').join('')
		puts '%s%s' % [candidate, padding]
		exit
	end
end
puts -1

infile = STDIN
a, b, n = infile.readline.split.map { |x| x.to_i }

append = (0..9).find { |i| (a*10+i)%b == 0 }
if append
  puts a.to_s + append.to_s + "0"*(n-1)
else
  puts -1
end

a,b,n = gets.split
a = a.to_i
b = b.to_i
n = n.to_i

def solve(a,b,n)
	return "" << (a.to_s + "0"*n) if a*10%b==0

	if (a*10 + (b - a*10%b) ).to_s.start_with? a.to_s then
		return solve(a*10+(b-a*10%b),b,n-1);
	else
		return -1;
	end
end

puts solve(a,b,n)

def run(line)
  a, b, n = line.split(" ").map {|x| x.to_i}

  prev_mod = a % b
  val = [a]
  n.times do |i|
    flag = true
    10.times do |j|
      mod = (prev_mod * 10 + j) % b
      if mod == 0
        flag = false
        prev_mod = mod
        val << j
        break
      end
    end

    return "-1" if flag
  end

  val.join
end

if $0 == __FILE__
  ARGF.readlines.each do |line|
    puts run(line)
  end
end

a,b,n = gets.split
a = a.to_i
b = b.to_i
n = n.to_i

w = (b-a*10%b) %b

if w < 10 then
	puts "" << a.to_s << w.to_s << "0"*(n-1)
else
	puts -1
end
exit

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

ret = -1
(0..9).each do |d|
  if (a * 10 + d) % b == 0 then
    ret = d
  end
end

if ret == -1 then
  puts "-1"
else
  ret =  (a * 10 + ret).to_s + "0" * (n - 1)
  puts ret
end

a, b, n = gets.split.map { |x| x.to_i }

begin
	10.times do |i|
		if (a * 10 + i) % b == 0
			raise i.to_s
		end
	end
rescue Exception => x
	puts a.to_s + x.message + '0' * (n - 1)
else
	puts -1
end

a=gets.chomp.split(" ")
f=a[0]
b=a[1].to_i
n=a[2].to_i
trig=0
0.upto(9) do |i|
if ((f+"#{i}").to_i)%b==0
f=(f+"#{i}")
n-=1
trig=1
break
end
end
if trig==1
f=f+"0"*n
puts f
else
puts "-1"
end

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

append = (0..9).find { |i| (a * 10 + i) % b == 0 }
if append
  puts (a * 10 + append).to_s + "0" * (n - 1)
else
  puts -1
end

a, b, n = gets.split.collect{|i| i.to_i}
(0...10).each do |i|
    if (a*10+i)%b==0
        print a, i, "0"*(n-1), "\n"
        exit
    end
end
puts -1

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

a*=10

unless a%b==0
  if b-a%b<10
    a+=b-a%b
  else
    puts '-1'
    exit
  end
end

a*=(10**(n-1))
puts a

st = gets.split
a = st[0].to_i
b = st[1].to_i
n = st[2].to_i

i = 1
while i <= n && (a != -1)
    t = true
    k = 1
    while k < 10 && t do
        q = (a.to_s + k.to_s).to_i
        if q % b  == 0 then
            t = false
            a = q
        end
        k += 1
    end
    if t then
        a = -1
    end
    i += 1
end

puts a

st = gets.split
a = st[0].to_i
b = st[1].to_i
n = st[2].to_i

i = 1
while i <= n && (a != -1)
    if a % b == 0 then
        a = (a.to_s + "0"*(n-i+1)).to_i
        i = n + 1
    else
        t = true
        k = 1
        while k < 10 && t do
            q = (a.to_s + k.to_s).to_i
            if q % b  == 0 then
                t = false
                a = q
            end
            k += 1
        end
        if t then
            a = -1
        end
    end
    i += 1
end

puts a

st = gets.split
a = st[0].to_i
b = st[1].to_i
n = st[2].to_i

i = 1
while i <= n && (a != -1)
    if a % b == 0 then
        a = (a.to_s + "0"*(n-i)).to_i
        i = n + 1
    else
        t = true
        k = 1
        while k < 10 && t do
            q = (a.to_s + k.to_s).to_i
            if q % b  == 0 then
                t = false
                a = q
            end
            k += 1
        end
        if t then
            a = -1
        end
    end
    i += 1
end

puts a

a, b, n = gets.split.map(&:to_i)
until n == 0
  a *= 10
  if a%b != 0
    if b-(a%b) > 9
      print -1
      exit
    else
      a += b-(a%b)
    end
  else
    a *= 10**n
    break
  end
  n -= 1
end
print a

a, b, n = gets.split.map(&:to_i)
until n == 0
  a *= 10
  if b-(a%b) > 9
    print -1
    exit
  else
    a += b-(a%b)
  end
  n -= 1
end
print a

#!/usr/bin/ruby1.9

in_file = STDIN

a, b, n = in_file.readline.split.collect {|s| s.to_i }

left = n

(1..n).each do |i|
	best = -1
	(0..9).each do |d|
		candidate = 10*a + d
		if candidate%b == 0
			best = candidate
			break
		end
	end
	if best == -1
		puts -1
		exit
	end
	a = best
	left -= 1
	if i == 5040
		s = a.to_s
		tail = s[-5040..-1]
		while left >= 5040
			s += tail
			left -= 5040
		end
		if left > 0
			s += tail[-left..-1]
		end
		puts s
		exit
	end
end
puts  a

infile = STDIN
# infile = File.open("1.txt")
a, b, n = infile.readline.split.map { |x| x.to_i }
append = (0..9).find { |i| (a*10+i)%b == 0 }
puts a.to_s + append.to_s + "0"*(n-1); exit if append
puts -1

a, b, n = gets.split.map(&:to_i)
o = a
i = 1
8.times do
  break if a%b == 0
  d = (a.to_s + i.to_s).to_i
  a = d if d%b == 0
  i += 1
end
puts a%b == 0 ? a.to_s.ljust(n+1, "0") : -1

a, b, n = gets.split.map(&:to_i)
o = a
i = 1
8.times do
  break if a%b == 0
  d = (a.to_s + i.to_s).to_i
  a = d if d%b == 0
  i += 1
end
puts a%b == 0 ? a * (10**(n-1)) : -1

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

ret = -1
(0..9).each do |d|
  if (a * 10 + d) % b == 0 then
    ret = d
  end
end

if ret == -1 then
  puts "-1"
elsif b <= 5 or b == 10 or n == 1 then
  ret =  (a * 10 + ret).to_s + b.to_s * (n - 1)
  puts ret
else
  puts "-1"
end

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

ret = -1
(0..9).each do |d|
  if (a * 10 + d) % b == 0 then
    ret = d
  end
end

if ret == -1 then
  puts "-1"
elsif b <= 5 or b == 10 or n == 1 then
  b = b % 10
  ret =  (a * 10 + ret).to_s + b.to_s * (n - 1)
  puts ret
else
  puts "-1"
end

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

ret = -1
(0..9).each do |d|
  if (a * 10 + d) % b == 0 then
    ret = d
  end
end

if ret == -1 then
  puts "-1"
elsif b <= 5 or n == 1 then
  ret =  (a * 10 + ret).to_s + b.to_s * (n - 1)
  puts ret
else
  puts "-1"
end

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

n.times do
  found = 1.upto(9).find do |i|
    if (a * 10 + i) % b == 0
      a = a * 10 + i
    end
  end
  if !found
    puts -1
    exit
  end
end

p a

a,b,n = gets.chomp.split(" ").collect {|x| x.to_i}
if b>10 and n>1
 puts -1
 exit
end

if b>10
	p = -1
	(0..9).each{|k|
		if (10*a+k)%b == 0
			p=k
		end
	}
	if p==-1
		puts -1
	else
		puts (10*a+p)
	end
end

if b<=10
	p = -1
	(0..9).each{|k|
		if (10*a+k)%b == 0
			p=k
		end
	}
	a=10*a+p
	s = (b%10).to_s*(n-1)
	puts "#{a}#{s}"
end

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

a*=10

unless a%b==0
  if a-a%b<10
    a+=a-a%b
  else
    puts '-1'
    exit
  end
end

a*=(10**(n-1))
puts a

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

(1..n).each do
  a*=10
  unless a%b==0
    if b-a%b<10
      a+=b-a%b
    else
      puts '-1'
      exit!(0)
    end
  end
end

puts a

a,b,n = gets.split
a = a.to_i
b = b.to_i
n = n.to_i

def solve(a,b,n)
	return "" << (a.to_s + "0"*n) if a%b==0

	if (a*10 + (b - a*10%b) ).to_s.start_with? a.to_s then
		return solve(a*10+(b-a*10%b),b,n-1);
	else
		return -1;
	end
end

puts solve(a,b,n)

a,b,n = gets.split
a = a.to_i
b = b.to_i
n = n.to_i

w = (b-a*10%b) %b

if w < 10 then
	puts a.to_s << w <<"0"*(n-1)
else
	puts -1
end

a,b,n = gets.split
a = a.to_i
b = b.to_i
n = n.to_i

def solve(a,b,n)
	return "" << (a.to_s + "0"*n) if a%b==0

	if (a*10 + (b - a%10%b) ).to_s.start_with? a.to_s then
		return solve(a*10+(b-a*10%b),b,n-1);
	else
		return -1;
	end
end

puts solve(a,b,n)

a,b,n = gets.split
n.to_i.times do |i|
	0.upto(9) do |u|
		a << u.to_s
		if a.to_i%b.to_i==0 then
			puts a
			exit
		end
	end
end
puts "-1"

a,b,n = gets.split.collect{|t| t.to_i}

solved = false
for i in 0..9
	solved |= (10*a + i) % b == 0
	if solved
		print a, i
		break
	end
end

if solved
	n.times do |i|
		print 0
	end
else
	print -1
end
puts