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

sub=0
idx=0
k.times.each do |i|
  while idx<n && (ary[idx]-sub)==0 do
    idx+=1
  end
  v = 0
  if idx >= n
    v = 0
  else
    v = (ary[idx]-sub)
    sub += v
  end
  puts v
end

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
a.sort!
j = a.index{|x| x > 0}
if j == nil
    j = a.size
end
pj = 0
k.times do
    if j == a.size
        puts 0
        next
    end
    puts(a[j] - pj)
    pj += (a[j] - pj)

    i = j + 1
    i += 1 while i < a.size && a[i] == a[j]
    j = i
end

nk = gets.chomp.split(" ").map(&:to_i)
arr = gets.chomp.split(" ").map(&:to_i)
n = nk[0]
k = nk[1]
tosubtr = 0

arr.sort!
#p arr

i = 0
k.times do

  if i >= n
    puts 0
  else
    x = arr[i]

    while ((x-tosubtr)==0 and i<n-1)
      i += 1
      x = arr[i]
    end

    if i==n
      puts 0
      continue
    end

#    puts "#{x} #{tosubtr}"

    if x-tosubtr<=0
      puts 0
    else
      puts x-tosubtr
      tosubtr = x
    end

  end
  i += 1
end

n,k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort
i = 0
sum = 0
k.times do |j|
    while i < n && a[i]-sum <= 0
        i += 1
        #break if i == n
    end
    if i == n
        puts 0
    else
        puts a[i]-sum
        sum = a[i]
    end
end

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

arr.sort!
index = 0
sbst = 0
k.times do
    if index >= n
        puts 0
    else
        min = arr[index] - sbst
        puts min
        sbst += min
        for i in index..(n - 1) do
            if arr[i] - sbst > 0
                index = i
                break
            else i == n - 1
                index = n
            end
        end
    end
end

n,k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort
i = 0
sum = 0
k.times do |j|
    while i < n && a[i]-sum <= 0
        i += 1
        #break if i == n
    end
    if i == n
        puts 0
    else
        puts a[i]-sum
        sum += a[i]
    end
end

nk = gets.chomp.split(" ").map(&:to_i)
arr = gets.chomp.split(" ").map(&:to_i)
n = nk[0]
k = nk[1]
tosubtr = 0

arr.sort!
#p arr

i = 0
k.times do

  if i >= n
    puts 0
  else
    x = arr[i]

    while ((x-tosubtr)==0 and i<n-1)
      i += 1
      x = arr[i]
    end

    if i==n
      puts 0
      continue
    end

#    puts "#{x} #{tosubtr}"

    if x-tosubtr<=0
      puts 0
    else
      puts x-tosubtr
      tosubtr += x
    end

  end
  i += 1
end


#!/usr/bin/env ruby

usb, ps2, usb_ps2 = gets.split.map {|v| v.to_i}
mouses_n = gets.to_i

mouses_prices = { USB: [],
                  :'PS/2' => []}

mouses_n.times do |n|
  price, type = gets.split
  mouses_prices[type.to_sym] << price.to_i
end

mouses_prices[:USB].sort!
mouses_prices[:'PS/2'].sort!

total_mouse = 0
total_price = 0

usb.times do
  break if mouses_prices[:USB].empty?
  total_price += mouses_prices[:USB].shift
  total_mouse += 1
end

ps2.times do
  break if mouses_prices[:'PS/2'].empty?
  total_price += mouses_prices[:'PS/2'].shift
  total_mouse += 1
end

usb_ps2_remainings = mouses_prices[:USB] + mouses_prices[:'PS/2']
usb_ps2_remainings.sort!

usb_ps2.times do
  break if usb_ps2_remainings.empty?
  total_price += usb_ps2_remainings.shift
  total_mouse += 1
end

puts "#{total_mouse} #{total_price}"

a,b,c=gets.split.map(&:to_i)
r=s=0
H=Hash.new{|h,k|h[k]=[]}
gets.to_i.times{
	x,y=gets.split
	H[y]<<x.to_i
}
H['USB'].sort!
a=H['USB'].size if a>H['USB'].size
r+=a
s+=H['USB'].shift(a).reduce(0,:+)
H['PS/2'].sort!
b=H['PS/2'].size if b>H['PS/2'].size
r+=b
s+=H['PS/2'].shift(b).reduce(0,:+)
q=(H['USB']+H['PS/2']).sort
c=q.size if c>q.size
r+=c
s+=q.shift(c).reduce(0,:+)
puts '%d %d'%[r,s]

#!/usr/bin/env ruby

usb, ps2, usb_ps2 = gets.split.map {|v| v.to_i}
mouses_n = gets.to_i

mouses_prices = { USB: [],
                  :'PS/2' => []}

mouses_n.times do |n|
  price, type = gets.split
  mouses_prices[type.to_sym] << price.to_i
end

mouses_prices[:USB].sort!
mouses_prices[:'PS/2'].sort!

total_mouse = 0
total_price = 0

usb.times do
  break if mouses_prices[:USB].empty?
  total_price += mouses_prices[:USB].shift
  total_mouse += 1
end

ps2.times do
  break if mouses_prices[:'PS/2'].empty?
  total_price += mouses_prices[:'PS/2'].shift
  total_mouse += 1
end

usb_ps2.times do
  if !mouses_prices[:USB].empty?
    total_price += mouses_prices[:USB].shift
    total_mouse += 1
    next
  end
  if !mouses_prices[:'PS/2'].empty?
    total_price += mouses_prices[:'PS/2'].shift
    total_mouse += 1
    next
  end
  break
end

puts "#{total_mouse} #{total_price}"


t = gets.chomp.to_i

results = []

(1..t).each do |_t|

  a = gets.chomp.split(' ').map{|a| a.to_i}

  a.sort!.reverse!

  a,b = a

 if a > 2*b
  results[_t] = b
 else
  results[_t] = (a + b) /3
 end

end

results.each do |res|
  puts res
end

def solve(a, b)
  min = [a, b].min
  max = [a, b].max
  [(max + min) /3, min].min
end

amount = gets.chomp.to_i
amount.times do
  input = gets.chomp.split(" ")
  puts solve(input[0].to_i, input[1].to_i)
end

t = gets.chomp.to_i

results = []

(1..t).each do |_t|

  a = gets.chomp.split(' ').map{|a| a.to_i}

  a.sort!.reverse!

  if a[0] % 2 == 0
    if a[1] - (a[0]/2 - 1) >= 4
      results[_t] = a[0] / 2 + 1
    else
      results[_t] = [a[0]/2, a[1]].min
    end
  else
    x = a[1] - a[0] / 2

    if x > 1
      results[_t] =a[0]/2 + 1
    else
      results[_t] = [a[0]/2, a[1]].min
    end
  end

end

results.each do |res|
  puts res
end

def compute(a,b,x)
  return b if b <= x

  a -= 2*x
  b -= x

  return x if [a,b].max < 2

  return x if [a,b].min < 1

  return x + 1 if [a,b].max < 4

  return x + 1 if [a,b].min < 2

  x + 2
end

t = gets.chomp.to_i

results = []

(1..t).each do |_t|

  a = gets.chomp.split(' ').map{|a| a.to_i}

  a.sort!.reverse!

  x = a[0] / 2

  results[_t] = [compute(a[0], a[1], x), compute(a[0], a[1], x-1)].max

end

results.each do |res|
  puts res
end


$stdout.sync = true
while line=gets
  puts "NO"
end

$stdout.sync = true

puts "NO" while gets

while line = gets
    puts "NO"
    STDOUT.flush
end


s = gets
m = gets.to_i
while m>0
    l,r,k = gets.split(" ").map(&:to_i)
    new_s = s[l-1,(r-l+1)]
    pre = s[0,l-1]
    post = s[r,s.size]
    k = k%(r-l+1)
    ass = new_s[new_s.size-k,new_s.size]
    arr = ass + new_s.chomp(ass)
    s = pre + arr + post
    m-=1
end
puts s

s = gets.to_s
m = gets.to_i

m.times do
  l, r, k = gets.split(' ').map{ |x| x.to_i }
  l -= 1
  r -= 1
  k = k % (r-l+1)
  s[l..r] = s[r-k+1..r] + s[l..r-k] if l != r
end

puts s

# c598-B

def process_query(str, l,r,k)
	n = str.size
	a = l-1
	b = r - k % (r-a)
	c = r
	str[0,a] + str[b, c-b] + str[a, b-a] + str[c, n-c]
end

str = gets
gets.to_i.times do
	l, r, k = gets.split.map {|x| Integer(x)}
	str = process_query(str, l, r, k)
end
puts str

#!/usr/bin/env ruby

s = gets.strip # < 10000
m = gets.to_i

m.times.map do
  l_i, r_i, k_i = gets.split(' ').map(&:to_i)
  l_i -= 1
  r_i -= 1

  window_len = r_i - l_i + 1
  k_i = k_i.modulo(window_len)

  slice_len = window_len - k_i
  substr = s.slice!(l_i, slice_len)
  #p s + ' ' + substr
  insertIdx = l_i+k_i
  #p insertIdx
  s.insert(insertIdx, substr)
  #p s
end

puts s

def rotate(s, l, r, k)
  len = r - l + 1
  k = k % len
  result = ''
  result += s[0..l-2] if (l - 2 >= 0)
  result += s[r-k..r-1] if (r-k <= r-1)
  result += s[l-1..r-1-k] if (l-1 <= r-1-k)
  result += s[r..-1] if (r < s.length)
end

s = gets
m = gets.to_i
(1..m).each do
  (l, r, k) = gets.split(' ').map(&:to_i)
  s = rotate(s, l, r, k)
end

puts s

s = gets.chomp.split("")
m = gets.chomp.to_i
m.times do
  l, r, k = gets.chomp.split(" ").map(&:to_i)
  kk = k % (r - l + 1)
  sr = []
  kk.times do
    sr.unshift(s.slice!(r-1))
    r -= 1
  end
  s.insert(l-1, sr).flatten!
end
puts s.join("")

#!/usr/bin/ruby

str = gets.strip()
len = gets.to_i
for iter in 0..len - 1
    l, r, k = gets.split(' ')
    l = l.to_i
    r = r.to_i
    k = k.to_i
    substr = str[l-1..r-1]
    k %= substr.length
    substr = substr[-k, k] + substr[0, substr.length-k]
    substr = substr + str[r..-1]
    if l >= 2
        str = str[0..l-2] + substr
    else
        str = substr
    end
end
puts str

s = gets
m = gets.to_i

def shift(s, step)
  return s if step == 0
  s[-step..-1] + s[0..s.size-step-1]
end

m.times do
  l,r,k = gets.split.map(&:to_i)
  l-=1
  r-=1
  k %= r + 1 - l
  s[l..r] = shift(s[l..r], k)
end
puts(s)

s = gets.chomp.split('')
m = gets.to_i
for i in 0..m-1
	l, r, k = gets.split(' ').map{|i| i.to_i}
	l -= 1
	r -= 1
	s = (l > 0 ? s[0..l-1] : []) + s[l..r].rotate(-k) + s[r+1..s.length-1]
end

puts s.reduce{|a, b| a << b}

str = gets.chomp
m = gets.chomp.to_i
for i in 1..m do
  l, r, k = gets.split.map(&:to_i)
  k =  k % (r - l + 1)
  str[l-1..r-1] = str[r-k..r-1] + str[l-1..r-1-k]
end
puts str

str = gets.chomp
m = gets.chomp.to_i
for i in 1..m do
  l, r, k = gets.split.map(&:to_i)
  for n in 1..k do
    newStr = ""
    newStr << str[r-1]
    newStr << str[l-1..r-2]
    str[l-1..r-1] = newStr
  end
end
puts str

s = gets.to_s
m = gets.to_i

m.times do
  l, r, k = gets.split(' ').map{ |x| x.to_i }
  l -= 1
  r -= 1
  s[l..r] = s[r-k+1..r] + s[l..r-k] if l != r
end

puts s

s = gets.to_s
m = gets.to_i

m.times do
  l, r, k = gets.split(' ').map{ |x| x.to_i }
  l -= 1
  r -= 1
  s[l..r] = s[r-k+1..r] + s[l..r-k]
end

puts s

def rotate(s, l, r, k)
  len = r - l + 1
  k = k % len
  result = ''
  result += s[0..l-2] if (l - 2 > 0)
  result += s[r-k..r-1]
  result += s[l-1..r-1-k]
  result += s[r..-1] if (r < s.length)

end

s = gets
m = gets.to_i
(1..m).each do
  (l, r, k) = gets.split(' ').map(&:to_i)
  s = rotate(s, l, r, k)
end

puts s

s = gets
m = gets.to_i

def shift(s, step)
  return s if step == 0
  s[step..-1] + s[0..step-1]
end

m.times do
  l,r,k = gets.split.map(&:to_i)
  k %= r + 1 - l
  s[l..r] = shift(s[l..r], k)
end
puts(s)

s = gets
m = gets.to_i

def shift(s, step)
  return s if step == 0
  s[0..step-1] + s[step..-1]
end

m.times do
  l,r,k = gets.split.map(&:to_i)
  l-=1
  r-=1
  k %= r + 1 - l
  s[l..r] = shift(s[l..r], k)
end
puts(s)

s = gets
m = gets.to_i

def shift(s, step)
  return s if step == 0
  s[step..-1] + s[0..step-1]
end

m.times do
  l,r,k = gets.split.map(&:to_i)
  l-=1
  r-=1
  k %= r + 1 - l
  s[l..r] = shift(s[l..r], k)
end
puts(s)


require 'set'

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

current = 1
ans = []

until m - current < 0
    if alreadyHas.include? current
        current += 1
        next
    else
        ans << current
        m -= current
        current += 1
    end
end

puts ans.length

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

puts ''

#!/usr/bin/ruby
n,m=gets.split.map(&:to_i)
h={}
gets.split.each{|e|h[e.to_i]=1}
r=[]
(1..1/0.0).each{|i|
	if !h[i]
		break if m<i
		m-=i
		r<<i
	end
}
p r.size
puts r*' '

n, m = gets.chomp.split.map(&:to_i)
hash = {}.tap do |h|
  gets.chomp.split.map(&:to_i).each { |idx| h[idx] = true }
 end

values = []
(1..(10**9)).each do |idx|
  break if idx > m
  next if hash.key?(idx)

  m -= idx
  values << idx
end
puts values.length
puts values.join(' ')

n, m = gets.chomp.split.map(&:to_i)
hash = {}.tap do |h|
  gets.chomp.split.map(&:to_i).each { |idx| h[idx] = true }
 end

values = []
sum = 0
(1..(10**9)).each do |idx|
  next if hash.key?(idx)
  break if sum + idx > m

  sum += idx
  values << idx
end
puts values.length
puts values.join(' ')

n, m = readline.split(' ').collect(&:to_i)
h = Hash[readline.split(' ').collect(&:to_i).collect{|num| [num, true]}]
res = []
ct = 0
left = m

1.upto(m) do |item|
  if item > left
    break
  end
  if !h.has_key?(item)
    res.push(item)
    left -= item
    ct += 1
  end
end

puts ct
res.each_with_index do |item, ind|
  if ind != 0
    print " "
  end
  print item
end
puts ""

n, m = readline.split(' ').collect(&:to_i)
h = Hash[readline.split(' ').collect(&:to_i).collect{|num| [num, true]}]
res = []
ct = 0
left = m

1.upto(m) do |item|
  if item > left
    break
  end
  if !h.has_key?(item)
    res.insert(-1, item)
    left -= item
    ct += 1
  end
end

puts ct
res.each_with_index do |item, ind|
  if ind != 0
    print " "
  end
  print item
end
puts ""

a=gets.chomp.split(" ")
cost=a[1].to_i
b=gets.chomp.split(" ").map {|i| i.to_i}
b.sort!
ans=0
ar=[]
i=1
j=0
loop do

if i!=b[j]
cost-=i
if cost>=0
ans+=1
ar << i
else
break
end

else
j+=1
end

i+=1
break if i == 1000000000

end

puts "#{ans}"
puts ar.join(" ")

buf = gets.split(' ')
n,m=buf[0].to_i, buf[1].to_i
buf = gets.split(' ')
hash = Hash.new
buf.each do |x|
	hash[x.to_i] = 1
end
ans = Array.new
for i in 1..1000000
	break if i>m
	if !hash[i]
		m-=i
		ans<<i
	end
end
puts ans.size
puts ans.join(' ')

buf = gets.split(' ')
n,m=buf[0].to_i, buf[1].to_i
buf = gets.split(' ')
hash = Hash.new
buf.each do |x|
	hash[x.to_i] = 1
end
ans = Array.new
for i in 1..100000
	break if i>m
	if !hash[i]
		m-=i
		ans<<i
	end
end
puts ans.size
puts ans.join(' ')


def read
  gets.split.map &:to_i
end
gets.to_i.times do
  n, x = read
  a = read
  res, b = a.sum, a.dup
  while true
    ok = true
    (0...n).each do |i|
      break unless ok
      if b[i] % x == 0
        b[i] /= x
        res += a[i]
      else
        ok = false
      end
    end
    break unless ok
  end
  puts res
end

t = gets.chomp.to_i
t.times do
  n, x = gets.chomp.split(' ').map(&:to_i)
  arr = gets.chomp.split(' ').map(&:to_i)
  brr = [0] * arr.size
  min = 10 ** 10
  min_i = -1
  sum = [0] * (arr.size + 1)
  arr.each_with_index do |el, ind|
    sum[ind + 1] = sum[ind] + el
    t = 0
    el1 = el
    while el1 % x == 0
      el1 /= x
      t += 1
    end
    brr[ind] = t
    if t < min
      min = t
      min_i = ind
    end
  end
  res = sum[arr.size] * (min + 1) + sum[min_i]
  puts res
end

t = gets.to_i
ans = []
t.times do
  n,x = gets.chomp.split(" ").map(&:to_i)
  a = gets.chomp.split(" ").map(&:to_i)
  b = a.dup
  arr = Array.new(n,0)
  n.times do |i|
    while b[i] % x == 0
      b[i] /= x
      arr[i] += 1
    end
  end
  min = arr.min
  judge = false
  sum = 0
  n.times do |i|
    judge = true if arr[i] == min
    if judge
      sum += a[i] * (min+1)
    else
      sum += a[i] * (min+2)
    end
  end
  ans << sum
end
puts ans

def read
  gets.split.map &:to_i
end
gets.to_i.times do
  n, x = read
  a = read
  res, b = a.sum, a
  while true
    ok = true
    (0...n).each do |i|
      break unless ok
      if b[i] % x == 0
        b[i] /= x
        res += a[i]
      else
        ok = false
      end
    end
    break unless ok
  end
  puts res
end


gets
x=gets.split.map{|e| e.to_i}
min1=x.min
max1=x.max
c1=x.count{|e|
	e==min1
}
c2=x.count{|e|
	e==max1
}

if min1==max1 then
	puts [0,(c1*(c1-1))/2]*" "
else
	puts [max1-min1,c1*c2]*" "
end

gets

freq = {}
flowers = []
gets.split(/\s+/).each do |f|
  f = f.to_i

  if freq[f].nil?
    freq[f] = 1
  else
    freq[f] += 1
  end
  flowers << f
end

flowers.sort!

first = flowers[0]
last = flowers[-1]

if(first == last)
  puts "#{last - first} #{(freq[first] * (freq[last]-1))/2}"
else
  puts "#{last - first} #{freq[first] * freq[last]}"
end

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

max = a.max
min = a.min

cmax = a.count(max)
cmin = a.count(min)

count = max == min ? cmax*(cmax-1)/2 : cmax*cmin

puts "#{max - min} #{count}"

line = gets
n = line.to_i
line = gets
f = line.split.map(&:to_i)
f_s = f.sort
d = f_s.last - f_s.first
f_m = {}
f.map do |i|
	next unless [f_s.first, f_s.last].include?(i)
	f_m[i] ||= 0
	f_m[i] += 1
end
w = if f_s.first != f_s.last
	f_m[f_s.first] * f_m[f_s.last]
else
	(n * (n-1))/2
end
puts "#{d} #{w}"

n = gets.to_i
b = gets.split.map{|r| r.to_i}.sort

if b[n-1] > b[0]
   counters = b.select{|r| r == b[n-1]}.size
   xounterz = b.select{|r| r == b[0]}.size
   puts (b[n-1]-b[0]).to_s + " " + (counters*xounterz).to_s
else
   puts "0" + ' ' + (b.size * (b.size - 1)/2).to_s
end

#!/usr/bin/ruby

n = $stdin.readline.to_i
b = $stdin.readline.split.map(&:to_i)

b.sort!

a1 = b[0]
a2 = b[b.length-1]

c1 = 0
c2 = 0

b.each { |x|
  if x == a1
    c1 += 1
  end }

b.each { |x|
  if x == a2
    c2 += 1
  end }

if a1 == a2
  ans = b.length * (b.length - 1) / 2
  puts  "0 #{ans}"
else
  puts  "#{a2 - a1} #{c1 * c2}"
end

n = gets.to_i
x = gets.split.map(&:to_i)
l, r = x.max, x.min
if l > r
  puts (l - r).to_s + " " + (x.select{|i| i == l}.size * x.select{|i| i == r}.size).to_s
else
  puts "0" + " " + (x.size * (x.size - 1) / 2).to_s
end

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

h, l = tab.max, tab.min

if h > l
  puts (h-l).to_s + " " +
    (tab.select{|x| x == h}.size * tab.select{|x| x == l}.size).to_s
else
  puts "0 " + (tab.size * (tab.size-1)/2).to_s
end

n, b = gets.to_i, gets.split.map { |x| x.to_i }.sort
max_d = b.last - b.first
arr = Hash.new(0)
b.each_with_index { |x| arr[x] += 1 }
if arr.size == 1
  print max_d, " ", arr[b.last] * (arr[b.last] - 1) / 2
else
  print max_d, " ", arr[b.last] * arr[b.first]
end

n = gets.to_i
flowers = Hash.new(0)

min, max = gets.chomp.split.map { |value| value.to_i }.each { |value| flowers[value] += 1 }.sort.minmax

dif = max - min
path = 0

if dif != 0
    flowers.each { |key, value| path += value * flowers[key - dif] }
else
    path = n * (n - 1) / 2
end

puts "#{dif} #{path}"

n = gets.to_i
flowers = Hash.new(0)

min, max = gets.chomp!.split.map! { |value| value.to_i }.each { |value| flowers[value] += 1 }.sort!.minmax

dif = max - min
path = 0

if dif != 0
    flowers.each { |key, value| path += value * flowers[key - dif] }
else
    path = n * (n - 1) / 2
end

puts "#{dif} #{path}"

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
min, min_counter = 10**9, 0
max, max_counter = 0, 0
gets.split.each { |i|
    i = i.to_i
    if i < min
        min = i
        min_counter = 0
    end

    if i == min
        min_counter += 1
    end

    if i > max
        max = i
        max_counter = 0
    end

    if i == max
        max_counter += 1
    end
}

if max != min
    puts "#{max - min} #{min_counter * max_counter}"
else
    # n C 2
    # -> n! / (2! * (n-2)!)
    # -> (n * (n -1)) / 2!
    # -> n * (n - 1) / 2
    combination = max_counter * (max_counter - 1) / 2
    puts "#{max - min} #{combination}"
end

# pair of two flowers
# first: maximum beauty difference
# second: how many ways to create the diff?

n = Integer(gets)
nums = gets.chomp.split
b = Array.new
for i in 0...n
    b[i] = Integer(nums[i])
end

b.sort!
min = Array.new
max = Array.new
min[0] = b[0]
max[0] = b[n-1]

gap = b[n-1] - b[0]
for i in 0...n-1
    if b[i+1] == b[0]
        min.push(b[i+1])
    end
    if b[n-i-2] == b[n-1]
        max.push(b[n-i-2])
    end
end

if gap != 0
    print gap , " " , min.length * max.length
else
    if n != 2
        ways = n * (n-1) / 2
        print gap , " " , ways
    else
        print gap, " " , "1"
    end
end

def read_ints; STDIN.gets.split.map(&:to_i); end
n = gets.to_i
a = read_ints
_max = a.max
_min = a.min
print _max-_min
a1 = 0
a2 = 0
i=0
n.times do
  if(a[i] == _max)
    a1+=1
  elsif (a[i] == _min)
    a2+=1
  end
  i+=1
end
print ' '
if(_max == _min)
  puts a1 * (a1-1) / 2
else
puts a1 * a2
end

n = gets.to_i
b = {}
h = Hash.new { |h, k| h[k] = 0 }
a = gets.split(/\s+/).map {|q| r = q.to_i; h[r]+=1; r}

r = 0
diff = a.max - a.min
if diff > 0
  a.each {|q| r += h[q + diff] }
else
  h.each {|k,v| r += v*(v-1)/2 if v > 1 }
end

puts "#{diff} #{r}"

#!/usr/bin/ruby

n = $stdin.readline.to_i
b = $stdin.readline.split.map(&:to_i)

b.sort!

a1 = b[0]
a2 = b[b.length-1]

c1 = 0
c2 = 0

b.each { |x|
  if x == a1
    c1 += 1
  end }

b.each { |x|
  if x == a2
    c2 += 1
  end }

if a1 == a2
  c1 = 0
end

puts  "#{a2 - a1} #{c1 * c2}"

#!/usr/bin/ruby

n = $stdin.readline.to_i
b = $stdin.readline.split.map(&:to_i)

b.sort!

a1 = b[0]
a2 = b[b.length-1]

c1 = 0
c2 = 0

b.each { |x|
  if x == a1
    c1 += 1
  end }

b.each { |x|
  if x == a2
    c2 += 1
  end }

puts  "#{a2 - a1} #{c1 * c2}"

n = gets.to_i
b = gets.split.map{|r| r.to_i}.sort

counters = b.select{|r| r == b[n-1]}.size
xounterz = b.select{|r| r == b[0]}.size

puts (b[n-1]-b[0]).to_s + " " + (counters*xounterz).to_s

n = gets.to_i
b = gets.split.map{|r| r.to_i}.sort

counters = 1
xounterz = 1
for i in 1..n-2
  if b[i] == b[0]
     counters+=1
   elsif b[i] == b[n-1]
     xounterz+=1
  end
end

puts b[n-1]-b[0]
puts counters*xounterz

n = gets.to_i
b = gets.split.map{|r| r.to_i}.sort

counters = 1
xounterz = 1
for i in 1..n-2
  if b[i] == b[0]
     counters+=1
  elsif b[i] == b[n-1]
     xounterz+=1
  end
end

max = [b[n-1]-b[0], counters*xounterz]
puts max

n = gets.to_i
b = gets.split.map{|r| r.to_i}.sort

counters = 1
xounterz = 1
for i in 1..n-2
  if b[i] == b[0]
     counters+=1
   elsif b[i] == b[n-1]
     xounterz+=1
  end
end

puts (b[n-1]-b[0]).to_s + " " + (counters*xounterz).to_s

n = gets.to_i
b = gets.split.map{|r| r.to_i}.sort

if b[n-1] > b[0]
   counters = b.select{|r| r == b[n-1]}.size
   xounterz = b.select{|r| r == b[0]}.size
   puts (b[n-1]-b[0]).to_s + " " + (counters*xounterz).to_s
else
   puts "0 1"
end

n = gets.to_i
b = gets.split.map{|r| r.to_i}.sort

counters = 1
xounterz = 1
for i in 1..n-2
  if b[i] == b[0]
     counters+=1
  elsif b[i] == b[n-1]
     xounterz+=1
  end
end

max = [b[n-1]-b[0], counters*xounterz]
max

n = gets.to_i
x = gets.split.map(&:to_i)
l, r = x.max, x.min
if l > r
  puts (l - r).to_s + " " + (x.select{|i| i == l}.size * x.select{|i| i == r}.size).to_s
else
  puts "0" + (x.size * (x.size - 1) / 2).to_s
end

n, b = gets.to_i, gets.split.map { |x| x.to_i }.sort
max_d = b.last - b.first
am = 0
b.each do |x|
  (x..b.last).step(1) do |j|
    am += 1 if (j - x).abs == max_d
  end
end
print max_d, " ",am

n, b = gets.to_i, gets.split.map { |x| x.to_i }.sort
max_d = b.last - b.first
arr = Hash.new(0)
b.each_with_index { |x| arr[x] += 1 }
print max_d, " ", arr[b.last] * arr[b.first]

n, b = gets.to_i, gets.split.map { |x| x.to_i }.sort
max_d = b.last - b.first
am = 0
b.each do |x|
 b[b.index(x) + 1..b.last].each do |y|
   am += 1 if (y - x).abs == max_d
  end
end
print max_d, " ",am

n, b = gets.to_i, gets.split.map { |x| x.to_i }.sort
max_d = b.last - b.first
arr = Hash.new(0)
b.each_with_index { |x| arr[x] += 1 }
if arr.size == 1
  print max_d, " ", arr[b.last] - 1
else
  print max_d, " ", arr[b.last] * arr[b.first]
end

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
min, min_counter = 10**9, 0
max, max_counter = 0, 0
gets.split.each { |i|
    i = i.to_i
    if i < min
        min = i
        min_counter = 0
    end

    if i == min
        min_counter += 1
    end

    if i > max
        max = i
        max_counter = 0
    end

    if i == max
        max_counter += 1
    end
}

if max != min
    puts "#{max - min} #{min_counter * max_counter}"
else
    puts "0 0"
end

# pair of two flowers
# first: maximum beauty difference
# second: how many ways to create the diff?

n = Integer(gets)
nums = gets.chomp.split
b = Array.new
for i in 0...n
    b[i] = Integer(nums[i])
end

b.sort!
min = Array.new
max = Array.new
min[0] = b[0]
max[0] = b[n-1]

gap = b[n-1] - b[0]
for i in 0...n-1
    if b[i+1] == b[0]
        min.push(b[i+1])
    end
    if b[n-i-2] == b[n-1]
        max.push(b[n-i-2])
    end
end
ways = 0
while n >= 2
    ways += (n-1) * n
    n -= 1
end
if gap != 0
    print gap , " " , min.length * max.length
else
    print gap , " " , ways
end

n = Integer(gets)
nums = gets.chomp.split
b = Array.new
for i in 0...n
    b[i] = Integer(nums[i])
end

b.sort!
min = Array.new
max = Array.new
min[0] = b[0]
max[0] = b[n-1]

gap = b[n-1] - b[0]
for i in 0...n-1
    if b[i+1] == b[0]
        min.push(b[i+1])
    end
    if b[n-i-2] == b[n-1]
        max.push(b[n-i-2])
    end
end

if gap != 0
    print gap , " " , min.length * max.length
else
    if n != 2
        ways = 0
        while n >= 2
            ways += (n-1) * n
            n -= 1
        end
        print gap , " " , ways
    else
        print gap, " " , "1"
    end
end

def read_ints; STDIN.gets.split.map(&:to_i); end
n = gets.to_i
a = read_ints
_max = a.max
_min = a.min
print _max-_min
a1 = 0
a2 = 0
i=0
n.times do
  if(a[i] == _max)
    a1+=1
  elsif (a[i] == _min)
    a2+=1
  end
  i+=1
end
print ' '
puts a1 * a2

n = gets.to_i
b = {}
h = Hash.new { |h, k| h[k] = 0 }
a = gets.split(/\s+/).map {|q| r = q.to_i; h[r]+=1; r}

r = 0
diff = a.max - a.min
if diff > 0
  a.each {|q| r += h[q + diff] }
else
  h.each {|k,v| r += v-1 if v > 1 }
end

puts "#{diff} #{r}"

n = gets.to_i
h = Hash.new { |h, k| h[k] = 0 }
a = gets.split(/\s+/).map {|q| r = q.to_i; h[r]+=1; r}

r = 0
diff = a.max - a.min
a.each {|q| r += h[q + diff] }

puts "#{diff} #{r}"

n = gets.to_i
b = {}
h = Hash.new { |h, k| h[k] = 0 }
a = gets.split(/\s+/).map {|q| r = q.to_i; h[r]+=1; r}

r = 0
diff = a.max - a.min
if diff > 0
  a.each {|q| r += h[q + diff] }
else
  h.each {|k,v| r += 2**(v-1)-1 if v > 1 }
end

puts "#{diff} #{r}"

gets

freq = {}
flowers = []
gets.split(/\s+/).each do |f|
  f = f.to_i

  if freq[f].nil?
    freq[f] = 1
  else
    freq[f] += 1
  end
  flowers << f
end

flowers.sort!

first = flowers[0]
last = flowers[-1]

if(first == last)
  puts "#{last - first} #{freq[first] * (freq[last]-1)}"
else
  puts "#{last - first} #{freq[first] * freq[last]}"
end

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

puts "#{a.max - a.min} #{a.count(a.max)*a.count(a.min)}"

line = gets
n = line.to_i
line = gets
f = line.split.map(&:to_i)
f_s = f.sort
d = f_s.last - f_s.first
f_m = {}
f.map do |i|
	next unless [f_s.first, f_s.last].include?(i)
	f_m[i] ||= 0
	f_m[i] += 1
end
w = if f_m.size > 1
	f_m[f_s.first] * f_m[f_s.last]
else
	(n * n-1)/2
end
puts "#{d} #{w}"

line = gets
n = line.to_i
line = gets
f = line.split.map(&:to_i)
f_s = f.sort
d = f_s.last - f_s.first
f_m = {}
f.map do |i|
	next unless [f_s.first, f_s.last].include?(i)
	f_m[i] ||= 0
	f_m[i] += 1
end
w = if f_m.size > 1
	f_m[f_s.first] * f_m[f_s.last]
else
	1
end
puts "#{d} #{w}"

line = gets
n = line.to_i
line = gets
f = line.split.map(&:to_i)
f_s = f.sort
d = f_s.last - f_s.first
f_m = {}
f.map do |i|
	next unless [f_s.first, f_s.last].include?(i)
	f_m[i] ||= 0
	f_m[i] += 1
end
w = if f_s.first != f_s.last
	f_m[f_s.first] * f_m[f_s.last]
else
	1
end
puts "#{d} #{w}"

line = gets
n = line.to_i
line = gets
f = line.split.map(&:to_i)
f_s = f.sort
d = f_s.last - f_s.first
f_m = {}
f.map do |i|
	next unless [f_s.first, f_s.last].include?(i)
	f_m[i] ||= 0
	f_m[i] += 1
end
w = if f_s.first == f_s.last
	f_m[f_s.first] * f_m[f_s.last]
else
	(n * (n-1))/2
end
puts "#{d} #{w}"

line = gets
n = line.to_i
line = gets
f = line.split.map(&:to_i)
f_s = f.sort
d = f_s.last - f_s.first
f_m = {}
f.map do |i|
	next unless [f_s.first, f_s.last].include?(i)
	f_m[i] ||= 0
	f_m[i] += 1
end
w = f_m[f_s.first] * f_m[f_s.last]
puts "#{d} #{w}"

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

h, l = tab.max, tab.min

if h > l
  puts (h-l).to_s + " " +
    (tab.select{|x| x == h}.size * tab.select{|x| x == l}.size).to_s
else
  puts "0 " + (tab.size * (tab.size-1)).to_s
end

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

h, l = tab.max, tab.min

if h != l
  puts (h-l).to_s + " " +
    (tab.select{|x| x == h}.size * tab.select{|x| x == l}.size).to_s
else
  puts "0 " + (tab.size * (tab.size-1)).to_s
end

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

h, l = tab.max, tab.min

puts (h-l).to_s + " " +
  (tab.select{|x| x == h}.size * tab.select{|x| x == l}.size).to_s


original = gets.chomp
altered = gets.chomp

mapping = {}
fixable = true

original.size.times do |i|
  a = original[i]
  b = altered[i]

  if (mapping[b] && mapping[b] != a) || (mapping[a] && mapping[a] != b)
    fixable = false
  else
    mapping[a] = b
    mapping[b] = a
  end
end

if fixable
  has_key = {}
  ans = {}
  mapping.each do |key, value|
    ans[key] = value unless has_key[key] || key == value
    has_key[key] = true
    has_key[value] = true
  end

  puts ans.length
  ans.each do |key, value|
    puts "#{key} #{value}"
  end

else
  puts "-1"
end

a = gets
b = gets
vis = {}
res = []
fin = true
for i in 0..(a.size() - 2)
  if ((vis.include?(a[i]) || vis.include?(b[i])))
      if (!(vis.include?a[i]) || !(vis.include?b[i]) || vis[a[i]] != b[i])
          puts -1
          fin = false
          break
      end
  end
   next if (vis.include?(a[i]))
   vis[a[i]] = b[i]
   vis[b[i]] = a[i]
   if (a[i] != b[i])
       res.push([a[i],b[i]])
   end
end
if (fin)
    puts res.size()
    for i in res
        puts "#{i[0]} #{i[1]}"
    end
end

original = gets.chomp
altered = gets.chomp

mapping = {}
fixable = true

original.size.times do |i|
  if mapping[original[i]]
    fixable = false unless mapping[original[i]] == altered[i]
  else
    mapping[original[i]] = altered[i]
  end
end

if fixable
  has_key = {}
  ans = {}
  mapping.each do |key, value|
    ans[key] = value unless has_key[key] || key == value
    has_key[key] = true
    has_key[value] = true
  end

  puts ans.length
  ans.each do |key, value|
    puts "#{key} #{value}"
  end

else
  puts "-1"
end


gets
$u=gets.hex
puts gets(p)=~/[#$u#{7-$u}]/?"NO":"YES"

n = gets.to_i
t = gets.to_i
a = Array.new(n){
    b = gets.split.map(&:to_i)
    [*1..6] - b - b.map{|_| 7 - _}
}
puts a.uniq.size == 1 ? :YES : :NO

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

def solve
	n = RD[]; t1 = RD[]; t2 = 7 - t1; n.times{
		a, b = I[]
		return "NO" if a == t1 || a == t2 || b == t1 || b == t2
	}
	return "YES"
end

puts solve

def solve(x, faces)
  faces.each do |a, b|
    if (a == b) or (a == x) or (b == x) or ((a + b) == 7) or ((a + x) == 7) or ((b + x) == 7)
      return false
    end
  end
  true
end

n = gets.chomp.to_i
x = gets.chomp.to_i
faces = []
n.times do
  faces << gets.chomp.split(" ").map{|e| e.to_i}
end

ret = solve(x, faces)
puts ret ? "YES" : "NO"

n = gets.to_i - 1
x = 7 - gets.to_i
gets
n.times do
  a = gets.split.map(&:to_i)
  g = ([1,2,3,4,5,6] - [a[0], 7-a[0], a[1], 7-a[1], 7-x])
  if g.size == 1
    x = g[0]
  else
    puts :NO
    exit
  end
end
puts :YES

def r() gets.to_s.split.map &:to_i end
n,x,o=*r,*r;
n.times do
  a,b=*r;o||=[a,b,7-a,7-b].include? x
end
print o ? "NO" : "YES"

#!/usr/bin/ruby
n = gets.to_i
top = gets.to_i
a = []
n.times do
  a << gets.split.map {|x| x.to_i}
end
ans = "YES"
sum = 1 + 2 + 3 + 4 + 5 + 6
for i in 0...n do
  t = sum - top - a[i][0] - a[i][1] - (7 - a[i][0]) - (7 - a[i][1])
  if t == a[i][0] || t == a[i][1] || t == (7 - a[i][0]) || t == (7 - a[i][1]) then
    ans = "NO"
    break
  end
end
puts ans

n = gets.to_i
top = gets.to_i
bottom = 7 - top.to_i
sides = []
flag = true
n.times do |i|
    side1, side2 = gets.chomp.split.map(&:to_i)
    if side1 == top || side1 == bottom || side2 == top ||side2 == bottom then
        flag = false
    end
end
puts flag ? "YES" : "NO"

n = gets.to_i
last = gets.to_i
1.upto(n) {
    buf = gets.split(" ")
    a = buf[0].to_i
    b = buf[1].to_i
    if a == last || b == last || a == 7 - last || b == 7 - last
        print "NO"
        exit 0
    end
}
print "YES"

n = gets.to_i
top = gets.to_i
(1..n).each do
    left, right = gets.split.collect{|i| i.to_i}
    if left == top or 7-left == top or\
       right == top or 7-right == top
        puts 'NO'
        exit
    end
end
puts 'YES'

n = gets.to_i
c = 7 - gets.to_i
n.times do
  c = 7 - c
  a,b = gets.split.map &:to_i
  if a==c || a==7-c || b==7-c || b == c
    puts 'NO'
    exit
  end
end
puts "YES"

def read_next_line
  gets.split.map { |x| x.to_i }
end

n = gets.to_i
d = gets.to_i
for _ in 1..n
  a = read_next_line
  a.concat(a.map { |x| 7 - x })
  if a.include? d
    puts 'NO'
    exit
  end
end
puts 'YES'

gets
u = gets.to_i
puts gets(p) =~ /[#{u}#{7-u}]/ ? "NO" : "YES"

n=gets.to_i
u=gets.to_i
d=7-u
gets
(n-1).times{
	unless gets.split.map(&:to_i)&[u,d]==[]
		puts "NO"
		exit
	end
}
puts "YES"

n = gets.to_i
last = gets.to_i
1.upto(n) {
    buf = gets.split(" ")
    a = buf[0].to_i
    b = buf[1].to_i
    if a == last || b == last
        print "NO"
        exit 0
    end
    last = 7 - last
}
print "YES"

n = gets.to_i
c = 7 - gets.to_i
n.times do
  c = 7 - c
  a,b = gets.split.map &:to_i
  if a==c || b == c
    puts 'NO'
    exit
  end
end
puts "YES"


a=gets.split(' ').map(&:to_i)
s=gets.strip
ans =0
s.each_char do |aa|
  ans+=a[aa.to_i-1]
end
puts ans

cal = gets.split(' ').map(&:to_i).unshift(0)
ans = 0
gets.split(//).map(&:to_i).each{|x| ans += cal[x]}
puts ans

a = gets.split.map(&:to_i)
b = gets.chomp.to_s
b = b.each_char.map(&:to_i)
sum = 0

for i in 0..b.length-1
    sum += a[b[i]-1]
end
puts sum

a,b,c,d = gets.split.map(&:to_i)
s = gets.chomp.to_s.split(//)
sum = 0
s.each {
    |i|
    if i == '1'
        sum+=a
    elsif i == '2'
        sum+=b
    elsif i == '3'
        sum+=c
    else
        sum +=d
    end
}
puts sum

p = gets.split.map(&:to_i)
counter=0
gets.chomp.to_s.split(//).each{|x| counter+=p[x.to_i-1]}
puts counter

p = gets.split.map(&:to_i)
s = gets.chomp.to_s.split(//)

counter = 0
for i in 0..s.length-1
  case s[i]
  when "1"
    counter+=p[0]
  when "2"
    counter+=p[1]
  when "3"
    counter+=p[2]
  when "4"
    counter+=p[3]
  end
end
puts counter

arr = gets.split.map &:to_i
sum = 0
s = gets.split("").map &:to_i
4.times do |j|
  sum += s.count { |x| x == j + 1 } * arr[j]
end
puts sum

a=gets.chomp.split(" ")
b=gets.chomp
ans=0
b.each_char do |i|
ind=i.to_i-1
ans+=a[ind].to_i
end
puts "#{ans}"

as = gets.split.map(&:to_i)
ans = 0
gets.chomp.chars.map(&:to_i).each do |s|
  ans += as[s-1]
end
puts ans

a=gets.split.map{|e| e.to_i}
a.unshift(0)
s=gets.chomp
puts s.chars.inject(0){|sum,e|
	sum+a[e.to_i]
}

s = gets.split.map{ |e| e.to_i }
sum = 0
q = gets.chomp.split(//).each { |e| sum += s[e.to_i-1] }
puts sum

a=gets.split.map &:to_i
p gets.chomp.split(//).inject(0){|s,x|s+a[x.to_i-1]}

# coding: utf-8

# if __FILE__ == $0
# end

nth_calories = gets.split.map { |i| i.to_i }
notes = gets[0...-1]
total_calories = 0
notes.each_char { |c|
    total_calories += nth_calories[c.to_i - 1]
}
puts total_calories

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

print s.count(1)*a[0] + s.count(2)*a[1] + s.count(3)*a[2] + s.count(4)*a[3]

#MADE BY HAMZA HASBI

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

mp=Hash.new(0)

t={'1'=>0,'2'=>1,'3'=>2,'4'=>3}

for i in '1'..'4'
  mp[i]=a[t[i]]
end

ans =0
for i in 0...b.length
  ans+=mp[b[i]]
end
puts ans

a = [nil] + gets.split.map(&:to_i)
s = gets.chomp.split("").map(&:to_i)
ans = 0
s.each do |x|
  ans += a[x]
end
p ans

a = [nil] + gets.split.map(&:to_i)
s = gets.chomp.split("").map(&:to_i)
puts s.map{|x| a[x]}.inject(:+)

a = [nil] + gets.split.map{|x| x.to_i}
puts(gets.strip.split('').map{|x| x.to_i}.inject(0){|s, x| s += a[x]})

a = gets.split(' ').map(&:to_i).unshift(0)

puts gets.chomp.chars.inject(0) { |s, x| s + a[x.to_i] }

a = gets.split.map(&:to_i)
s = gets.chomp
cal = 0
s.each_char do |c|
  cal += a[c.to_i - 1]
end
puts cal


def check(x)
  m = 0
  for i in (1..$n) do
    m+=$a[i][0]*x/$a[i][1]+1
  end
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
s = 0
for i in (1..$n) do
  $a[i] = gets.split.map{|e| e.to_i}
  s+=$a[i][0]
end
if s==0 && $n==$c
  puts -1
  exit
end
if s==0 && $n!=$c
  puts 0
  exit
end
l=0
r=1
c=check(r)
while c!=1 do
  r*=2
  c=check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
	exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  if check(z)==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  if check(z)==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = 0
  for i in (1..$n) do
    m+=$a[i][0]*x/$a[i][1]+1
    if m>$c then break end
  end
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
s = 0
for i in (1..$n) do
  $a[i] = gets.split.map{|e| e.to_i}
  s+=$a[i][0]
end
if s==0 && $n==$c
  puts -1
  exit
end
if s==0 && $n!=$c
  puts 0
  exit
end
l=0
r=1
c=check(r)
while c!=1 do
  r*=2
  c=check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
	exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  if check(z)==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  if check(z)==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = 0
  for i in (1..$n) do
    m+=$a[i][0]*x/$a[i][1]+1
  end
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  $a[i] = gets.split.map{|e| e.to_i}
end
l=0
r=1
c=check(r)
while c!=1 do
  r*=2
  c=check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
	exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  if check(z)==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  if check(z)==0 then l1=z else r=z end
end

puts r-l-1

# ABBYY Cup 2.0 - Easy
# Problem E - Space Voyage
$n, c = gets.split.map(&:to_i)
$a, $b = [], []
$n.times do
    a_i, b_i = gets.split.map(&:to_i)
    $a << a_i
    $b << b_i
end

def get_value(x)
    ret = $n
    $n.times do |i|
        ret += $a[i] * x / $b[i]
    end
    ret
end

low, high = 1, 10 ** 18 + 1
while low < high
    middle = low + high >> 1
    if get_value(middle) >= c
        high = middle
    else
        low = middle + 1
    end
end
minimum = high

low, high = 0, 10 ** 18
while low < high
    middle = low + high + 1 >> 1
    if get_value(middle) <= c
        low = middle
    else
        high = middle - 1
    end
end
maximum = low

answer = [maximum - minimum + 1, 0].max
answer = -1 if answer == 10 ** 18
puts answer

def check(x)
  m = 0
  for i in (1..$n) do
    m+=$a[i][0]*x/$a[i][1]+1
  end
  m>$c
end
def check2(x)
  m = 0
  for i in (1..$n) do
    m+=$a[i][0]*x/$a[i][1]+1
  end
  m>=$c
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  $a[i] = gets.split.map{|e| e.to_i}
end
l=1
r=1
c=check(r)
while !c do
  r*=2
  c=check(r)
end
l2 = l1 = l
r2 = r1 = r
while r1-l1>1 do
  m = (l1+r1)/2
  if check(m) then r1=m else l1=m end
end
while r2-l2>1 do
  m = (l2+r2)/2
  if check2(m) then r2=m else l2=m end
end
ans = l1-r2+1
puts ans

def check(x)
  m = 0
  for i in (1..$n) do
    m+=$a[i][0]*x/$a[i][1]+1
  end
  m>$c
end
def check2(x)
  m = 0
  for i in (1..$n) do
    m+=$a[i][0]*x/$a[i][1]+1
  end
  m>=$c
end

def check3(x)
  m = 0
  for i in (1..$n) do
    m+=$a[i][0]*x/$a[i][1]+1
  end
  m==$c
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  $a[i] = gets.split.map{|e| e.to_i}
end
l=1
r=1
c=check(r)
while !c do
  r*=2
  c=check(r)
end
l2 = l1 = l
r2 = r1 = r
while r1-l1>1 do
  m = (l1+r1)/2
  if check(m) then r1=m else l1=m end
end
while r2-l2>1 do
  m = (l2+r2)/2
  if check2(m) then r2=m else l2=m end
end
ans = l1-r2+1
if l1==r2 then
  c=check3(r2)
  if c then ans=1 else ans=0 end
end

puts ans

def check(x)
  m = 0
  for i in (1..$n) do
    m+=$a[i][0]*x/$a[i][1]+1
  end
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  $a[i] = gets.split.map{|e| e.to_i}
end
l=0
r=1
c=check(r)
while c!=1 do
  r*=2
  c=check(r)
end

m=0
puts l,r
loop do
  if r-1==l then
    puts 0
	halt
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  if check(z)==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  if check(z)==0 then l1=z else r=z end
end

puts l-r-1

def check(x)
  m = 0
  for i in (1..$n) do
    m+=$a[i][0]*x/$a[i][1]+1
  end
  m>$c
end
def check2(x)
  m = 0
  for i in (1..$n) do
    m+=$a[i][0]*x/$a[i][1]+1
  end
  m>=$c
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  $a[i] = gets.split.map{|e| e.to_i}
end
l=1
r=1
c=check(r)
while !c do
  r*=2
  c=check(r)
end
l2 = l1 = l
r2 = r1 = r
while r1-l1>1 do
  m = (l1+r1)/2
  if check(m) then r1=m else l1=m end
end
while r2-l2>1 do
  m = (l2+r2)/2
  if check2(m) then r2=m else l2=m end
end
ans = l1-r2+1
if l1==r2 then
  c=check(r2)
  if c then ans=1 else ans=0 end
end

puts ans

def check(x)
  m = ($oo*x+0.000001).to_i + $n
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$oo = 0
for i in (1..$n) do
  a = gets.split.map{|e| e.to_f}
  $oo+= a[0]/a[1]
end
l=0
r=1
c = check(r)
while c!=1 do
  r*=2
  c = check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
    exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  c = check(z)
  if c==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  c = check(z)
  if c==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = ($oo*x+0.00000001).to_i + $n
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$oo = 0
for i in (1..$n) do
  a = gets.split.map{|e| e.to_f}
  $oo+= a[0]/a[1]
end
l=0
r=1
c = check(r)
while c!=1 do
  r*=2
  c = check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
    exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  c = check(z)
  if c==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  c = check(z)
  if c==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = 0
  for i in (1..$n) do
    m+=($a[i]*x+1.0000001).to_i
    if m>$c then break end
  end
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  a = gets.split.map{|e| e.to_f}
  $a[i] = a[0]/a[1]
end
l=0
r=1
c = check(r)
while c!=1 do
  r*=2
  c = check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
    exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  c = check(z)
  if c==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  c = check(z)
  if c==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = 0
  for i in (1..$n) do
    if $a[i][0]==0 then
      m+=1
    else
      m+=$a[i][0]*x/$a[i][1]+1
    end
    if m>$c then break end
  end
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  $a[i] = gets.split.map{|e| e.to_i}
end
l=0
r=1
c = check(r)
while c!=1 do
  r*=2
  c = check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
    exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  c = check(m)
  if c==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  c = check(m)
  if c==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = 0
  for i in (1..$n) do
    m+=($a[i]*x+1.000001).to_i
    if m>$c then break end
  end
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  a = gets.split.map{|e| e.to_f}
  $a[i] = a[0]/a[1]
end
l=0
r=1
c = check(r)
while c!=1 do
  r*=2
  c = check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
    exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  c = check(z)
  if c==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  c = check(z)
  if c==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = 0
  for i in (1..$n) do
    m+=($a[i]*x+1.00000000000000001).to_i
    if m>$c then break end
  end
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  a = gets.split.map{|e| e.to_f}
  $a[i] = a[0]/a[1]
end
l=0
r=1
c = check(r)
while c!=1 do
  r*=2
  c = check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
    exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  c = check(z)
  if c==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  c = check(z)
  if c==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = ($oo*x+0.0000000000001).to_i + $n
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$oo = 0
for i in (1..$n) do
  a = gets.split.map{|e| e.to_f}
  $oo+= a[0]/a[1]
end
l=0
r=1
c = check(r)
while c!=1 do
  r*=2
  c = check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
    exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  c = check(z)
  if c==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  c = check(z)
  if c==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = ($oo*x).to_i + $n
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$oo = 0.to_r
for i in (1..$n) do
  a = gets.split.map{|e| e.to_r}
  $oo+= a[0]/a[1]
end
l=0
r=1
c = check(r)
while c!=1 do
  r*=2
  c = check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
    exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  c = check(z)
  if c==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  c = check(z)
  if c==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = 0
  for i in (1..$n) do
    m+=($a[i]*x+1.000000001).to_i
    if m>$c then break end
  end
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  a = gets.split.map{|e| e.to_f}
  $a[i] = a[0]/a[1]
end
l=0
r=1
c = check(r)
while c!=1 do
  r*=2
  c = check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
    exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  c = check(z)
  if c==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  c = check(z)
  if c==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = 0
  for i in (1..$n) do
    m+=($a[i]*x+1.0000000000001).to_i
    if m>$c then break end
  end
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  a = gets.split.map{|e| e.to_f}
  $a[i] = a[0]/a[1]
end
l=0
r=1
c = check(r)
while c!=1 do
  r*=2
  c = check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
    exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  c = check(z)
  if c==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  c = check(z)
  if c==0 then l1=z else r=z end
end

puts r-l-1

def check(x)
  m = 0
  for i in (1..$n) do
    m+=($a[i]*x+1.00000000001).to_i
    if m>$c then break end
  end
  if m>$c then return 1 end
  if m==$c then return 0 end
  if m<$c then return -1 end
end

$n,$c = gets.split.map{|e| e.to_i}
$a = []
for i in (1..$n) do
  a = gets.split.map{|e| e.to_f}
  $a[i] = a[0]/a[1]
end
l=0
r=1
c = check(r)
while c!=1 do
  r*=2
  c = check(r)
end

m=0
loop do
  if r-1==l then
    puts 0
    exit
  end
  m = (l+r)/2
  c = check(m)
  if c==1
    r = m
  elsif c==-1
    l = m
  elsif c==0
    break
  end
end

l1 = r1 = m
while r1-l>1 do
  z = (l+r1)/2
  c = check(z)
  if c==0 then r1=z else l=z end
end

while r-l1>1 do
  z = (l1+r)/2
  c = check(z)
  if c==0 then l1=z else r=z end
end

puts r-l-1

$n, c = gets.split.map(&:to_i)
$a, $b = [], []
$n.times do
    a_i, b_i = gets.split.map(&:to_i)
    $a << a_i
    $b << b_i
end

def get_value(x)
    ret = $n
    $n.times do |i|
        ret += $a[i] * x / $b[i]
    end
    ret
end

low, high = 0, 10 ** 18
while low < high
    middle = low + high >> 1
    if get_value(middle) >= c
        high = middle
    else
        low = middle + 1
    end
end
minimum = high

low, high = 0, 10 ** 18
while low < high
    middle = low + high + 1 >> 1
    if get_value(middle) <= c
        low = middle
    else
        high = middle - 1
    end
end
maximum = low

puts [maximum - minimum + 1, 0].max

$n, c = gets.split.map(&:to_i)
$a, $b = [], []
$n.times do
    a_i, b_i = gets.split.map(&:to_i)
    $a << a_i
    $b << b_i
end

def get_value(x)
    ret = $n
    $n.times do |i|
        ret += $a[i] * x / $b[i]
    end
    ret
end

low, high = 1, 10 ** 18
while low < high
    middle = low + high >> 1
    if get_value(middle) >= c
        high = middle
    else
        low = middle + 1
    end
end
minimum = high

low, high = 0, 10 ** 18
while low < high
    middle = low + high + 1 >> 1
    if get_value(middle) <= c
        low = middle
    else
        high = middle - 1
    end
end
maximum = low

puts [maximum - minimum + 1, 0].max

$n, c = gets.split.map(&:to_i)
$a, $b = [], []
$n.times do
    a_i, b_i = gets.split.map(&:to_i)
    $a << a_i
    $b << b_i
end

def get_value(x)
    ret = $n
    $n.times do |i|
        ret += $a[i] * x / $b[i]
    end
    ret
end

low, high = 1, 10 ** 18
while low < high
    middle = low + high >> 1
    if get_value(middle) >= c
        high = middle
    else
        low = middle + 1
    end
end
minimum = high

low, high = 0, 10 ** 18
while low < high
    middle = low + high + 1 >> 1
    if get_value(middle) <= c
        low = middle
    else
        high = middle - 1
    end
end
maximum = low

answer = [maximum - minimum + 1, 0].max
answer = -1 if answer == 10 ** 18
puts answer

$n, c = gets.split.map(&:to_i)
$a, $b = [], []
$n.times do
    a_i, b_i = gets.split.map(&:to_i)
    $a << a_i
    $b << b_i
end

def get_value(x)
    ret = $n
    $n.times do |i|
        ret += $a[i] * x / $b[i]
    end
    ret
end

low, high = -1, 10 ** 18
while low < high
    middle = low + high >> 1
    if get_value(middle) >= c
        high = middle
    else
        low = middle + 1
    end
end
minimum = high

low, high = -1, 10 ** 18
while low < high
    middle = low + high + 1 >> 1
    if get_value(middle) <= c
        low = middle
    else
        high = middle - 1
    end
end
maximum = low

puts [maximum - minimum + 1, 0].max

$n, c = gets.split.map(&:to_i)
$a, $b = [], []
$n.times do
    a_i, b_i = gets.split.map(&:to_i)
    $a << a_i
    $b << b_i
end

def get_value(x)
    ret = $n
    $n.times do |i|
        ret += $a[i] * x / $b[i]
    end
    ret
end

low, high = 0, 10 ** 18
while low < high
    middle = low + high >> 1
    if get_value(middle) >= c
        high = middle
    else
        low = middle + 1
    end
end
minimum = high

low, high = -1, 10 ** 18
while low < high
    middle = low + high + 1 >> 1
    if get_value(middle) <= c
        low = middle
    else
        high = middle - 1
    end
end
maximum = low

puts [maximum - minimum + 1, 0].max


#!/usr/bin/env ruby
n, m = gets.split(' ').map(&:to_i)
puts n + m - 1
n.times do |i|
  puts "#{i + 1} 1"
end
(m - 1).times do |i|
  puts "1 #{i + 2}"
end

m, n = gets.chomp.split.map { |x| x.to_i }
puts n + m - 1
for i in 1..n
  puts "1 #{i}"
end
for i in 2..m
  puts "#{i} 1"
end

boys, girls = gets.chomp.split.map(&:to_i)
puts boys + girls - 1
(1..girls).each { |e| puts "1 #{e}" }
(2..boys).each { |e| puts "#{e} 1" }

n, m = gets.split.map(&:to_i)
puts n + m - 1
puts [*1..n].map{|i| i.to_s+" 1"}
puts [*2..m].map{|i| "1 "+i.to_s}

a, b = gets.split.map(&:to_i)
p a + b - 1
puts "1 1"
2.upto(a) do |i|
  puts "#{i} 1"
end
2.upto(b) do |i|
  puts "1 #{i}"
end

require "Matrix"

h=Matrix
n, m = gets.split.map{|i| i.to_i}
puts(n+m-1)
for i in 1..m
  puts("1 #{i}")
end
for i in 2..n
  puts("#{i} 1")
end

n = gets(" ")
m = gets
n = n.to_i
m = m.to_i
puts m + n - 1
m.times { |x| print "1 #{x+1}\n"}
(n-1).times { |x|
	print "#{x+2} #{m > 1 ? 2 : 1}\n"}

def run
  n, m = $stdin.readline.split.map(&:to_i)

  puts n + m - 1

  n.times do |i|
    puts "#{i + 1} 1"
  end

  if n == 1
    (m - 1).times do |i|
      puts "1 #{i + 2}"
    end
  elsif m > 1
    (m - 1).times do |i|
      puts "2 #{i + 2}"
    end
  end
end

run if $0 == __FILE__

def run
  n, m = $stdin.readline.split.map(&:to_i)

  puts n + m - 1

  n.times do |i|
    puts "#{i + 1} 1"
  end

  (m - 1).times do |i|
    if n == 1
      puts "1 #{i + 2}"
    else
      puts "2 #{i + 2}"
    end
  end
end

run if $0 == __FILE__

class A
  def initialize
    n, m = gets.chomp.split(' ').map(&:to_i)

    res = n+m-1
    ans = []

    1.upto(m) do |index|
      ans << [1,index]
    end

    2.upto(n) do |index|
      ans << [index, m]
    end

    puts ans.size
    ans.each do |a|
      puts a.join(' ')
    end
  end

end

a = A.new

a=gets.chomp.split(" ")
ans=-1
a=a.each do |i|
a[a.index(i)]=i.to_i
ans+=i.to_i
end
t=0
if a[0]>a[1]
t=1
end
a.sort!
a.reverse!
puts "#{ans}"
n=0
m=1
trig1=1
trig2=-1
loop do
if n<a[0] && trig1>0
n+=1
end
if m<a[1] && trig2>0
m+=1
end
trig1=trig1*(-1)
trig2=trig1*(-1)
if t== 1
puts "#{n} #{m}"
else
puts "#{m} #{n}"
end
if m==a[1]
trig1=2
end
break if n==a[0] && m==a[1]
end

n,m = gets.split.map &:to_i
cant,ln,lm = 0,1,1
nn = false
a = []
while true
    cant+=1
    a<< "#{ln} #{lm}"
    som = false
    if nn then
        if ln<n then
            ln+=1
            som=true
        elsif lm<m
            lm+=1
            som=true
        end
        nn=false
    else
        if lm<m then
            lm+=1
            som=true
        elsif ln<n then
            ln+=1
            som=true
        end
        nn=true
    end
    if !som then
        break
    end
end
puts cant
for b in a
    puts b
end

n,m=gets.split.map(&:to_i)
used1=[]
used2=[]
res=[]
1.upto(n) {|i|
    1.upto(m) {|j|
        if !used1.include?(i) or !used2.include?(j)
            res<<[i, j]
            used1<<i if !used1.include?(i)
            used2<<j if !used2.include?(j)
        end
    }
}
p res.size
res.each {|v| puts v*' '}

#!/usr/bin/ruby
include Math

n,m = gets.split.map(&:to_i)
ret = []
boys = Array.new(n,true)
girls = Array.new(m,true)
for i in 1..n
  for j in 1..m
    if boys[i-1] || girls[j-1]
      boys[i-1] = false
      girls[j-1] = false
      ret << [i,j].join(" ")
    end
  end
end
puts ret.size
puts ret.join "\n"

class Pair
  attr_accessor :first, :second
  def initialize(first,second)
    @first = first
    @second = second
  end
  def to_s()
    "#{@first} #{@second}"
  end
end

n,m = gets.split.map &:to_i
cont=0
may = [n,m].max
men = [n,m].min
vec = Array.new
for i in 1...men
  for j in i..i+1
    cont+=1
    vec<<Pair.new(i,j)
  end
end

for i in men..may
  (men == n)? vec<<Pair.new(men,i) : vec<<Pair.new(i,men)
  cont+=1
end

puts cont

for elem in vec
  puts elem.to_s
end

n,m = STDIN.readline.split.map(&:to_i)
puts n+m-1
(1..n).each{|x|
  puts "#{x} 1"
}
(2..m).each{|x|
  puts "1 #{x}"
}

#!/usr/bin/ruby
guys, girls = gets.chomp.split(" ").map{ |x| x.to_i}

puts girls + guys - 1
lesser = [girls, guys].min
greater = [girls, guys].max

girls_first = lesser == girls

# all but one girl
min_greater = 1
for i in 1..lesser -1 do
    puts "1 " + i.to_s if girls_first
    puts i.to_s + " 1" if !girls_first

end

for i in min_greater..greater do
    puts i.to_s + " " + lesser.to_s if girls_first
    puts lesser.to_s + " " + i.to_s if !girls_first
end

n, m = gets.strip.split(" ").collect{|v| v.to_i}
puts (m + n - 1)
(1..n).each{|i| puts [i, 1].join(" ")}
(2..m).each{|j| puts [1, j].join(" ")}

a, b = gets.split.map(&:to_i)
p a + b - 1
puts"1 1"
2.upto(a) do |i|
	puts "#{i} 1"
end
2.upto(b) do |i|
	puts "1 #{i}"
end

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

puts n + m - 1

m.times { |i| puts "1 #{i+1}" }
(n-1).times { |i| puts "#{i+2} 1" }

n = gets(" ")
m = gets
n = n.to_i
m = m.to_i
puts m + n - 1
m.times { |x| print "1 #{x+1}\n"}
(n-1).times { |x| print "#{x+2} 2\n"}

n = gets(" ")
m = gets
n = n.to_i
m = m.to_i
puts m + n - 1
if m < n
	m, n = n, m
end
m.times { |x| print "1 #{x+1}\n"}
(n-1).times { |x| print "#{x+2} 2\n"}

def run
  n, m = $stdin.readline.split.map(&:to_i)

  puts n + m - 1

  n.times do |i|
    puts "#{i + 1} 1"
  end

  if m > 1
    (m - 1).times do |i|
      puts "2 #{i + 2}"
    end
  end
end

run if $0 == __FILE__

a=gets.chomp.split(" ")
ans=-1
a=a.each do |i|
a[a.index(i)]=i.to_i
ans+=i.to_i
end
a.sort!
a.reverse!
puts "#{ans}"
#puts "1 1"
n=0
m=1
trig1=1
trig2=-1
loop do
if n<a[0] && trig1>0
n+=1
end
if m<a[1] && trig2>0
m+=1
end
trig1=trig1*(-1)
trig2=trig1*(-1)
puts "#{n} #{m}"
if m==a[1]
trig1=2
end
break if n==a[0] && m==a[1]
end

#!/usr/bin/ruby
include Math

n,m = gets.split.map(&:to_i)
ret = []
for i in 1..n
  for j in 1..m
    ret << [i,j].join(" ")
  end
end
puts ret.size
puts ret.join "\n"


def isvalid(s)
  n = s.size
  return true if n.zero?
  return false if n == 1

  for i in 0..n - 2
    return true unless s[i] == s[i + 1]
  end
  false
end

def solve
  gets
  f = true
  gets.chomp.split('W').each do |s|
    f = false unless isvalid(s)
  end
  puts f ? :YES : :NO
end

gets.to_i.times do
  solve
end

gets.to_i.times do
  gets
  s = gets.chomp
  ok = s.split("W").reject(&:empty?).all? { |s| s.include?("R") && s.include?("B") }
  puts ok ? "YES" : "NO"
end


  n = gets.to_i
  n.times do
    str = gets.to_s.strip
    len = str.length
    ans = if len.even?
            half = len / 2
            str[0...half] == str[half..] ? 'YES' : 'NO'
          else
            'NO'
          end
    puts ans
  end

t = gets.chomp.to_i
t.times do |n|
    lin = gets.chomp
    ln = lin.size
    lin1 = ""
    lin2 = ""
    if ln%2 == 0
        ln.times do |i|
            if i< (ln/2)
                lin1 += lin[i]
            else
                lin2 += lin[i]
            end
        end
    end
    if lin1!= "" && lin1 == lin2
        puts "YES"
    else
        puts "NO"
    end

end

n = gets.chomp.to_i
n.times do
    w = gets.chomp
    puts (w[0...w.length/2] == w[w.length/2..] ? "YES" : "NO")
end

n = gets.to_i
a = []

def find_ans(w)
  if w.length.odd?
    'NO'
  else
    if w[0..w.length/2 - 1] == w[w.length/2..w.length]
      'YES'
    else
      'NO'
    end
  end
end

(0..n-1).each do |x|
  w = gets.chomp
	a.append(find_ans(w))
end

a.each do |x|
 puts x
end

t = gets.to_i
inputs = []
(0...t).each do |i|
  str = gets.chomp
  inputs << str
end

inputs.each do |str|
  len = str.length
  if len == 1
    puts "NO"
    next
  end
  m = len/2
  substr1 = str[0..m-1]
  substr2 = str[m..len]
  if substr1 == substr2
    puts "YES"
  else
    puts "NO"
  end
end

inputs = STDIN.read.split("\n")
inputs.shift

inputs.each do |input|
  if input.length % 2 != 0
    puts "NO"
    next
  end

  if input[0..input.length/2 - 1] == input[input.length/2..input.length]
    puts "YES"
  else
    puts "NO"
  end
end

t = gets.chomp.to_i
t.times do |n|
    lin = gets.chomp
    ln = lin.size
    lin1 = ""
    lin2 = ""
    if ln%2 == 0
        ln.times do |i|
            if i< (ln/2)
                lin1 += lin[i]
            else
                lin2 += lin[i]
            end
        end
    end
    if lin1!= "" && lin1 == lin2
        p "YES"
    else
        p "NO"
    end

end

t = gets.to_i
t.times do |n|
    lin = gets
    ln = lin.length
    lin1 = ""
    lin2 = ""
    if ln%2 == 0
        ln.times do |i|
            if i< (ln/2)
                lin1 += lin[i]
            else
                lin2 += lin[i]
            end
        end
    end
    if lin1!= "" && lin1 == lin2
        p "YES"
    else
        p "NO"
    end

end

t = gets.to_i
t.times do |n|
    lin = gets
    ln = lin.size
    lin1 = ""
    lin2 = ""
    if ln%2 == 0
        ln.times do |i|
            if i< (ln/2)
                lin1 += lin[i]
            else
                lin2 += lin[i]
            end
        end
    end
    if lin1!= "" && lin1 == lin2
        p "YES"
    else
        p "NO"
    end

end

n = gets.to_i
n.times do
  str = gets.to_s
  len = str.length
  ans = if len.even?
          half = len / 2
          str[0...half] == str[half..] ? 'YES' : 'NO'
        else
          'NO'
        end
  puts ans
end

s = gets.chomp
puts (s[0...s.length/2] == s[s.length/2..] ? "YES" : "NO")

n = gets.to_i
a = []

def find_ans(w)
  if w.length.odd?
    'NO'
  else
    if w[0..w.length/2 - 1] == w[w.length/2..w.length]
      'YES'
    else
      'NO'
    end
  end
end

(0..n-1).each do |x|
  w = gets.chomp
	a.append(find_ans(w))
end

a.each do |x|
 p x
end


n = gets.to_i
l = Array.new(n)
r = Array.new(n)
n.times do |i|
  l[i], r[i] = gets.split.map(&:to_i)
end
k = gets.to_i
puts r.count { |rr| rr >= k }

n = gets.to_i
ch = []
(1..n).each do |i|
    ch << gets.split(" ").map(&:to_i)
end
k = gets.to_i

res = 0
(0..n-1).each do |i|
    if k > ch[i][1]
        res += 1
    end
end

puts n-res

n = gets.to_i
l, r = [], []
n.times do
    li, ri = gets.split.map(&:to_i)
    l << li
    r << ri
end
k = gets.to_i
i = 0
loop do
    break if k <= r[i]
    i += 1
end
puts n - i


s=gets.chomp
n=gets.to_i
puts s.size<n ? :impossible : [0,n-s.chars.uniq.size].max

s=gets.chomp
k=gets.to_i
if k>s.size
	puts "impossible"
else
	n=s.chars.uniq.size
	puts [0, k-n].max
end

s=gets.chomp.chars
k=gets.to_i
puts k > s.size ? :impossible : [0, k - s.uniq.size].max

s, k = gets.chomp.chars, gets.to_i
puts k > s.size ? :impossible : [0, k - s.uniq.size].max

s=gets.chomp.chars
k=gets.to_i
puts k > s.size ? :impossible : [k - s.uniq.size].max

s, cnt, k = gets.chomp, Array.new(26, 0), gets.to_i
s.chars {|c| cnt[c.ord - 97] += 1}
cnt.reject! {|c| c.zero?}
puts k > s.length ? :impossible : k - cnt.length

cnt = Array.new(26, 0)
gets.chomp.chars {|c|
  cnt[c.ord - 97] += 1
}
cnt.reject! {|c| c.zero?}
k = gets.to_i - cnt.length
puts k > cnt.reduce(:+) ? :impossible : k

cnt = Array.new(26, 0)
gets.chomp.chars {|c|
  cnt[c.ord - 97] += 1
}
cnt.reject! {|c| c.zero?}
k = gets.to_i
puts k > cnt.reduce(:+) ? :impossible : k - cnt.length


gets.to_i.times do
  gets
  s = gets.chomp
  answer = s.size
  0.upto(s.size - 2).each do |i|
    if s[i] != s[i + 1]
      answer += i + 1
    end
  end
  p answer
end

# gets.to_i
# gets.split.map(&:to_i)

gets.to_i.times do
  n = gets.to_i
  s = gets.chomp.chars

  ans = n
  i = 1
  while i < n
    if s[i] != s[i-1]
      ans += i
    end
    i += 1
  end

  puts ans
end


r = proc {gets.split.map(&:to_i)}
n = r.call[0]
x, s = r.call
a = r.call
b = r.call
q1 = a.zip(b)
c = r.call
d = r.call
q2 = c.zip(d)
q2.reverse!
c = q2.bsearch{|c, d| s>=d} || [0,0]
sol = (n-c[0]) * x
(q1.select{|a,b| b<=s}).each do |a,b|
  c = q2.bsearch{|c, d| (s-b)>=d} || [0,0]
  sol = [sol, (n-c[0])*a].min
end
puts sol

# Codeforces My Practice
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

ab = a.zip(b).sort_by{|_, bi| bi}
cd = c.zip(d).sort{|u, v|
    if u[1] != v[1]
        v[1] <=> u[1]
    else
        v[0] <=> u[0]
    end
}

res = n * x

m.times do |i|
    bi = ab[i][1]
    break if bi > s
    ai = ab[i][0]
    res = [res, n * ai].min
    cj, _ = cd.bsearch{|_, dj| bi + dj <= s}
    next if cj.nil?
    if cj <= n
        res = [res, (n - cj) * ai].min
    else
        res = [res, ai].min
    end
end
cd.reverse.each do |cj, dj|
    break if dj > s
    if cj <= n
        res = [res, (n - cj) * x].min
    else
        res = [res, x].min
    end
end

puts res

#!/bin/ruby

# n m k       n = total potions to produce, m = number of a/b, k = number of c/d
# x s         initial rate, manapoints available
# a1 a2 a3... (new rate, # of seconds per potion)
# b1 b2 b3... (cost  to speed up rate)
# c1 c2 c3... c1 <= c2, number of instant potions
# d1 d2 d3... d1 <= d2, cost of instant potions

# min total time spent to produce the potions
# for picked zero or one spell of each type

def find_next_smallest(input, target)
  left = 0
  right = input.size - 1
  while right != left
    midpoint = (right + left) / 2
    if right - left == 1
      # adjacent
      if input[right] > target
        return left
      else
        return right
      end
    elsif input[midpoint] > target
      right = midpoint - 1
    else
      left = midpoint
    end
  end

  return left
end

# read input
n, m, k = gets.strip.split(' ').map{ |x| x.to_i }
x, s = gets.strip.split(' ').map{ |x| x.to_i }
a = gets.strip.split(' ').map{ |x| x.to_i }
b = gets.strip.split(' ').map{ |x| x.to_i }
c = gets.strip.split(' ').map{ |x| x.to_i }
d = gets.strip.split(' ').map{ |x| x.to_i }

# base case, no type 1 spell
j = find_next_smallest(d, s)
rem_potion = n
rem_potion = n - c[j] if d[j] <= s
best_time = rem_potion * x

# consider each type 1 spell
0.upto(m-1) do |i|
  next if s < b[i]

  # find largest type 2 spell that can fit in remainder of s
  target = s - b[i]
  j = find_next_smallest(d, target)

  # compute timing
  rem_potion = n
  rem_potion = n - c[j] if d[j] <= target
  time = rem_potion * a[i]
  best_time = time if time < best_time
end

puts best_time

# Codeforces My Practice
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

ab = a.zip(b).sort_by{|_, bi| bi}
cd = c.zip(d).sort{|u, v|
    if u[1] != v[1]
        v[1] <=> u[1]
    else
        v[0] <=> u[0]
    end
}

res = n * x

m.times do |i|
    bi = ab[i][1]
    break if bi > s
    ai = ab[i][0]
    res = [res, n * ai].min
    cj, _ = cd.bsearch{|_, dj| bi + dj <= s}
    next if cj.nil?
    res = [res, (n - cj) * ai].min
end
cd.reverse.each do |cj, dj|
    break if dj > s
    res = [res, (n - cj) * x].min
end

puts res

n, m, k = gets.strip.split.map(&:to_i)
x, s = gets.strip.split.map(&:to_i)
ai = gets.strip.split.map(&:to_i)
bi = gets.strip.split.map(&:to_i)
ci = gets.strip.split.map(&:to_i)
di = gets.strip.split.map(&:to_i)

type2 = []
(k-1).downto(0).each do |i|
  type2 << [di[i], ci[i]]
end

ans = n*x

sp2 = type2.bsearch{|i| i[0]<=s}
ans = [ans, [n - sp2[1], 0].max * x].min if sp2

0.upto(m-1).each do |i|
  if s >= bi[i]
    rem = s - bi[i]
    sp2 = type2.bsearch{|j| j[0]<=rem}
    if sp2
      ans = [ans, [n - sp2[1], 0].max * ai[i]].min
    else
      ans = [ans, n * ai[i]].min
    end
  end
end

puts ans

# Try Codeforces
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

ab = a.zip(b).sort_by{|_, bi| bi}
cd = c.zip(d).sort_by{|_, di| di}

res = n * x

k.times do |i|
    di = cd[i][1]
    break if di > s
    ci = cd[i][0]
    rm = n
    if ci >= n
        rm = 1
        res = [res, x].min
    else
        rm = n - ci
        res = [res, rm * x].min
    end
    m.times do |j|
        break if ab[j][1] + di > s
        res = [res, rm * ab[j][1]].min
    end
end
m.times do |j|
    break if ab[j][1] > s
    res = [res, n * ab[j][0]].min
end

puts res

# Codeforces My Practice
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

ab = a.zip(b).sort_by{|_, bi| bi}
cd = c.zip(d).sort_by{|_, di| -di}

res = n * x

m.times do |i|
    bi = ab[i][1]
    break if bi > s
    ai = ab[i][0]
    res = [res, n * ai].min
    cj, _ = cd.bsearch{|_, dj| bi + dj <= s}
    next if cj.nil?
    res = [res, (n - cj) * ai].min
end
cd.reverse.each do |cj, dj|
    break if dj > s
    res = [res, (n - cj) * x].min
end

puts res

# Codeforces My Practice
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

ab = a.zip(b).sort_by{|_, bi| bi}
cd = c.zip(d).sort_by{|ci, di| [-di, -ci] }

res = n * x

m.times do |i|
    bi = ab[i][1]
    break if bi > s
    ai = ab[i][0]
    res = [res, n * ai].min
    cj, _ = cd.bsearch{|_, dj| bi + dj <= s}
    next if cj.nil?
    if cj < n
        res = [res, (n - cj) * ai].min
    else
        res = [res, ai].min
    end
end
cd.reverse.each do |cj, dj|
    break if dj > s
    if cj < n
        res = [res, (n - cj) * x].min
    else
        res = [res, x].min
    end
end

puts res

# Codeforces My Practice
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

ab = a.zip(b) #.sort_by{|_, bi| bi}
cd = c.zip(d).reverse #.sort_by{|ci, di| [-di, -ci] }

res = n * x

m.times do |i|
    bi = ab[i][1]
    break if bi > s
    ai = ab[i][0]
    res = [res, n * ai].min
    cj, _ = cd.bsearch{|_, dj| bi + dj <= s}
    next if cj.nil?
    if cj <= n
        res = [res, (n - cj) * ai].min
    else
        res = [res, ai].min
    end
end
cd.reverse.each do |cj, dj|
    break if dj > s
    if cj <= n
        res = [res, (n - cj) * x].min
    else
        res = [res, x].min
    end
end

puts res

# Codeforces My Practice
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

ab = a.zip(b).sort_by{|_, bi| bi}.reverse
cd = c.zip(d).sort_by{|_, di| di}

res = n * x

k.times do |i|
    di = cd[i][1]
    break if di > s
    ci = cd[i][0]
    rm = n
    if ci >= n
        rm = 1
        res = [res, x].min
    else
        rm = n - ci
        res = [res, rm * x].min
    end
    ai, bi = ab.bsearch{|_, bi| bi + di <= s}
    res = [res, rm * ai].min
end
m.times do |j|
    break if ab[j][1] > s
    res = [res, n * ab[j][0]].min
end

puts res

# Try Codeforces
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

ab = a.zip(b).sort_by{|_, bi| bi}
cd = c.zip(d).sort_by{|_, di| di}

res = n * x

k.times do |i|
    di = cd[i][1]
    break if di > s
    ci = cd[i][0]
    rm = n
    if ci >= n
        rm = 1
        res = [res, x].min
        next
    else
        rm = n - ci
        res = [res, rm * x].min
    end
    m.times do |j|
        break if ab[j][1] + di > s
        res = [res, rm * ab[j][1]].min
    end
end
m.times do |j|
    break if ab[j][1] > s
    res = [res, n * ab[j][0]].min
end

puts res

# Codeforces My Practice
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

ab = a.zip(b).sort_by{|_, bi| bi}
cd = c.zip(d).sort_by{|_, di| -di}

res = n * x

m.times do |i|
    bi = ab[i][1]
    break if bi > s
    ai = ab[i][0]
    res = [res, n * ai].min
    cj, _ = cd.bsearch{|_, dj| bi + dj <= s}
    next if cj.nil?
    res = [res, (n - cj) * ai].min
end
k.times do |j|
    break if cd[j][1] > s
    res = [res, (n - cd[j][0]) * x].min
end

puts res

# Codeforces My Practice
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

ab = a.zip(b).sort_by{|_, bi| bi}
cd = c.zip(d).sort_by{|_, di| -di}

res = n * x

m.times do |i|
    bi = ab[i][1]
    break if bi > s
    ai = ab[i][0]
    res = [res, n * ai].min
    cj, _ = cd.bsearch{|_, dj| bi + dj <= s}
    next if cj.nil?
    if cj < n
        res = [res, (n - cj) * ai].min
    else
        res = [res, ai].min
    end
end
cd.reverse.each do |cj, dj|
    break if dj > s
    res = [res, (n - cj) * x].min
end

puts res

# Try Codeforces
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

res = n * x

k.times do |i|
    di = d[i]
    break if di > s
    ci = c[i]
    rm = n
    if ci >= n
        rm = 1
        res = [res, x].min
    else
        rm = n - ci
        res = [res, rm * x].min
    end
    m.times do |j|
        break if b[j] + di > s
        res = [res, rm * a[j]].min
    end
end
m.times do |j|
    break if b[j] > s
    res = [res, n * a[j]].min
end

puts res

# Try Codeforces
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.strip end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end
############################################################

n, m, k = gis
x, s = gis
a = gis # speed
b = gis # cost
c = gis # create
d = gis # cost

res = n * x

k.times do |i|
    di = d[i]
    next if di > s
    ci = c[i]
    rm = n
    if ci >= n
        rm = 1
        res = [res, x].min
    else
        rm = n - ci
        res = [res, rm * x].min
    end
end
m.times do |j|
    next if b[j] > s
    res = [res, n * a[j]].min
end

puts res

r = proc {gets.split.map(&:to_i)}
n, m, k = r.call
x, s = r.call
a = r.call
b = r.call
q1 = a.zip(b)
c = r.call
d = r.call
q2 = c.zip(d)
q2.reverse!
sol = x * n
(q1.select{|a,b| b<=s}).each do |a,b|
  c = q2.bsearch{|c, d| (s-b) > d} || [0,0]
  sol = [sol, (n-c[0])*a].min
end
puts sol

r = proc {gets.split.map(&:to_i)}
n, m, k = r.call
x, s = r.call
a = r.call
b = r.call
q1 = a.zip(b)
c = r.call
d = r.call
q2 = c.zip(d)
sol = x * n
(q1.select{|a,b| b<=s}).each do |a,b|
  c = q2.bsearch{|c, d| [(s-b)-d, 0].min} || [0,0]
  sol = [sol, (n-c[0])*a].min
end
puts sol

r = proc {gets.split.map(&:to_i)}
n = r.call[0]
x, s = r.call
a = r.call
b = r.call
q1 = a.zip(b)
c = r.call
d = r.call
q2 = c.zip(d)
q2.reverse!
c = q2.bsearch{|c, d| s>d} || [0,0]
sol = (n-c[0]) * x
(q1.select{|a,b| b<=s}).each do |a,b|
  c = q2.bsearch{|c, d| (s-b)>d} || [0,0]
  sol = [sol, (n-c[0])*a].min
end
puts sol


class Solver

  def self.solve(string)
    result = {}
    height = 0
    result[0] = [0]
    index = 0
    string.each_char do |char|
      index += 1
      height = height + (char == '1' ? 1 : -1)
      if result.fetch(height, nil).nil?
        result[height] = [index]
      elsif result[height].size == 1
        result[height] << index
      else
        result[height].pop
        result[height].push index
      end
      # puts "#{height}:#{index}"
      # puts result
    end
    array_result = result.values
    array_result.select! { |pair| pair.size == 2 }
    # puts array_result.inspect
    return [] if array_result.empty?
    final = array_result.max_by { |pair| pair[1] - pair.first }
    final[1] = final[1] -= 1
    final
  end

  def self.size(string)
    result = self.solve(string)
    # puts result
    return 0 if result.empty?
    result[1] - result[0] + 1
  end
end

string = ARGF.read
arr = string.split
# puts arr.inspect
puts Solver.size(arr[1])

gets
i=1
hs={}
a=0
ans=0
gets.chomp.chars.each{|e|
	a+=1 if e=="1"
	b=i-a
	if b==a
		ans=i
	else
		if hs.key?(b-a)
			ans=[ans,i-hs[b-a]].max
		end
	end
	if hs.key?(b-a)==false
		hs[b-a]=i
	end
	i+=1
}
puts ans

class Solver

  def self.solve(string)
    result = {}
    height = 0
    result[0] = [0]
    string.each_char_with_index do |char, index|
      index += 1
      height = height + (char == '1' ? 1 : -1)
      if result.fetch(height, nil).nil?
        result[height] = [index]
      elsif result[height].size == 1
        result[height] << index
      else
        result[height].pop
        result[height].push index
      end
      puts "#{height}:#{index}"
      puts result
    end
    array_result = result.values
    array_result.select! { |pair| pair.size == 2 }
    puts array_result
    return [] if array_result.empty?
    final = array_result.max_by { |pair| pair[1] - pair.first }
    final[1] = final[1] -= 1
    final
  end

  def self.size(string)
    result = self.solve(string)
    puts result
    return 0 if result.empty?
    result[1] - result[0] + 1
  end
end

class Solver

  def self.solve(string)
    result = {}
    height = 0
    result[0] = [0]
    index = 0
    string.each_char do |char|
      index += 1
      height = height + (char == '1' ? 1 : -1)
      if result.fetch(height, nil).nil?
        result[height] = [index]
      elsif result[height].size == 1
        result[height] << index
      else
        result[height].pop
        result[height].push index
      end
      # puts "#{height}:#{index}"
      # puts result
    end
    array_result = result.values
    array_result.select! { |pair| pair.size == 2 }
    # puts array_result
    return [] if array_result.empty?
    final = array_result.max_by { |pair| pair[1] - pair.first }
    final[1] = final[1] -= 1
    final
  end

  def self.size(string)
    result = self.solve(string)
    # puts result
    return 0 if result.empty?
    result[1] - result[0] + 1
  end
end

string = ARGF.read
puts Solver.size(string)

class Solver

  def self.solve(string)
    result = {}
    height = 0
    result[0] = [0]
    index = 0
    string.each_char do |char|
      index += 1
      height = height + (char == '1' ? 1 : -1)
      if result.fetch(height, nil).nil?
        result[height] = [index]
      elsif result[height].size == 1
        result[height] << index
      else
        result[height].pop
        result[height].push index
      end
      # puts "#{height}:#{index}"
      # puts result
    end
    array_result = result.values
    array_result.select! { |pair| pair.size == 2 }
    # puts array_result.inspect
    return [] if array_result.empty?
    final = array_result.max_by { |pair| pair[1] - pair.first }
    final[1] = final[1] -= 1
    final
  end

  def self.size(string)
    result = self.solve(string)
    # puts result
    return 0 if result.empty?
    result[1] - result[0] + 1
  end
end

string = ARGF.read
puts Solver.size(string.strip)


gets.to_i.times do
 moves = gets.split(' ').map(&:to_i)
 points = gets.split(' ').map(&:to_i)

 left = moves[0]
 right = moves[1]
 down = moves[2]
 up = moves[3]

 start_x, start_y = points[0], points[1]
 min_x, min_y = points[2], points[3]
 max_x, max_y = points[4], points[5]

 if ((start_x - left + right).between?(min_x, max_x) && (min_x < max_x || left + right == 0)) &&
  ((start_y - down + up).between?(min_y, max_y)) && (min_y < max_y || down + up == 0)

  puts "Yes"
 else
  puts "No"
 end
end

gets.to_i.times do
 left, right, down, up  = gets.split(' ').map(&:to_i)
 start_x, start_y, min_x, min_y, max_x, max_y = gets.split(' ').map(&:to_i)

 if (start_x - left + right).between?(min_x, max_x) &&
  (start_y - down + up).between?(min_y, max_y) &&
  (min_x < max_x || left + right == 0) &&
  (min_y < max_y || down + up == 0)
  puts "Yes"
 else
  puts "No"
 end
end

n = gets.chomp.to_i

n.times do
  a,b,c,d = gets.chomp.split.map(&:to_i)

  x,y,x1,y1,x2,y2 = gets.chomp.split.map(&:to_i)

  if x1 == x2 && (a > 0 || b > 0)
    puts 'No'
    next
  end

  if y1 == y2 && (c > 0 || d > 0)
    puts 'No'
    next
  end

  x += b - a
  y += d - c
  if x1 <= x && x <= x2 && y1 <= y && y <= y2
    puts 'Yes'
  else
    puts 'No'
  end
end

t = gets.to_i
t.times do
  a, b, c, d = gets.split.map(&:to_i)
  x, y, x1, y1, x2, y2 = gets.split.map(&:to_i)
  ok = true
  ok = false unless (x1..x2).include?(x - a + b)
  ok = false unless (y1..y2).include?(y - c + d)
  ok = false if (a + b > 0) && (x1 == x2)
  ok = false if (c + d > 0) && (y1 == y2)
  puts ok ? "YES" : "NO"
end

def solve(d,r)
  d > r
end

t = gets.to_i

t.times do

  l, r, d, u = gets.split.map(&:to_i)
  x, y, xl, yd, xr, yu = gets.split.map(&:to_i)

  rr = xr - x
  ll = x - xl
  uu = yu - y
  dd = y - yd

  m = rr
  m = ll if m > ll
  m = uu if m > uu
  m = dd if m > dd

  if l < r
    if solve(r-l, rr)
      puts "No"
      next
    end
  elsif l > r
    if solve(l-r, ll)
      puts "No"
      next
    end
  elsif l == r && l > 0
    if ll == 0 && rr == 0
      puts "No"
      next
    end
  end

  if d < u
    if solve(u-d,uu)
      puts "No"
      next
    end
  elsif d > u
    if solve(d-u,dd)
      puts "No"
      next
    end
  elsif d == u && d > 0
    if dd == 0 && uu == 0
      puts "No"
      next
    end
  end

  puts "Yes"
end

def solve(d,r)
  d > r
end

t = gets.to_i

t.times do

  l, r, d, u = gets.split.map(&:to_i)
  x, y, xl, yd, xr, yu = gets.split.map(&:to_i)

  rr = xr - x
  ll = x - xl
  uu = yu - y
  dd = y - yd

  m = rr
  m = ll if m > ll
  m = uu if m > uu
  m = dd if m > dd

  if l < r
    if solve(r-l, rr)
      puts "No"
      next
    end
  elsif l > r
    if solve(l-r, ll)
      puts "No"
      next
    end
  elsif l == r && l > 0
    if ll == 0 || rr == 0
      puts "No"
      next
    end
  end

  if d < u
    if solve(u-d,uu)
      puts "No"
      next
    end
  elsif d > u
    if solve(d-u,dd)
      puts "No"
      next
    end
  elsif d == u && d > 0
    if dd == 0 || uu == 0
      puts "No"
      next
    end
  end

  puts "Yes"
end


gets
dp,arr=[0],gets.split.map(&:to_i)
arr.each_with_index{|i,k|dp[k+1]=(dp[k]*2-dp[i-1]+2)%1000000007}
p dp[-1]

def f(x)
  if @d[x] != nil
    return @d[x]
  end
  sum = 1
  for i in @p[x]..x - 1
    sum += f(i)
  end
  sum += 1
  @d[x] = sum
  sum
end

n = Integer gets
@p = gets.split.map { |x| (Integer x) - 1 }
@d = Array.new(n, nil)
@d[0] = 2
sum = 0
for i in 0..n - 1
  sum += f i
end
puts sum % (10 ** 9 + 7)

MOD = 1000000007
n = STDIN.readline.to_i
p = [0] + STDIN.readline.split.map { |x| x.to_i }

f = Array.new(n+2,0)
2.upto(n+1) do |i|
	f[i] = f[i-1]+1
	t = i-1
	while p[t] != t ;
		f[i] += f[t]-f[p[t]+1]+2
		flag = true
		t = p[t]
	end
	f[i] += 1
	f[i] %= MOD
end

print f[n+1]


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

ans = 0
sz =ary.size

1.upto(sz-1).each do |i|
  ans += (ary[i] - ary[i-1] - 1)
end

puts ans

n,*a=$<.read.split.map &:to_i
p a.max-a.min+1-n

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

gets
a = read_vector_int
puts a.max - a.min - a.size + 1

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

puts as[-1] - as[0] - n + 1

remain_keyborads = gets.chomp.to_i
values = gets.chomp

remain_keybord_indicies = values.split(' ')
remain_keybord_indicies.map!(&:to_i)

max = remain_keybord_indicies.max
min = remain_keybord_indicies.min

puts max - min - remain_keyborads + 1

gets
a = gets.split.map(&:to_i).sort
i = 1
ans = 0
while i < a.size
    ans += a[i] - a[i - 1] - 1
    i += 1
end
puts ans

n,*a=$<.read.split.map &:to_i
p n-a.max+a.min-1


#! ruby -Ku
# -*- coding: UTF-8 -*-
a = gets

str_front = "heavy"
str_rear = "metal"

count_front = 0
sum_substring = 0

offset_front = 0
offset_rear = 0
while true
  offset_front_temp = a.index(str_front, offset_front)
  offset_rear_temp = a.index(str_rear, offset_rear)
  if offset_front_temp == nil
    offset_front_temp = 10 ** 6
    offset_front = 10 ** 6
  end
  if offset_rear_temp == nil
    break
  end
  if offset_front_temp < offset_rear_temp
    count_front += 1
    offset_front = offset_front_temp + 4
  else
    sum_substring += count_front
    offset_rear = offset_rear_temp + 4
  end
end

puts sum_substring

s = gets
count = 0
heavy = 0
for i in 0..s.length-5
	if s[i..i+4] == "heavy"
		heavy += 1
	end

	if s[i..i+4] == "metal"
		count += heavy
	end
end

puts count

#!/usr/bin/env ruby
str = gets
harr = []
marr = []
index = -1
loop do
  index = str.index('heavy', index + 1)
  break unless index
  harr << index
end
index = -1
loop do
  index = str.index('metal', index + 1)
  break unless index
  marr << index
end
ret = 0
match = 0
harr.each do |hi|
  find = false
  for i in match...marr.size
    match = i
    if marr[i] > hi
      find = true
      break
    end
  end
  ret += marr.size - match if find
end
puts ret

def get_pos(s, ss)
  x, l = [], ss.length
  i = -l
  while true
    i = s.index(ss, i + l)
    break if i.nil?
    x << i
  end
  x
end

s = gets.chomp
x = get_pos(s, "heavy")
y = get_pos(s, "metal")
total, pox, poy, sx, sy = 0, 0, 0, x.size, y.size
while pox < sx
  poy += 1 while poy < sy && y[poy] < x[pox]
  total += sy - poy
  pox += 1
end
puts total

input = gets.chomp
heavy = 0
itr = 0
len = input.length
ans = 0

until itr >= len
	if input[itr] == 'h'
		if input[itr..(itr+4)] == "heavy"
			heavy += 1
			itr += 5
		else itr += 1
		end
	elsif input[itr] == 'm'
		if input[itr..(itr+4)] == "metal"
			ans += heavy
			itr += 5
		else 	itr += 1
		end
	else
		itr += 1
	end
end

puts ans

s = gets.strip
h = []
m = []
offset = 0

while n = s.index("heavy", offset) do
  h << n
  offset = n + 1
end

if h.size == 0 then
  puts 0
  exit
end

offset = 0
total = 0
h_idx = -1
while n = s.index("metal", offset) do
  while h_idx+1 < h.size and h[h_idx+1] < n do
    h_idx += 1
  end
  total += h_idx + 1
  offset = n + 1
end

puts total

S = gets.chomp

h   = 0
ans = 0

(0...S.length).each do |i|

  h   += 1 if S[i,5] == "heavy"
  ans += h if S[i,5] == "metal"

end

puts ans

s = gets

res = 0
i=-1
h = []
loop do
  i = s.index('heavy', i+1)
  break if i.nil?
  h << i
end

i=-1
j=0
loop do
  i = s.index('metal', i+1)
  break if i.nil?

  while j < h.size and h[j] < i
    j+=1
  end
  res += j
end

puts res

#!/usr/bin/env ruby

def get_pos(s, ss)
	x, l = [], ss.length
	i = -l
	while true
		i = s.index(ss, i + l)
		break if i.nil?
		x << i
	end
	x
end

s = gets.chomp
x = get_pos(s, "heavy")
y = get_pos(s, "metal")
tot, px, py, sx, sy = 0, 0, 0, x.size, y.size
while px < sx
	py += 1 while py < sy && y[py] < x[px]
	tot += sy - py
	px += 1
end
puts tot

#!/usr/bin/env ruby

def get_pos(s, ss)
	x, ls, lss = [], s.length, ss.length
	ls.times { |i| x << i if s[i..i+lss-1] == ss }
	x
end

s = gets.chomp
x = get_pos(s, "heavy")
y = get_pos(s, "metal")
tot, px, py, sx, sy = 0, 0, 0, x.size, y.size
while px < sx
	py += 1 while py < sy && y[py] < x[px]
	tot += sy - py
	px += 1
end
puts tot

#!/usr/bin/env ruby
str = gets
harr = []
marr = []
index = -1
loop do
  index = str.index('heavy', index + 1)
  break unless index
  harr << index
end
index = -1
loop do
  index = str.index('metal', index + 1)
  break unless index
  marr << index
end
ret = 0
harr.each do |hi|
  match = 0
  for i in match...marr.size
    match = i
    break if marr[i] >= hi
  end
  ret += marr.size - match
end
puts ret

def get_pos(s,ss)
  x, l = [],ss.length
  i = -l
  while true
    i = s.index(ss,i+1)
    break if i.nil?
    x << i
  end
  x
end

s = gets.chomp
x = get_pos(s,"heavy")
y = get_pos(s,"metal")
total,pox,poy,sx,sy = 0,0,0,x.size,y.size
while pox < sx
  py += 1 while poy < sy && y[poy] < x[pox]
  total += sy - poy
  pox += 1
end
puts total

def get_pos(s,ss)
  x, l = [],ss.length
  i = -l
  while true
    i = s.index(ss,i+1)
    break if i.nil?
    x << i
  end
  x
end

s = gets.chomp
x = get_pos(s,"heavy")
y = get_pos(s,"metal")
total,pox,poy,sx,sy = 0,0,0,x.size,y.size
while pox < sx
  py += 1 while poy < sy && y[py] < x[pox]
  total += sy - poy
  pox += 1
end
puts total

#! ruby -Ku
# -*- coding: UTF-8 -*-
a = gets
# puts a

str_front = "heavy"
str_rear = "metal"

offset_front = a.index(str_front)
offset_front_array = [a.index(str_front)]
while offset_front != nil
  offset_front = a.index(str_front, offset_front + 1)
  offset_front_array << offset_front
end

offset_rear = a.index(str_rear)
offset_rear_array = [a.index(str_rear)]
while offset_rear != nil
  offset_rear = a.index(str_rear, offset_rear + 1)
  offset_rear_array << offset_rear
end

# puts offset_front_array
# puts offset_rear_array

count_front = 0
sum_substring = 0

offset_rear = offset_rear_array.first
while offset_rear != nil
  count_front += 1
  val_front = offset_front_array.shift
  val2_front = offset_front_array.first
  offset_rear = offset_rear_array.shift
  if offset_rear == nil
    break
  elsif val2_front == nil
    while offset_rear != nil
      sum_substring += count_front
      offset_rear = offset_rear_array.shift
    end
  else
    while val_front < offset_rear && offset_rear < val2_front
      sum_substring += count_front
      offset_rear = offset_rear_array.first
      if offset_rear == nil
        break
      end
    end
  end
end

puts sum_substring

#! ruby -Ku
# -*- coding: UTF-8 -*-
a = gets

str_front = "heavy"
str_rear = "metal"

offset_front = a.index(str_front)
offset_front_array = [a.index(str_front)]
while offset_front != nil
  offset_front = a.index(str_front, offset_front + 1)
  offset_front_array << offset_front
end

offset_rear = a.index(str_rear)
offset_rear_array = [a.index(str_rear)]
while offset_rear != nil
  offset_rear = a.index(str_rear, offset_rear + 1)
  offset_rear_array << offset_rear
end

# puts offset_front_array
# puts offset_rear_array

count_front = 0
sum_substring = 0

while (offset_rear = offset_rear_array.first) != nil
  count_front += 1
  interval_front = offset_front_array.first(2)
  first_front = interval_front.shift
  second_front = interval_front.shift
  if first_front == nil
    first_front = -1
  end
  if second_front == nil
    second_front = 10**6
  end
  while (first_front < offset_rear) && (offset_rear < second_front)
    sum_substring += count_front
    offset_rear_array.shift
    offset_rear = offset_rear_array.first
    if offset_rear == nil
      break
    end
  end
  offset_front_array.shift
end

puts sum_substring

s = gets
count = 0
for i in (0..s.length) do
	a = s.index("heavy", i)
	if a and a == i
		b = s.index("metal", i)
		if b and b > a
			count = count + 1
		end
	end
end

print count