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

puts (as.inject(:+)/x).abs.ceil

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @cards_num, @max_abs = @dats[0].split(" ").map{|x| x.to_i}
    @cards = @dats[1].split(" ").map{|x| x.to_i}
  end

  def solve
    sum = @cards.inject(:+).abs
    return 1 if sum != 0 && sum <= @max_abs-1
    return 0 if @max_abs == 0

    count_ret = 0
    (@max_abs).downto(1) do |elem|
      if sum >= elem
        d = sum / elem
        sum -= elem * d
        count_ret += d
      elsif sum == 0
        return count_ret
      end
    end
    return count_ret
  end
end

solver = ProblemA.new
p solver.solve

a = readline.split().map {|x| x.to_i}
s = 0
readline.split().map {|i| s += i.to_i}
puts (s.abs + a[1] - 1) / a[1]

n, x = readline.split().map {|i| i.to_i}
s = 0
readline.split().map {|i| s += i.to_i}
puts (s.abs + x - 1) / x

a=gets.chomp.split(" ")
ans=0.0
b=gets.chomp.split(" ").each {|i|  ans+=i.to_i  }
ans=ans.abs
ans=(ans/a[1].to_i).ceil
puts "#{ans}"

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

sum = gets.split.collect{|x| x.to_i}.inject{|y, x| y += x}

sum = sum.abs

k = (sum + x -1)/x

p k

n,x = gets.chomp!.split.map { |e|  e.to_i}
sum = gets.split.collect{|x| x.to_i}.inject{|y, x| y += x}
p (sum.abs + x -1)/x

input = gets.chomp
num = input.split
n = Integer(num[0])
x = Integer(num[1])
input2 = gets.chomp
nums= input2.split
card = Array.new
for i in 0...n
    card[i] = Integer(nums[i])
end

sum = 0
for i in 0...n
    sum += card[i]
end
if sum == 0
    puts 0
else
    if sum.abs <= x
        puts 1
    else
        if sum.abs % x == 0
            puts sum.abs / x
        else
            puts sum.abs / x + 1
        end
    end
end

card = gets.split(" ").map { |e| e.to_i }
mas = gets.split(" ").map { |e| e.to_i }
sum = 0
mas.map { |e| sum += e }
sum = sum.abs
puts (sum.abs + card[1] - 1) / card[1]

n, m = $stdin.gets.split(' ').map{|e| e.to_i}
cards = $stdin.gets.split(' ').map{|e| e.to_i}

result = (cards.inject(:+).abs.to_f / m).ceil
puts result

first_line = (gets()).split()
second_line = (gets()).split()

found_num = (first_line.shift).to_i
max_abs = (first_line.shift).to_i
num_ary = 0

second_line.each do |c|
	num_ary += c.to_i
end
result = (num_ary).abs / max_abs
if ((num_ary % max_abs) != 0)
	result += 1
end
p result

n, x = gets.split(' ').map(&:to_i)
s = gets.split(' ').map(&:to_i).inject(:+).abs
puts s/x + ( s%x == 0 ? 0 : 1 )

n,x = gets.chomp.split

g = gets.chomp.split

n = n.to_i
x = x.to_i
s = 0

for i in 0...n
  s += g[i].to_i
end

s = s.abs

s = s/x.to_f

s = s.ceil

puts s.to_i

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

#!/usr/bin/ruby
n,x=gets.split.map(&:to_i)
s=gets.split.map(&:to_i).reduce(:+).abs
p (s+x-1)/x

n, x = gets.chomp.split(" ").map{ |e| e.to_i }
cards = gets.chomp.split(" ").map{ |e| e.to_i }

sum = cards.inject{ |s, i| s += i }.abs
puts (sum.to_f / x.to_f).ceil

def run
  n, x = $stdin.gets.split.map(&:to_i)
  c = $stdin.gets.split.map(&:to_i)
  s = c.reduce(&:+)
  puts (s.abs.to_f / x).ceil
end

run if $0 == __FILE__

def solve(max, cards)
  sum = cards.inject(:+).abs
  div = sum / max
  rem = sum % max
  div += 1 if rem > 0
  div
end

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

if __FILE__ == $0
  puts solve(read_next_line[1], read_next_line)
end

n, x = gets.split.map(&:to_i)
given = gets.split.map(&:to_i)
sum = 0

given.each do |i|
    sum += i
end
ans = sum.abs / Float(x)
ans = ans.ceil
p Integer(ans)

n, x = gets.split.map &:to_i
sum = gets.split.map { |x| x.to_i }.inject(:+)
puts (sum.abs/x.to_f).ceil

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

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

    sum = num_list.inject(:+)

    puts (sum.abs/x.to_f).ceil
  end
end

A.new

#f = File.open('test1.txt', 'r')
f = STDIN
n, x = f.gets.chomp.split.map &:to_i
sum = f.gets.chomp.split.map(&:to_i).reduce(&:+).abs
puts (sum / x + (sum % x != 0 ? 1 : 0))

ri=->{gets.split.map(&:to_i)}
n,x=ri[]
p (ri[].inject(:+).abs+x-1)/x

n, x = gets.split.map { |x| x.to_i }
sum = gets.split.map { |x| x.to_i }.reduce(:+).abs
puts (sum + x - 1) / x

(n , x) = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
(sum , res) = 0,0
n.times do |i|
  sum += arr[i]
end

sum = sum.abs
if sum != 0
  res += (sum/x) + (sum%x==0?0:1)
end
puts res

n, x = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
sum = a.reduce(:+).abs
p (sum + x - 1) / x

input = gets.chomp
num = input.split
n = Integer(num[0])
x = Integer(num[1])
input2 = gets.chomp
nums= input2.split
card = Array.new
for i in 0...n
    card[i] = Integer(nums[i])
end

sum = 0
for i in 0...n
    sum += card[i]
end
if sum.abs <= x
    puts 1
else
    if sum.abs % x == 0
        puts sum.abs / x
    else
        puts sum.abs / x + 1
    end
end

card = gets.split(" ").map { |e| e.to_i }
mas = gets.split(" ").map { |e| e.to_i }
sum = 0
mas.map { |e| sum += e }
if sum % 2 != 0 then
    sum = sum / 2 + 1
else
    sum = sum / 2
end
puts sum.abs

card = gets.split(" ").map { |e| e.to_i }
mas = gets.split(" ").map { |e| e.to_i }
sum = 0
mas.map { |e| sum += e }
if sum % card[1] != 0 then
    sum = sum / card[1] + 1
else
    sum = sum / card[1]
end
puts sum.abs

def check_pair (pos_ary,neg_ary,surplus_cards)
	if (pos_ary.first and neg_ary.first)
		num = pos_ary.first
		overlap = neg_ary.index(num)
		if overlap
			pos_ary.shift
			neg_ary.delete_at(overlap)
			return check_pair(pos_ary,neg_ary,surplus_cards)
		else
			pos_ary.shift
			surplus_cards += 1
			return check_pair(pos_ary,neg_ary,surplus_cards)
		end
	else
		surplus_cards = pos_ary.length + neg_ary.length
		return surplus_cards
	end
end

first_line = (gets()).split()
second_line = (gets()).split()
pos_cards = []
neg_cards = []

found_num = (first_line.shift).to_i
mux_abs = (first_line.shift).to_i

second_line.each do |c|
	num = c.to_i
	if num > 0
		pos_cards << num
	elsif num < 0
		neg_cards << num
	end
end

p check_pair(pos_cards,neg_cards,0)

n, x = gets.split.map(&:to_i)
given = gets.split.map(&:to_i)
sum = 0

given.each do |i|
    sum += i
end
sum += x - 1
p sum / x

n, x = gets.split.map(&:to_i)
given = gets.split.map(&:to_i)
sum = 0

given.each do |i|
    sum += i
end
p (sum / x).abs.ceil

#f = File.open('test1.txt', 'r')
f = STDIN
n, x = f.gets.chomp.split.map &:to_i
sum = f.gets.chomp.split.map(&:to_i).reduce(&:+).abs
puts (sum / x + sum % x)

#!/usr/bin/ruby
n,x=gets.split.map(&:to_i)
s=gets.split.map(&:to_i).reduce(:+).abs
p (x+s-1)/s

n, x = gets.split.map &:to_i
sum = gets.split.map { |x| x.to_i }.inject(:+)
puts (sum/x).ceil.abs

n, x = gets.split.map &:to_i
sum = gets.split.map { |x| x.to_i }.inject(:+)
puts (sum/x.to_f).ceil.abs

(n , x) = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
print arr
sum = 0
res = 0
n.times do |i|
  sum += arr[i]
end

sum = sum.abs
if sum != 0
  res += (sum/x)+1
end
puts res

(n , x) = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
(sum , res) = 0,0
n.times do |i|
  sum += arr[i]
end

sum = sum.abs
if sum != 0
  res += (sum/x)+1
end
puts res

(n , x) = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
sum = 0
res = 0
n.times do |i|
  sum += arr[i]
end

sum = sum.abs
if sum != 0
  res += (sum%x)+1
end
puts res

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @cards_num, @max_abs = @dats[0].split(" ").map{|x| x.to_i}
    @cards = @dats[1].split(" ").map{|x| x.to_i}
  end

  def solve
    sum = @cards.inject(:+).abs
    return 1 if sum <= @max_abs-1
    return 0 if @max_abs == 0

    count_ret = 0
    (@max_abs).downto(1) do |elem|
      if sum >= elem
        d = sum / elem
        sum -= elem * d
        count_ret += d
      elsif sum == 0
        return count_ret
      end
    end
    return count_ret
  end
end

solver = ProblemA.new
p solver.solve

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @cards_num, @max_abs = @dats[0].split(" ").map{|x| x.to_i}
    @cards = @dats[1].split(" ").map{|x| x.to_i}
  end

  def solve
    sum = @cards.inject(:+).abs
    return 1 if sum <= @max_abs
    return 0 if @max_abs == 0

    count_ret = 0
    (@max_abs-1).downto(1) do |elem|
      if sum > elem
        count_ret += 1
        sum -= elem
      elsif sum == 1
        return count_ret+1
      end
    end
    return count_ret
  end
end

solver = ProblemA.new
p solver.solve

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @cards_num, @max_abs = @dats[0].split(" ").map{|x| x.to_i}
    @cards = @dats[1].split(" ").map{|x| x.to_i}
  end

  def solve
    sum = @cards.inject(:+).abs
    return 1 if sum <= @max_abs
    return 0 if @max_abs == 0

    count_ret = 0
    (@max_abs-1).downto(1) do |elem|
      if sum >= elem
        count_ret += 1
        sum -= elem
      elsif sum == 1
        return count_ret+1
      elsif sum == 0
        return count_ret
      end
    end
    return count_ret
  end
end

solver = ProblemA.new
p solver.solve

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @cards_num, @max_abs = @dats[0].split(" ").map{|x| x.to_i}
    @cards = @dats[1].split(" ").map{|x| x.to_i}
  end

  def solve
    sum = @cards.inject(:+).abs
    return 1 if sum <= @max_abs
    return 0 if @max_abs == 0

    count_ret = 0
    (@max_abs-1).downto(1) do |elem|
      if sum >= elem
        count_ret += 1
        sum -= elem
      elsif sum == 0
        return count_ret
      end
    end
    return count_ret
  end
end

solver = ProblemA.new
p solver.solve

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @cards_num, @max_abs = @dats[0].split(" ").map{|x| x.to_i}
    @cards = @dats[1].split(" ").map{|x| x.to_i}
  end

  def solve
    sum = @cards.inject(:+).abs
    return 1 if sum <= @max_abs-1
    return 0 if @max_abs == 0

    count_ret = 0
    (@max_abs-1).downto(1) do |elem|
      if sum >= elem
        d = sum / elem
        sum -= elem * d
        count_ret += d
      elsif sum == 0
        return count_ret
      end
    end
    return count_ret
  end
end

solver = ProblemA.new
p solver.solve

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @cards_num, @max_abs = @dats[0].split(" ").map{|x| x.to_i}
    @cards = @dats[1].split(" ").map{|x| x.to_i}
  end

  def solve
    sum = @cards.inject(:+).abs
    return 1 if sum <= @max_abs
    return 0 if @max_abs == 0

    count_ret = 0
    @max_abs.downto(1) do |elem|
      if sum > elem
        count_ret += 1
        sum -= elem
      elsif sum == 1
        return count_ret+1
      end
    end
    return count_ret
  end
end

solver = ProblemA.new
p solver.solve

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @cards_num, @max_abs = @dats[0].split(" ").map{|x| x.to_i}
    @cards = @dats[1].split(" ").map{|x| x.to_i}
  end

  def solve
    sum = @cards.inject(:+).abs
    return 1 if sum <= @max_abs-1
    return 0 if @max_abs == 0

    count_ret = 0
    (@max_abs-1).downto(1) do |elem|
      if sum >= elem
        count_ret += 1
        sum -= elem
      elsif sum == 0
        return count_ret
      end
    end
    return count_ret
  end
end

solver = ProblemA.new
p solver.solve


n, k = gets.strip.split.map(&:to_i)
ary = gets.strip.split.map(&:to_i)
hsh = {}
ary.each {|x| hsh[x] = (hsh[x] || 0) + 1 }
ary.sort!
ary.uniq!
remaining_ary = []
un = ary.size
0.upto(un-2).each do |j|
  i =  j + 1
  next if ary[j] < ary[i] && ary[i] <= (ary[j]+k)
  remaining_ary << ary[j]
end
remaining_ary << ary.last
puts remaining_ary.map{|x| hsh[x] }.inject(:+)

def read_line
  gets.split.map{|e| e.to_i }
end

n, k = read_line
a = read_line.sort

count = 0

nearest_bigger = []
nearest_bigger[a.count - 1] = a[a.count -1]

(0...n-1).reverse_each do |index|
  cur = a[index]
  next_val = a[index+1]
  if cur == next_val
    nearest_bigger[index] = nearest_bigger[index+1]
  else
    nearest_bigger[index] = next_val
  end
end

a[0...-1].to_enum.with_index.each do |val, index|
  next_val = nearest_bigger[index]
  if val < next_val && (val + k) >= next_val
    count += 1
  end
end

puts n-count

# http://codeforces.com/problemset/problem/990/B

def count
  n, k = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)
  a.sort!
  # puts a.inspect

  return n if a.first == a.last || n == 1

  s = 0
  c = 0
  1.upto(n - 1) do |i|
    next if a[i] - a[i-1] <= k

    c += skip_count(a, i-1, s)
    s = i
  end

  c += skip_count(a, n-1, s)

  return n - c
end

def skip_count(a, startIdx, endIdx)
  c = 0
  startIdx.downto(endIdx) do |i|
    if a[i] == a[startIdx]
      next
    end

    c = (i - endIdx + 1)
    break
  end

  c
end

puts count

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

b.sort!

n = 0
prev = b.first
prev_cnt = 0
b.each do |i|
  if i > prev
    if i > prev + k
      n += prev_cnt
    end
    prev = i
    prev_cnt = 1
  else
    prev_cnt +=1
  end
end
n += prev_cnt
puts n

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

c = 1
s = 1
unique = true
1.upto(n - 1) do |i|
  unique = false if (a[i] != a[i - 1]) && unique
  next if a[i] - a[i-1] <= k

  (i - 1).downto(s) do |j|
    if a[j] == a[i - 1]
      c += 1
      next
    end

    c += 1
    break
  end

  s = i
end

if unique
  puts n
else
  puts c
end


gets.to_i.times do
  gets
  puts gets.split(' ').map(&:to_i).uniq.join(' ')
end

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map(&:to_i)
	flag = [false] * (n + 1)
	puts a.reduce([]) { |acc, i|
			if !flag[i]
				acc << i
				flag[i] = true
			end
			acc
	     }.join(' ')
end

# frozen_string_literal: true

lines = gets.to_i
lines.times do
  numbers = gets.to_i
  number_permutation = gets.split(' ').map(&:to_i)
  find_map = {}
  number_permutation.each do |i|
    next if find_map[i]

    find_map[i] = true
  end
  print find_map.keys.join(' '), "\n"
end

n = gets.chomp.to_i
n.times do
    b = gets.chomp.to_i
    atr = gets.chomp.split(" ")
    retsu = []
    until atr == []
        a = atr.shift
       # p retsu
        if !retsu.include?(a)
            retsu << a
        end
    end
    #p retsu
    retsu.delete(" ")
    puts retsu.join(" ")
end

tc = gets.to_i

tc.times do
    n = gets.to_i
    arr = gets.split().map{|i| i.to_i}
    dic = Hash.new(0)
    ans = []
    arr.each do |x|
        if(dic[x] == 0)
            ans << x
        end
        dic[x] += 1
    end
    puts ans.join(" ")
end

gets.to_i.times do
    n = gets.to_i
    a = gets.split.map(&:to_i)
    used = [false] * n
    res = []
    (0...2 * n).each do |i|
        if !used[a[i] - 1]
            used[a[i] - 1] = true
            res << a[i]
        end
    end
    puts res.join ' '
end

def solve
  n = gets.strip.to_i
  a = gets.strip.split(' ').map(&:to_i)
  v = []
  (1..n).each { |e| v[e] = true }
  a.each { |e|
    break if n.zero?
    if v[e] == true
      print "#{e} "
      v[e] = false
    end
  }
  puts
end

gets.strip.to_i.times { solve }

gets.to_i.times do
  gets
  puts gets.split.map(&:to_i).uniq.join(' ')
end

n = gets.chomp.to_i
n.times do
    b = gets.chomp.to_i
    atr = gets.chomp.split("")
    retsu = []
    until atr == []
        a = atr.shift
        if !retsu.include?(a)
            retsu << a
        end
    end
    retsu.delete(" ")
    puts retsu.join(" ")
end


n, m = STDIN.gets.split(' ').map{|x| x.to_i}
a = []

m.times do |i|
  a.push(STDIN.gets.split(' ').map{|y| y.to_i - 1});
end

=begin
n, m = 5, 6
a = ['1 2',
'1 3',
'2 3',
'2 4',
'3 4',
'4 5'].map{|x| x.split(' ').map{|y| y.to_i - 1}}
=end
musketeers = Array.new(n){|x| {'known'=>{}, 'knowncount'=> 0}}
a.map do |elem|
  musketeers[elem[0]]['knowncount'] +=1;
  musketeers[elem[0]]['known'][elem[1]]=true;
  musketeers[elem[1]]['knowncount'] +=1;
  musketeers[elem[1]]['known'][elem[0]]=true;
end

# получить тройки знающих
troikas = [];
a.map do |x|
  unite = musketeers[x[0]]['known'].keys & musketeers[x[1]]['known'].keys

  unite.map{|y| troikas.push([y, x[0],x[1] ].sort) }

end

troikas.uniq!

res = troikas.map{|x|  -6 + x.inject(0){|result, y| result+= musketeers[y]['knowncount']}}.min
res = -1 if res == nil
puts res

=begin
      ____________
     /            \
    /  /\      /\  \
   /  /  \    /  \  \
   \                /
    \     \___/    /
     \____________/
=end

class Solve
  def run()
    str = gets.split
    n , m = Integer(str[0]), Integer(str[1])
    graph = Array.new(n) {Array.new(n,false)}
    graph2 = Array.new(n) {Array.new}
    degree = Array.new(n,0)
    for i in 1..m
      str = gets.split
      u , v = Integer(str[0]),Integer(str[1])
      if v < u
        u, v = v , u
      end
      u -= 1
      v -= 1
      graph[u][v] = graph[v][u] = true
      graph2[u].push(v)
      degree[u] += 1
      degree[v] += 1
    end
    mn = 4000 * 4000 * 4000 * 2
    for i in 0..n-1
      for j in 0..graph2[i].length-2
        for k in j+1..graph2[i].length-1
          if graph[graph2[i][j]][graph2[i][k]] == true and degree[i] + degree[graph2[i][j]] + degree[graph2[i][k]] < mn
            mn = degree[i] + degree[graph2[i][j]] + degree[graph2[i][k]]
          end
        end
      end
    end
    if mn == 4000 * 4000 * 4000 * 2
      puts -1
    else
      puts (mn - 6)
    end
  end
end

Solve.new.run

q = Hash.new([])
n, m = gets.split.map(&:to_i)
glory = 0
out = -1

m.times do
  a, b = gets.split.map(&:to_i)
  q[a] += [b]
  q[b] += [a]
end

q.delete_if{ |key, value| value.size < 2 }

q.each do |key, value|

  for i in 0...value.size-1 do
    next unless q.key?(value[i])

    for j in i+1..value.size-1 do
      next unless q.key?(value[j])
      next unless q[value[j]].include?(value[i])

      glory = -6
      glory += q[value[i]].size
      glory += q[value[j]].size
      glory += q[key].size
      out = glory if out > glory || out == -1

    end
  end
end

puts out

require 'set'

n, m = gets.split.map &:to_i
neighbour = (1..n).map{0}
degree = (1..n).map{0}

edges = []
m.times {
	i, j = gets.split.map(&:to_i)
	i -= 1
	j -= 1
	neighbour[i] |= (1 << j)
	neighbour[j] |= (1 << i)
	degree[i] += 1
	degree[j] += 1
	edges << [i, j]
}

min = Float::INFINITY

edges.each{|i, j|
	d0 = degree[i] + degree[j]
	x = neighbour[i] & neighbour[j]
	(0..n-1).each{|k|
		if x[k] == 1
			d = d0 + degree[k]
			min = d if d < min
		end
	}
}

#(0..n-1).sort_by{|i| degree[i]}.each{|i|
#	d1 = degree[i]
#	break if d1 * 3 >= min
#
#	edges.reject!{|j, k|
#		if j == i || k == i
#			true
#		else
#			if neighbour[i].include?(j) && neighbour[i].include?(k)
#				d2, d3 = degree[j], degree[k]
#				d = d1 + d2 + d3
#				min = d if d < min
#			end
#			false
#		end
#	}
#}
#

p min == Float::INFINITY ? -1 : min - 6

n, m = gets.chomp.split(' ').map(&:to_i)
as = []
bs = []
m.times do
  a, b = gets.chomp.split(' ').map(&:to_i)
  as << a
  bs << b
end

rs = [[]]
n.times do
  rs << []
end

hs = Hash.new(0)
for i in 0..m-1
  hs[as[i]] += 1
  hs[bs[i]] += 1
  rs[as[i]] << bs[i]
  rs[bs[i]] << as[i]
end

rtn = -1
for m1 in 1..n-2
  r = rs[m1]
  if 1 < r.size
    for j in 0..r.size-2
      m2 = r[j]
      for k in j+1..r.size-1
        m3 = r[k]
        if rs[m2].include?(m3)
          if rtn < 0 || hs[m1] + hs[m2] + hs[m3] - 6 < rtn
            rtn = hs[m1] + hs[m2] + hs[m3] - 6
          end
        end
      end
    end
  end
end

puts rtn

=begin
      ____________
     /            \
    /  /\      /\  \
   /  /  \    /  \  \
   \                /
    \     \___/    /
     \____________/
=end

class Solve
  def run()
    str = gets.split
    n , m = Integer(str[0]), Integer(str[1])
    graph = Array.new(n) {Array.new(n,false)}
    graph2 = Array.new(n) {Array.new}
    degree = Array.new(n,0)
    for i in 1..m
      str = gets.split
      u , v = Integer(str[0]),Integer(str[1])
      if v < u
        u, v = v , u
      end
      u -= 1
      v -= 1
      graph[u][v] = graph[v][u] = true
      graph2[u].push(v)
      degree[u] += 1
      degree[v] += 1
    end
    mn = 4000 * 4000 * 4000 * 2
    for i in 0..n-1
      for j in 0..graph2[i].length-2
        for k in j+1..graph2[i].length-1
          if graph[graph2[i][j]][graph2[i][k]] == true and degree[i] + degree[graph2[i][j]] + degree[graph2[i][k]] < mn
            mn = degree[i] + degree[graph2[i][j]] + degree[graph2[i][k]]
          end
        end
      end
    end
    puts (mn - 6)
  end
end

Solve.new.run

q = Hash.new([])
n, m = gets.split.map(&:to_i)
glory = 0
out = -1

m.times do
  a, b = gets.split.map(&:to_i)
  q[a] += [b]
  q[b] += [a]
end

q.delete_if{ |key, value| value.size < 2 }

q.each do |key, value|
  for i in 0...value.size-1 do
    unless q.key?(value[i])
      glory = 0
      break
    end
    for j in i+1..value.size-1 do
      if q.key?(value[j])

        if q[value[j]].include?(value[i])
          glory += q[value[i]].size
          glory += q[value[j]].size
          glory += q[key].size
          glory -= 6
          out = glory if out > glory || out == -1
        else
          glory = 0
          break
        end
      else
        glory = 0
        break
      end

    end
  end
end

puts out

q = Hash.new([])
n, m = gets.split.map(&:to_i)
glory = 0
out = -1

m.times do
  a, b = gets.split.map(&:to_i)
  q[a] += [b]
  q[b] += [a]
end

q.delete_if{ |key, value| value.size < 2 }

q.each do |key, value|

  for i in 0...value.size-1 do
    break unless q.key?(value[i])

    for j in i+1..value.size-1 do
      break unless q.key?(value[j])
      break unless q[value[j]].include?(value[i])

      glory = -6
      glory += q[value[i]].size
      glory += q[value[j]].size
      glory += q[key].size
      out = glory if out > glory || out == -1

    end
  end
end

puts out


gets.to_i.times do
  n = gets.to_i
  a = gets.split.map &:to_i
  i = 0
  (0..100).each do |x|
    if j = a.index(x)
      a[i], a[j] = a[j], a[i]
    else
      break
    end
    i += 1
  end
  puts a.join ' '
end

gets.to_i.times {
  gets
  arr = gets.split(' ').map(&:to_i).sort
  res = []
  res_tail = []
  arr.each { |el|
    if el == res[-1]
      res_tail.push(el)
    else
      res.push(el)
    end
  }
  puts [*res, *res_tail].join(' ')
}

def solve(n, a)
    mex = 0
    b = []
    n.times {
        if (i = a.index(mex))
            b << a.delete_at(i)
            mex += 1
        else
            break
        end
    }
    b += a
    b
end

def main
    t = int
    t.times do
        n = int
        a = ints
        puts solve(n, a) * " "
    end
end

DEBUG = true
MOD = 10**9+7
YESNO = %w(No Yes)
INF = 10**9

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

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

class TrueClass
include Boolean
def to_i; 1 end
end

class FalseClass
include Boolean
def to_i; 0 end
end

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

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

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

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

main

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map &:to_i
  a.sort!
  res = []
  (0...n).each do |i|
    res << a[i] if i == 0 || a[i] != a[i - 1]
  end
  (0...n).each do |i|
    res << a[i] if i > 0 && a[i] == a[i - 1]
  end
  puts res.join ' '
end

t = gets.chomp.to_i
for test in (1..t)
  n = gets.chomp.to_i
  a = gets.chomp.split(' ').map { |x| x.to_i }
  c = Array.new(101).fill(0)
  for i in a
    c[i] += 1
  end
  j = 0
  for i in (0..100)
    if c[i] > 0
      c[i] -= 1
      a[j] = i
      j += 1
    end
  end
  for i in (0..100)
    while c[i] > 0
      c[i] -= 1
      a[j] = i
      j += 1
    end
  end
  a.each do |x|
    print x, ' '
  end
  puts
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map &:to_i
  res = []
  (0...n).each do |i|
    res << a[i] if i == 0 || a[i] != a[i - 1]
  end
  (0...n).each do |i|
    res << a[i] if i > 0 && a[i] == a[i - 1]
  end
  puts res.join ' '
end


n = gets.to_i
arr = gets.split(" ").map(&:to_i).sort.reverse
ans = nil
if arr.include?(0)
	sum = arr.inject(0,:+)
	if sum % 3 == 0
		ans = arr.join("")
	elsif sum % 3 == 1

		one = [1,4,7]
		two = [2,5,8]
		l = nil
		one.each do |o|
			l = arr.rindex(o)
			if l
				arr.delete_at(l)
				break
			end
		end

		unless l
			deleted = 0
			two.each do |t|
				l = arr.rindex(t)
				if l
					if deleted == 0
						l1 = arr.index(t)
						if l == l1
							arr.delete_at(l)
							deleted = 1
							next
						else
							arr.delete_at(l)
							arr.delete_at(l1)
							break
						end
					end

					if deleted == 1
						arr.delete_at(l)
					end

				end
			end
		end

		# j = arr.length - 1
		# while j >= 0
		# 	a = arr[j]

		# 	if a % 3 == 1 && one.length < 1
		# 		one << j
		# 		break
		# 	end

		# 	if a % 3 == 2  && two.length < 2
		# 		two << j
		# 	end
		# 	j -= 1
		# end
		# if one.empty?
		# 	arr.delete_at(two[0])
		# 	arr.delete_at(two[1])
		# else
		# 	arr.delete_at(one[0])
		# end

		# puts "here"
		# l = arr.rindex{|a| a % 3 == 1}
		# if l
		# 	arr.delete_at(l)
		# else
		# 	l = arr.rindex{|a| a % 3 == 2}
		# 	arr.delete_at(l)
		# 	l = arr.rindex{|a| a % 3 == 2}
		# 	arr.delete_at(l)
		# end
	else

		one = [1,4,7]
		two = [2,5,8]
		l = nil
		two.each do |o|
			l = arr.rindex(o)
			if l
				arr.delete_at(l)
				break
			end
		end

		unless l
			deleted = 0
			one.each do |t|
				l = arr.rindex(t)
				if l
					if deleted == 0
						l1 = arr.index(t)
						if l == l1
							arr.delete_at(l)
							deleted = 1
							next
						else
							arr.delete_at(l)
							arr.delete_at(l1)
							break
						end
					end

					if deleted == 1
						arr.delete_at(l)
					end

				end
			end
		end

		# one = [1,4,7]
		# two = [2,5,8]
		# j = arr.length - 1
		# while j >= 0
		# 	a = arr[j]

		# 	if a % 3 == 2 && two.length < 1
		# 		two << j
		# 		break
		# 	end

		# 	if a % 3 == 1  && one.length < 2
		# 		one << j
		# 	end
		# 	j -= 1
		# end
		# if two.empty?
		# 	arr.delete_at(one[0])
		# 	arr.delete_at(one[1])
		# else
		# 	arr.delete_at(two[0])
		# end

		# l = arr.rindex{|a| a % 3 == 2}
		# if l
		# 	arr.delete_at(l)
		# else
		# 	l = arr.rindex{|a| a % 3 == 1}
		# 	arr.delete_at(l)
		# 	l = arr.rindex{|a| a % 3 == 1}
		# 	arr.delete_at(l)
		# end
	end
	ans = arr.join("")
	unless /[^0]/ =~ ans
		puts "0"
	else
		puts ans
	end
else
	puts "-1"
end

n = gets.to_i
digits = gets.split.map(&:to_i).sort_by {|el| -el}

unless digits.last==0
  puts -1
  exit
end

sum = digits.inject{|sum,el| sum+=el}
ans = 0
size = digits.size

if sum%3 == 0
  ans =  digits.join unless digits[0] == 0
elsif sum%3 == 1
  (size-1).downto(0) do |i|
    if digits[i]%3==1
      digits.delete_at i
      break
    end
  end
  if digits.size == size
    (size-1).downto(0) do |i|
      if digits[i]%3==2 && ( size - digits.size < 2 )
        digits.delete_at i
      end
    end
  end
  ans =  digits.join unless digits[0] == 0
elsif sum%3 == 2
  (size-1).downto(0) do |i|
    if digits[i]%3==2
      digits.delete_at i
      break
    end
  end
  if digits.size == size
    (size-1).downto(0) do |i|
      if digits[i]%3==1 && ( size - digits.size < 2 )
        digits.delete_at i
      end
    end
  end
  ans =  digits.join unless digits[0] == 0
end

puts (ans[0] == '0' ? 0 : ans)

n = gets.to_i
arr = gets.split(" ").map(&:to_i).sort.reverse
ans = nil
if arr.include?(0)
	sum = arr.inject(0,:+)
	if sum % 3 == 0
		ans = arr.join("")
	elsif sum % 3 == 1

		one = [1,4,7]
		two = [2,5,8]
		l = nil
		one.each do |o|
			l = arr.rindex(o)
			if l
				arr.delete_at(l)
				break
			end
		end

		unless l
			deleted = 0
			two.each do |t|
				l = arr.rindex(t)
				if l
					if deleted == 0
						l1 = arr.index(t)
						if l == l1
							arr.delete_at(l)
							deleted = 1
							next
						else
							arr.delete_at(l)
							arr.delete_at(l1)
							break
						end
					end

					if deleted == 1
						arr.delete_at(l)
					end

				end
			end
		end

		# j = arr.length - 1
		# while j >= 0
		# 	a = arr[j]

		# 	if a % 3 == 1 && one.length < 1
		# 		one << j
		# 		break
		# 	end

		# 	if a % 3 == 2  && two.length < 2
		# 		two << j
		# 	end
		# 	j -= 1
		# end
		# if one.empty?
		# 	arr.delete_at(two[0])
		# 	arr.delete_at(two[1])
		# else
		# 	arr.delete_at(one[0])
		# end

		# puts "here"
		# l = arr.rindex{|a| a % 3 == 1}
		# if l
		# 	arr.delete_at(l)
		# else
		# 	l = arr.rindex{|a| a % 3 == 2}
		# 	arr.delete_at(l)
		# 	l = arr.rindex{|a| a % 3 == 2}
		# 	arr.delete_at(l)
		# end
	else

		one = [1,4,7]
		two = [2,5,8]
		l = nil
		two.each do |o|
			l = arr.rindex(o)
			if l
				arr.delete_at(l)
				break
			end
		end

		unless l
			deleted = 0
			one.each do |t|
				l = arr.rindex(t)
				if l
					if deleted == 0
						l1 = arr.index(t)
						if l == l1
							arr.delete_at(l)
							deleted = 1
							next
						else
							arr.delete_at(l)
							arr.delete_at(l1)
							break
						end
					end

					if deleted == 1
						arr.delete_at(l)
					end

				end
			end
		end

		# one = [1,4,7]
		# two = [2,5,8]
		# j = arr.length - 1
		# while j >= 0
		# 	a = arr[j]

		# 	if a % 3 == 2 && two.length < 1
		# 		two << j
		# 		break
		# 	end

		# 	if a % 3 == 1  && one.length < 2
		# 		one << j
		# 	end
		# 	j -= 1
		# end
		# if two.empty?
		# 	arr.delete_at(one[0])
		# 	arr.delete_at(one[1])
		# else
		# 	arr.delete_at(two[0])
		# end

		# l = arr.rindex{|a| a % 3 == 2}
		# if l
		# 	arr.delete_at(l)
		# else
		# 	l = arr.rindex{|a| a % 3 == 1}
		# 	arr.delete_at(l)
		# 	l = arr.rindex{|a| a % 3 == 1}
		# 	arr.delete_at(l)
		# end
	end
	ans = arr.join("")
	# if ans.to_i == 0
	# 	puts "0"
	# else
		puts ans
	# end
else
	puts "-1"
end


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

if n == 2
	puts "-1"
	exit 0
end

mn = x.min
k = x.index(mn)
lmx = x[0..k-1].max
rmx = x[k+1..-1].max
if 0 < k && k + 1 < n && lmx > mn && rmx > mn
	if lmx != rmx
		puts "#{x[0..k-1].index(lmx)+1} #{x[k+1..-1].index(rmx)+k+2}"
	else
		1.upto(k-1) do |i|
			if x[i] != mn
				puts "#{i+1} #{k+1}"
				exit 0
			end
		end
		(k+1).upto(n-2) do |i|
			if x[i] != mn
				puts "#{k+1} #{i+1}"
				exit 0
			end
		end
		if n == 3
			puts -1
			exit 0
		end
		puts "1 2"
	end
	exit 0
end

mx = x.max
k = x.index(mx)
lmn = x[0..k-1].min
rmn = x[k+1..-1].min
if 0 < k && k + 1 < n && lmn < mx && rmn < mx
	if lmn != rmn
		puts "#{x[0..k-1].index(lmn)+1} #{x[k+1..-1].index(rmn)+k+2}"
	else
		1.upto(k-1) do |i|
			if x[i] != mx
				puts "#{i+1} #{k+1}"
				exit 0
			end
		end
		(k+1).upto(n-2) do |i|
			if x[i] != mx
				puts "#{k+1} #{i+1}"
				exit 0
			end
		end
		if n == 3
			puts -1
			exit 0
		end
		puts "1 2"
	end
	exit 0
end

if mn == mx
	puts -1
	exit 0
end

x[1..-2].each_with_index do |v, i|
	if v != x[0]
		puts "1 #{i+2}"
		exit 0
	end
	if v != x[-1]
		puts "#{i+2} #{n}"
		exit 0
	end
end

puts -1

t = gets.to_i

a = gets.split.collect{|x| x.to_i}

up = a.sort
dn = a.sort{|x,y| y <=> x}

i = 0
finish = false

1.upto(a.size-1) do |i|
	if a[i-1] != a[i]
		a[i], a[i-1] = a[i-1], a[i]

		finish = a != up && a != dn

		if finish
			print i,' ', i+1
			break
		end

		a[i], a[i-1] = a[i-1], a[i]
	end
end

if not finish or a == Array.new(a.size, a[0])
	print -1
end

n = gets.to_i
a = gets.split(' ').map(&:to_i)
if n < 3
  puts "-1"
  exit
end

incr = a.sort
decr = a.sort{|x,y| y <=> x}

i = 0
finish = false

1.upto(a.size-1) do |i|
  if a[i] != a[i-1]
    a[i],a[i-1] = a[i-1],a[i]

    finish = (a != incr) && (a != decr)
    if finish
      puts "#{i} #{i+1}"
      break
    end
    a[i],a[i-1] = a[i-1],a[i]
  end
end

if !finish
  puts "-1"
end

n = gets.to_i
a = gets.split(' ').map(&:to_i)
if n < 3
  puts "-1"
  exit
end

incr = a.sort
decr = a.sort{|x,y| y <=> x}

i = 0
finish = false

1.upto(a.size-1) do |i|
  if a[i] != a[i-1]
    a[i],a[i-1] = a[i-1],a[i]

    finish = (a != incr) && (a != decr)
    if finish
      puts "#{i} #{i+1}"
      break
    end
    a[i],a[i-1] = a[i-1],a[i]
  end
end

if !finish
  puts "-1 "
end

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

if n == 2
	puts "-1"
	exit 0
end

mn = x.min
k = x.index(mn)
lmx = x[0..k-1].max
rmx = x[k+1..-1].max
if 0 < k && k + 1 < n && lmx > mn && rmx > mn
	puts "#{x[0..k-1].index(lmx)+1} #{x[k+1..-1].index(rmx)+1}"
	exit 0
end

mx = x.max
k = x.index(mx)
lmn = x[0..k-1].min
rmn = x[k+1..-1].min
if 0 < k && k + 1 < n && lmn < mx && rmn < mx
	puts "#{x[0..k-1].index(lmn)+1} #{x[k+1..-1].index(rmn)+1}"
	exit 0
end

if mn == mx
	puts -1
	exit 0
end

x[1..-2].each_with_index do |v, i|
	if v != x[0]
		puts "1 #{i+2}"
		exit 0
	end
	if v != x[-1]
		puts "#{i+2} #{n}"
		exit 0
	end
end

puts -1

def gao(x, y)
	return [-1] if x.size == 2
	a, b = [], []
	x.each_index { |i| x[i] == y[i] ? a << x[i] : b << x[i] }
	a.uniq!
	ub = b.uniq
	if a.size >= 2
		[x.index(a[0]) + 1, x.index(a[1]) + 1]
	else
		if ub.size >= 2 && b.size >= 3
			[x.index(ub[0]) + 1, x.index(ub[1]) + 1]
		else
			[-1]
		end
	end
end

n = gets.to_i
x = gets.split.map(&:to_i)
r = gao(x, x.sort)
puts r.size == 1 ? gao(x, x.sort.reverse) : r.join(" ")

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

if n == 2
	puts "-1"
	exit 0
end

mn = x.min
k = x.index(mn)
lmx = x[0..k-1].max
rmx = x[k+1..-1].max
if 0 < k && k + 1 < n && lmx > mn && rmx > mn
	if lmx != rmx
		puts "#{x[0..k-1].index(lmx)+1} #{x[k+1..-1].index(rmx)+k+2}"
	else
		1.upto(k-1) do |i|
			if x[i] != mn
				puts "#{i+1} #{k+1}"
				exit 0
			end
		end
		(k+1).upto(n-1) do |i|
			if x[i] != mn
				puts "#{k+1} #{i+1}"
				exit 0
			end
		end
		if n == 3
			puts -1
			exit 0
		end
		puts "1 2"
	end
	exit 0
end

mx = x.max
k = x.index(mx)
lmn = x[0..k-1].min
rmn = x[k+1..-1].min
if 0 < k && k + 1 < n && lmn < mx && rmn < mx
	if lmx != rmx
		puts "#{x[0..k-1].index(lmn)+1} #{x[k+1..-1].index(rmn)+k+2}"
	else
		1.upto(k-1) do |i|
			if x[i] != mx
				puts "#{i+1} #{k+1}"
				exit 0
			end
		end
		(k+1).upto(n-1) do |i|
			if x[i] != mx
				puts "#{k+1} #{i+1}"
				exit 0
			end
		end
		if n == 3
			puts -1
			exit 0
		end
		puts "1 2"
	end
	exit 0
end

if mn == mx
	puts -1
	exit 0
end

x[1..-2].each_with_index do |v, i|
	if v != x[0]
		puts "1 #{i+2}"
		exit 0
	end
	if v != x[-1]
		puts "#{i+2} #{n}"
		exit 0
	end
end

puts -1


# first steps

n, n1, n2 = gets.split(' ')
a = gets.split(' ')

n = n.to_i
i = 0
while i < n
	a[i] = a[i].to_i
	i += 1
end

n1 = n1.to_i
n2 = n2.to_i

a = a.sort {|x, y| x.to_i <=> y.to_i}
a = a.reverse

if n1 > n2
	temp = n2
	n2 = n1
	n1 = temp
end

cnt1, cnt2 = [0, 0]

i = 0
n1.times do
	cnt1 += a[i].to_i
	i += 1
end
i = n1
n2.times do
	cnt2 += a[i].to_i
	i += 1
end
#puts a.join(', ')
#puts cnt1.to_s + ' ' + cnt2.to_s
puts cnt1.to_f / n1.to_f + cnt2.to_f / n2.to_f

n, n1, n2 = gets.split.map(&:to_i)
data = gets.split.map(&:to_i)

data.sort! { |x, y| y <=> x }
n1, n2 = [n1,n2].minmax

ans = data[0,n1].reduce(0,:+).to_f / n1.to_f + data[n1,n2].reduce(0,:+).to_f / n2.to_f
p ans


MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
n,k=gsmi
s=gs
c=gets.chomp.split

i=0
cnt=0
while i<n do
  if c.include?(s[i])
    cnt+=1
  else
    h[cnt]+=1
    cnt=0
  end
  i+=1
end
h[cnt]+=1

# substringなん文字が何個あるか
h.each do |key,val|
  cnt = key*(key+1)/2*val
  sum+=cnt
end
puts sum

gets
s = gets.chomp
c = gets.split
p s.chars.chunk { c.include? _1 }.select { _1[0] }.sum {
		_2.size * (_2.size + 1) / 2
  }


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

class Player
  attr_reader :time, :team, :b_num, :card

  def initialize(l)
    @time  = l[0].to_i
    @team  = l[1]
    @b_num = l[2].to_i
    @card  = l[3]
  end

  def update(p)
    if @card != 'r'
      @time = p.time
      @card = 'r'
    end
  end

end

class Players
  def initialize
    @faul_receivers = []
  end

  def <<(p)
    if @faul_receivers.length < 1
      @faul_receivers << p
    else
      update = false
      @faul_receivers.each do |r|
        update= true if r.team == p.team and r.b_num == p.b_num
        if update
          r.update(p)
          break
        end
      end
      @faul_receivers << p unless update
    end
  end

  def red_card_list
    reds = []
    @faul_receivers.each do |p|
      reds << p if p.card == 'r'
    end

    reds.sort_by! { |p| p.time }
    return reds
  end

  def each(&block)
    @faul_receivers.each &block
  end
end

h, a, n = gets.chomp, gets.chomp, gets.chomp.to_i

p_l = Players.new
n.times do
  l = gets.chomp.split(' ')
  p = Player.new(l)
  p_l << p
end

p_l.red_card_list.each do |p|
  t = p.team == "a" ? a : h
  puts "#{t} #{p.b_num} #{p.time}"
end

team_a = STDIN.readline.strip
team_b = STDIN.readline.strip
n = STDIN.readline.to_i
yellow, red = {}, {}
n.times do
  tokens = STDIN.readline.split
  time, player, color = tokens[0].to_i, tokens[2].to_i, tokens[3]
  team = tokens[1] == 'h' ? team_a : team_b
  key = [player, team]
  first_red = false
  if color == 'y'
    if yellow.include?(key) and not red.include?(key)
      red[key] = first_red = true
    end
    yellow[key] = true
  else
    if not red.include?(key)
      red[key] = first_red = true
    end
  end
  if first_red
    puts '%s %d %d' % [team, player, time]
  end
end

require 'pp'

teama = STDIN.gets.chomp
teamb = STDIN.gets.chomp

h ={teama => {}, teamb => {}}

n = STDIN.gets.chomp.to_i

n.times do
  t, ha, num, yr = STDIN.gets.chomp.split(/ /)
  team = ha == 'h' ? teama : teamb

  if h[team].has_key?(num) && h[team][num] == 2
    # nothing
  elsif h[team].has_key?(num) && h[team][num] == 1
    print "#{team} #{num} #{t}\n"
      h[team][num] = 2
  elsif !h[team].has_key?(num)
    if yr == 'r'
      print "#{team} #{num} #{t}\n"
      h[team][num] = 2
    else
      h[team][num] = 1
    end
  end
end

name=[gets.chomp,gets.chomp]
foul=[{},{}]
done=[{},{}]
gets.to_i.times{
  time,team,n,type=gets.split
  team_n=team==?h?0:1
  if foul[team_n][n] || type==?r
    unless done[team_n][n]
      puts [name[team_n],n,time]*" "
      done[team_n][n]=true
    end
  else
    foul[team_n][n]=true
  end
}

home_team = gets.chomp
away_team = gets.chomp
N = gets.to_i
used = {}
red = {}
while $_ = gets do
    t_, team, m_, type = $_.split
    t = t_.to_i
    m = m_.to_i
    if red["#{team}:#{m}"]
        next
    end
    if type == "r" || used["#{team}:#{m}"] then
        if team == "h"
            print home_team
        else
            print away_team
        end
        print " #{m} #{t}\n"
        red["#{team}:#{m}"] = true
    end
    used["#{team}:#{m}"] = true
end

#!/usr/bin/ruby
home = STDIN.gets.chomp
away = STDIN.gets.chomp
n = STDIN.gets.chomp.to_i
marked = {'a' => [], 'h' => []}
teams = {'a' => away, 'h' => home}
n.times do
    tokens = STDIN.gets.chomp.split(' ')
    minute = tokens[0].to_i
    team = tokens[1]
    player = tokens[2].to_i
    card = tokens[3]
    marked[team][player] = 0 if not marked[team][player]
    next if marked[team][player] > 1
    if card == 'r' or marked[team][player] == 1
        marked[team][player] = 2
        puts "#{teams[team]} #{player} #{minute}"
    else
        marked[team][player] += 1
    end
end

a=Array.new(105,0)
b=Array.new(105,0)
sz=Array.new
sta=gets.to_s
stb=gets.to_s
n=gets.to_i
for i in 1..n
  stc=gets.to_s
  sz=stc.split
  if sz[3]=='y'
    tr=1
  elsif sz[3]=="r"
    tr=2
  end
  if sz[1]=="h"
    t=sz[2].to_i
    if a[t]>=2
      next
    end
    a[t]+=tr
    if a[t]>=2
      print sta.chomp
      print " "
      print t
      print " "
      puts sz[0]
    end
  elsif sz[1]=="a"
    t=sz[2].to_i
    if b[t]>=2
      next
    end
    b[t]+=tr
    if b[t]>=2
      print stb.chomp
      print " "
      print t
      print " "
      puts sz[0]
    end
  end
end

# 493A

class Foul
  include Comparable
  attr :time

  def initialize(player, time, team)
    @player = player
    @time = time.to_i
    @team = team
  end

  def <=>(another)
    time <=> another.time
  end

  def to_s
    [@team, @player, @time].join(' ')
  end
end

names = {h: gets.chomp, a: gets.chomp}
cards = {h: {y: {}, r: {}}, a: {y: {}, r: {}}}
gets.to_i.times do
  time, _team, player, card_colour = gets.chomp.split
  team = _team.to_sym
  unless cards[team][:r].include?(player)
    if card_colour == 'r'
      cards[team][:r][player] = time
    else
      if cards[team][:y].include?(player)
        cards[team][:r][player] = time
      else
        cards[team][:y][player] = time
      end
    end
  end
end

fouls = []
[:h, :a].each { |c| cards[c][:r].each { |k, v| fouls << Foul.new(k, v, names[c]) } }
fouls.sort.each { |foul| puts foul }

home = gets.chomp
away = gets.chomp
a = {}
arry = []
n = Integer(gets)
for j in 1..n
  arry[j] = gets.split(" ")
  arry[j].each { |arr| arr.chomp }
end
for i in 1..n
  if (((a[arry[i][1]]==nil || a[arry[i][1]][arry[i][2]]!= 'r') && arry[i][3] == 'r') || ( a[arry[i][1]]  &&  a[arry[i][1]][arry[i][2]] == 'y' && arry[i][3] == 'y'))
    if arry[i][1] == 'a'
      puts "#{away} #{arry[i][2]} #{arry[i][0]}"
    else
      puts "#{home} #{arry[i][2]} #{arry[i][0]}"
    end

    if a[arry[i][1]] ==nil
      a.merge!( arry[i][1] => {} )
    end

    if a[arry[i][1]][arry[i][2]] == 'y'
      a[arry[i][1]][arry[i][2]] = 'r'
    end

    if a[arry[i][1]][arry[i][2]] == nil
      a[arry[i][1]].merge!(  arry[i][2] => arry[i][3] )
    end

  end

  if a[arry[i][1]] ==nil
    a.merge!( arry[i][1] => {} )
  end
  if a[arry[i][1]][arry[i][2]] == nil
    a[arry[i][1]].merge!(  arry[i][2] => arry[i][3] )
  end
end

h = gets.chomp
a = gets.chomp
n = gets.to_i
hash = {}
processed = {}
result = []
n.times do
  m,team,player,card = gets.chomp.split
  if card == 'r'
    if !processed[[team,player]]
      result << [team,player,m.to_i]
      processed[[team,player]] = true
    end
  else
    if hash[[team,player]]
      if !processed[[team,player]]
        result << [team,player,m.to_i]
        processed[[team,player]] = true
      end
    else
      hash[[team,player]] = true
    end
  end
end
result = result.sort_by {|x| x[2]}
result.each do |team,player,minute|
  puts "#{team == 'h' ? h : a} #{player} #{minute}"
end

h = gets.chomp
a = gets.chomp
n = gets.to_i
hash = {}
result = []
n.times do
  m,team,player,card = gets.chomp.split
  if card == 'r'
    result << [team,player,m.to_i]
  else
    if hash[[team,player]]
      result << [team,player,m.to_i]
    else
      hash[[team,player]] = true
    end
  end
end
result = result.sort_by {|x| x[2]}
result.each do |team,player,minute|
  puts "#{team == 'h' ? h : a} #{player} #{minute}"
end

h = gets.chomp
a = gets.chomp
n = gets.to_i
hash = {}
result = []
n.times do
  m,team,player,card = gets.chomp.split
  if card == 'r'
    result << [team,player,m.to_i]
    hash.delete([team,player]) if hash[[team,player]]
  else
    if hash[[team,player]]
      result << [team,player,m.to_i]
      hash.delete([team,player])
    else
      hash[[team,player]] = true
    end
  end
end
result = result.sort_by {|x| x[2]}
result.each do |team,player,minute|
  puts "#{team == 'h' ? h : a} #{player} #{minute}"
end

h = gets.chomp
a = gets.chomp
n = gets.to_i
hash = {}
result = []
n.times do
  m,team,player,card = gets.chomp.split
  if card == 'r'
    result << [team,player,m]
  else
    if hash[[team,player]]
      result << [team,player,m]
    else
      hash[[team,player]] = true
    end
  end
end
result = result.sort_by {|x| x[2]}
result.each do |team,player,minute|
  puts "#{team == 'h' ? h : a} #{player} #{minute}"
end

h = gets.chomp
a = gets.chomp
n = gets.to_i
hash = {}
result = []
n.times do
  m,team,player,card = gets.chomp.split
  if card == 'r'
    result << [team,player,m.to_i]
  else
    if hash[[team,player]]
      result << [team,player,[m.to_i,hash[[team,player]]].max]
    else
      hash[[team,player]] = m.to_i
    end
  end
end
result = result.sort_by {|x| x[2]}
result.each do |team,player,minute|
  puts "#{team == 'h' ? h : a} #{player} #{minute}"
end

team_a = STDIN.readline.strip
team_b = STDIN.readline.strip
n = STDIN.readline.to_i
foul = {}
n.times do
  tokens = STDIN.readline.split
  time, player, color = tokens[0].to_i, tokens[2].to_i, tokens[3]
  team = tokens[1] == 'h' ? team_a : team_b
  key = [player, team]
  if color == 'y'
    if foul[key] == 1
      puts '%s %d %d' % [team, player, time]
      foul[key] += 1
    else
      foul[key] = 1
    end
  else
    if not foul.include? key
      puts '%s %d %d' % [team, player, time]
      foul[key] = 2
    else
      if foul[key] == 1
        puts '%s %d %d' % [team, player, time]
      end
      foul[key] += 1
    end
  end
end

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

class Player
  attr_reader :time, :team, :b_num, :card

  def initialize(l)
    @time  = l[0].to_i
    @team  = l[1]
    @b_num = l[2].to_i
    @card  = l[3]
  end

  def update(p)
    @time = p.time
    @card = 'r'
  end

end

class Players
  def initialize
    @faul_receivers = []
  end

  def <<(p)
    if @faul_receivers.length < 1
      @faul_receivers << p
    else
      update = false
      @faul_receivers.each do |r|
        update= true if r.team == p.team and r.b_num == p.b_num
        if update
          r.update(p)
          break
        end
      end
      @faul_receivers << p unless update
    end
  end

  def red_card_list
    reds = []
    @faul_receivers.each do |p|
      reds << p if p.card == 'r'
    end
    reds.sort_by! { |p| p.time }
    return reds
  end

  def each(&block)
    @faul_receivers.each &block
  end
end

h, a, n = gets.chomp, gets.chomp, gets.chomp.to_i

p_l = Players.new
n.times do
  l = gets.chomp.split(' ')
  p = Player.new(l)
  p_l << p
end

p_l.each do |p|
  if p.card == "r"
    t = p.team == "a" ? a : h
    puts "#{t} #{p.b_num} #{p.time}"
  end
end

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

class Player
  attr_reader :time, :team, :b_num, :card

  def initialize(l)
    @time  = l[0].to_i
    @team  = l[1]
    @b_num = l[2].to_i
    @card  = l[3]
  end

  def update(p)
    @time = p.time
    @card = 'r'
  end

end

class Players
  def initialize
    @faul_receivers = []
  end

  def <<(p)
    if @faul_receivers.length < 1
      @faul_receivers << p
    else
      update = false
      @faul_receivers.each do |r|
        update= true if r.team == p.team and r.b_num == p.b_num
        if update
          r.update(p)
          break
        end
      end
      @faul_receivers << p unless update
    end
  end

  def red_card_list
    reds = []
    @faul_receivers.each do |p|
      reds << p if p.card == 'r'
    end

    reds.sort_by! { |p| p.time }
    return reds
  end

  def each(&block)
    @faul_receivers.each &block
  end
end

h, a, n = gets.chomp, gets.chomp, gets.chomp.to_i

p_l = Players.new
n.times do
  l = gets.chomp.split(' ')
  p = Player.new(l)
  p_l << p
end

p_l.red_card_list.each do |p|
  t = p.team == "a" ? a : h
  puts "#{t} #{p.b_num} #{p.time}"
end

home_team = gets.chomp
away_team = gets.chomp
N = gets.to_i
used = {}
while $_ = gets do
    t_, team, m_, type = $_.split
    t = t_.to_i
    m = m_.to_i
    if type == "r" || used["#{team}:#{m}"] then
        if team == "h"
            print home_team
        else
            print away_team
        end
        print " #{m} #{t}\n"
    end
    used["#{team}:#{m}"] = true
end

a=Array.new(105,0)
b=Array.new(105,0)
sz=Array.new
sta=gets.to_s
stb=gets.to_s
n=gets.to_i
for i in 1..n
  stc=gets.to_s
  sz=stc.split
  if sz[3]=='y'
    tr=1
  elsif sz[3]=="r"
    tr=2
  end
  if sz[1]=="h"
    t=sz[2].to_i
    a[t]+=tr
    if a[t]>=2
      print sta.chomp
      print " "
      print t
      print " "
      puts sz[0]
    end
  elsif sz[1]=="a"
    t=sz[2].to_i
    b[t]+=tr
    if b[t]>=2
      print stb.chomp
      print " "
      print t
      print " "
      puts sz[0]
    end
  end
end

# 493A

class Foul
  include Comparable
  attr :time

  def initialize(player, time, team)
    @player = player
    @time = time
    @team = team
  end

  def <=>(another)
    time <=> another.time
  end

  def to_s
    [@team, @player, @time].join(' ')
  end
end

names = {h: gets.chomp, a: gets.chomp}
cards = {h: {y: {}, r: {}}, a: {y: {}, r: {}}}
gets.to_i.times do
  time, _team, player, card_colour = gets.chomp.split
  team = _team.to_sym
  unless cards[team][:r].include?(player)
    if card_colour == 'r'
      cards[team][:r][player] = time
    else
      if cards[team][:y].include?(player)
        cards[team][:r][player] = time
      else
        cards[team][:y][player] = time
      end
    end
  end
end

fouls = []
[:h, :a].each { |c| cards[c][:r].each { |k, v| fouls << Foul.new(k, v, names[c]) } }
fouls.sort.each { |foul| puts foul }

home = gets.chomp
away = gets.chomp
a = {}
arry = []
n = Integer(gets)
for j in 1..n
  arry[j] = gets.split(" ")
  arry[j].each { |arr| arr.chomp }
end
for i in 1..n
  if (((a[arry[i][1]]==nil || a[arry[i][1]][arry[i][2]]!= 'r') && arry[i][3] == 'r') || ( a[arry[i][1]]  &&  a[arry[i][1]][arry[i][2]] == 'y' && arry[i][3] == 'y'))
    if arry[i][1] == 'a'
      puts "#{away} #{arry[i][2]} #{arry[i][0]}"
    else
      puts "#{home} #{arry[i][2]} #{arry[i][0]}"
    end
    if a[arry[i][1]] ==nil
      a.merge!( arry[i][1] => {} )
    end
    if a[arry[i][1]][arry[i][2]] == nil || a[arry[i][1]][arry[i][2]] == 'y'
      a[arry[i][1]][arry[i][2]] = 'r'
    end
  end

  if a[arry[i][1]] ==nil
    a.merge!( arry[i][1] => {} )
  end
  a[arry[i][1]].merge!(  arry[i][2] => arry[i][3] )
end

home = gets.chomp
away = gets.chomp
a = {}
arry = []
n = Integer(gets)
for j in 1..n
  arry[j] = gets.split(" ")
  arry[j].each { |arr| arr.chomp }
end
for i in 1..n
  if (((a[arry[i][1]]==nil || a[arry[i][1]][arry[i][2]]==nil) && arry[i][3] == 'r') || ( a[arry[i][1]]  &&  a[arry[i][1]][arry[i][2]] == 'y' && arry[i][3] == 'y'))
    if arry[i][1] == 'a'
      puts "#{away} #{arry[i][2]} #{arry[i][0]}"
    else
      puts "#{home} #{arry[i][2]} #{arry[i][0]}"
    end
  end

  if a[arry[i][1]] ==nil
    a.merge!( arry[i][1] => {} )
  end
  a[arry[i][1]].merge!(  arry[i][2] => arry[i][3] )
end

home = gets.chomp
away = gets.chomp
a = {}
arry = []
n = Integer(gets)
for j in 1..n
  arry[j] = gets.split(" ")
  arry[j].each { |arr| arr.chomp }
end
for i in 1..n
  if ((a[arry[i][1]]==nil && arry[i][3] == 'r') || ( a[arry[i][1]]  &&  a[arry[i][1]][arry[i][2]] == 'y' && arry[i][3] == 'y'))
    if arry[i][1] == 'a'
      puts "#{away} #{arry[i][2]} #{arry[i][0]}"
    else
      puts "#{home} #{arry[i][2]} #{arry[i][0]}"
    end
  end

  if a[arry[i][1]] ==nil
    a.merge!( arry[i][1] => {} )
  end
  a[arry[i][1]].merge!(  arry[i][2] => arry[i][3] )
end

home = gets.chomp
away = gets.chomp
a = {}
arry = []
n = Integer(gets)
for j in 1..n
  arry[j] = gets.split(" ")
  arry[j].each { |arr| arr.chomp }
end
for i in 1..n
  if (a[arry[i][1]] && a[arry[i][1]][arry[i][2]] == 'y' && arry[i][3] == 'y') || arry[i][3] == 'r'
    if arry[i][1] == 'a'
      puts "#{away} #{arry[i][2]} #{arry[i][0]}"
    else
      puts "#{home} #{arry[i][2]} #{arry[i][0]}"
    end
  end

  if a[arry[i][1]] ==nil
    a.merge!( arry[i][1] => {} )
  end
  a[arry[i][1]].merge!(  arry[i][2] => arry[i][3] )
end

home = gets.chomp
away = gets.chomp
a = {}
arry = []
n = Integer(gets)
for j in 1..n
  arry[j] = gets.split(" ")
  arry[j].each { |arr| arr.chomp }
end
for i in 1..n
  if (((a[arry[i][1]]==nil || a[arry[i][1]][arry[i][2]]!= 'r') && arry[i][3] == 'r') || ( a[arry[i][1]]  &&  a[arry[i][1]][arry[i][2]] == 'y' && arry[i][3] == 'y'))
    if arry[i][1] == 'a'
      puts "#{away} #{arry[i][2]} #{arry[i][0]}"
    else
      puts "#{home} #{arry[i][2]} #{arry[i][0]}"
    end

    if a[arry[i][1]] ==nil
      a.merge!( arry[i][1] => {} )
    end

    if a[arry[i][1]][arry[i][2]] == 'y'
      a[arry[i][1]][arry[i][2]] = 'r'
    end

    if a[arry[i][1]][arry[i][2]] == nil
      a[arry[i][1]].merge!(  arry[i][2] => arry[i][3] )
    end

  end

  if a[arry[i][1]] ==nil
    a.merge!( arry[i][1] => {} )
  end
  a[arry[i][1]].merge!(  arry[i][2] => arry[i][3] )
end

home = gets.chomp
away = gets.chomp
a = {}
arry = []
n = Integer(gets)
for j in 1..n
  arry[j] = gets.split(" ")
  arry[j].each { |arr| arr.chomp }
end
for i in 1..n
  if (((a[arry[i][1]]==nil || a[arry[i][1]][arry[i][2]]!= 'r') && arry[i][3] == 'r') || ( a[arry[i][1]]  &&  a[arry[i][1]][arry[i][2]] == 'y' && arry[i][3] == 'y'))
    if arry[i][1] == 'a'
      puts "#{away} #{arry[i][2]} #{arry[i][0]}"
    else
      puts "#{home} #{arry[i][2]} #{arry[i][0]}"
    end
    if a[arry[i][1]] ==nil
      a.merge!( arry[i][1] => {} )
    end
    a[arry[i][1]].merge!(  arry[i][2] => 'r' )
  end

  if a[arry[i][1]] ==nil
    a.merge!( arry[i][1] => {} )
  end
  a[arry[i][1]].merge!(  arry[i][2] => arry[i][3] )
end

home = gets.chomp
away = gets.chomp
a = {}
arry = []
n = Integer(gets)
for j in 1..n
  arry[j] = gets.split(" ")
  arry[j].each { |arr| arr.chomp }
end
for i in 1..n
  if (((a[arry[i][1]]==nil || a[arry[i][1]][arry[i][2]]!= 'r') && arry[i][3] == 'r') || ( a[arry[i][1]]  &&  a[arry[i][1]][arry[i][2]] == 'y' && arry[i][3] == 'y'))
    if arry[i][1] == 'a'
      puts "#{away} #{arry[i][2]} #{arry[i][0]}"
    else
      puts "#{home} #{arry[i][2]} #{arry[i][0]}"
    end
  end

  if a[arry[i][1]] ==nil
    a.merge!( arry[i][1] => {} )
  end
  a[arry[i][1]].merge!(  arry[i][2] => arry[i][3] )
end


n, k = gets.split.map(&:to_i)
xs = gets.split.map(&:to_i)
def get_result k, xs
  diffs=(0..xs.size-2).map do |i| xs[i+1]-xs[i]; end
  if diffs.any?{|d| d>k}
    return -1
  end

  diffs.inject([0]) do |memo,d|
    last = memo.pop
    if(last+d<=k)
      memo<<last+d
    else
      memo<<last
      memo<<d
    end
    memo
  end.length
end
puts get_result k, xs


def powmod(a, k, m)
  return 1 if k == 0
  t = powmod(a, k / 2, m)
  t = t * t % m
  t = t * a % m if k % 2 == 1
  return t
end

m = gets.chomp.to_i
ps = gets.chomp.split.map(&:to_i)
mod = 10**9 + 7
n = 1

hs = Hash.new(1)
ps.each do |p|
  hs[p] += 1
  n = n * p % mod
end

cnt = 1
hs.each do |_, v|
  cnt = cnt * v % (2 * (mod - 1))
end

r = 1
if cnt % 2 == 1
  hs.each do |k, v|
    r = r * powmod(k, (v - 1) / 2, mod) % mod
  end
end

puts r * powmod(n, cnt / 2, mod) % mod

MOD = 1000000000 + 7

def f(n, p)
  if p == 0 then
    return 1
  end
  m = f(n, p / 2)
  if p % 2 == 1 then
    return m * m * n % MOD
  else
    return m * m % MOD
  end
end

while s = $stdin.gets() do
  s = $stdin.gets()
  ps = s.split(" ")
  cnt = {}
  ps.each do |p|
    key = p.to_i()
    if cnt[key] then
      cnt[key] = cnt[key] + 1
    else
      cnt[key] = 1
    end
  end

  d = 1
  cnt.each do |key, val|
    d *= val + 1
  end

  ret = 1
  cnt.each do |key, val|
    x = f(key, (val * (val + 1) / 2) % (MOD - 1))
    y = f(x, (d / (val + 1)) % (MOD - 1))
    ret = ret * y % MOD
  end
  puts(ret)
end

def powmod(a, k, m)
  return 1 if k == 0
  t = powmod(a, k / 2, m)
  t = t * t % m
  t = t * a % m if k % 2 == 1
  return t
end

m = gets.chomp.to_i
ps = gets.chomp.split.map(&:to_i)
mod = 10**9 + 7
n = 1

hs = Hash.new(1)
ps.each do |p|
  hs[p] += 1
  n = n * p % mod
end

cnt = 1
hs.each do |_, v|
  cnt *= v
end

puts powmod(n, cnt / 2, mod)


def check(y,x)
  return 0<=y && y<$n && 0<=x && x<$m && $room[y][x,1]==$c
end

$n,$m,$c=STDIN.gets.chomp.split
$n=$n.to_i
$m=$m.to_i
$room=[]
$n.times {$room<<STDIN.gets.chomp}
desks={}
$n.times {|i|
  $m.times {|j|
    if $room[i][j,1]==$c || $room[i][j,1]=="." then next end
    if check(i,j-1) || check(i,j+1) || check(i-1,j) || check(i+1,j) then
      desks[$room[i][j,1]]=1
    end
  }
}
print(desks.length,"\n")

tmp=gets.chomp.split
n=tmp[0].to_i
m=tmp[1].to_i
c=tmp[2]
memo=Array.new(n)
n.times do |j|
  memo[j]= gets.chomp!.each_char.map{|i| i}
end
ti=0
tj=0
h={}
n.times do |i|
  m.times do |j|
    if memo[i][j]==c
      ti=i
      tj=j
      if ti!=0
        h[memo[ti-1][tj]]=true
      end
      if ti!=n-1
        h[memo[ti+1][tj]]=true
      end
      if tj!=0
        h[memo[ti][tj-1]]=true
      end
      if tj!=m-1
        h[memo[ti][tj+1]]=true
      end
    end
  end
end
h.delete(c)
h.delete(".")
puts h.size

require 'Set'
height,width,color = gets.split
width = width.to_i
height = height.to_i
#p width
#p height
#p color
dh = [-1, 0, 0, 1]
dw = [0, -1, 1, 0]
data = Array.new()
for h in 0...height do
  data[h] = gets
end
ans = Set.new()
ans.add?(color)
for h in 0...height do
  for w in 0...width do
    if data[h][w,1] == color then
      #print h, w
      #puts
      for d in 0...4 do
        nh = h + dh[d]
        nw = w + dw[d]
        if nh >= 0 && nw >= 0 && nh < height && nw < width && data[nh][nw,1] != "." then
          ans.add?(data[nh][nw,1])
        end
      end
    end
  end
end
p ans.size()-1

fst = gets.split
N=fst[0].to_i
M=fst[1].to_i
PresidentsColor=fst[2][0]
matrix = Array.new(N)
N.times do |i|
  line = gets.chomp
  matrix[i] = Array.new(M)
  M.times do |j|
    matrix[i][j] = line[j]
  end
end
deputies = []
matrix.each_index do |i|
  matrix[i].each_with_index do |c,j|
    if c==PresidentsColor
      deputies << matrix[i-1][j] if i!=0
      deputies << matrix[i+1][j] if i!=N-1
      deputies << matrix[i][j-1] if j!=0
      deputies << matrix[i][j+1] if j!=M-1
    end
  end
end
deputies.uniq!
deputies.delete "."[0]
deputies.delete PresidentsColor
puts deputies.size

require 'set'

n, m, p_desk = gets.split
n = n.to_i
m = m.to_i

grid = []
n.times do |index|
	row = gets.chomp.split('')
	grid << row
end

desks = Set.new
0.upto(n - 1) do |x|
	0.upto(m - 1) do |y|
		next if grid[x][y] == ?. or grid[x][y] == p_desk
		desks << grid[x][y] if (x > 0 and grid[x - 1][y] == p_desk) || (y > 0 and grid[x][y - 1] == p_desk) ||
							             (x < n - 1 and grid[x + 1][y] == p_desk) || (y < m - 1 and grid[x][y + 1] == p_desk)
  end
end

puts desks.size

n,m,c = gets.split(/\s+/)
n = n.to_i
m = m.to_i
c = c[0]
a = Array.new(n)
n.times do |i|
  a[i] = gets.strip
end

h = []

0.upto(n-1) do |i|
  0.upto(m-1) do |j|
    next if a[i][j] == ?. or a[i][j] == c
    h << a[i][j] if (i > 0 and a[i-1][j] == c) || (j > 0 and a[i][j-1] == c) ||
                    (i < n-1 and a[i+1][j] == c) || (j < m-1 and a[i][j+1] == c)
  end
end

puts h.uniq.size

def extractIndices(office, color)
  startingCol, endingCol, startingRow, endingRow = -1, -1, -1, -1
  office.each_with_index do |row, rIdx|
    if row.include? color
      if startingRow == -1 #we do this just for the first row
        startingCol = row.find_index(color)
        (startingCol..row.length-1).each do |idx|
          if row[idx] != color
            endingCol = idx-1
            break
          end
        end
        if endingCol == -1
          endingCol = row.length-1
        end
      end
      startingRow = rIdx if startingRow == -1
      endingRow = rIdx if rIdx >= endingRow
    end
  end
  return startingCol, endingCol, startingRow, endingRow
end

n, m, color = nil, nil, nil
office = []
# File.open("input1", "r") do |file|
#   n, m, color = file.readline.split(" ")
#   n = n.to_i
#   m = m.to_i
#   n.times do
#     office << file.readline.chomp.split("")
#   end
# end

n, m, color = gets.chomp.split(" ")
n = n.to_i
m = m.to_i
n.times do
  office << gets.chomp.split("")
end

startingCol, endingCol, startingRow, endingRow = extractIndices(office, color)
topRow = startingRow - 1
bottomRow = endingRow + 1
leftCol = startingCol - 1
rightCol = endingCol + 1
found = []

if topRow >= 0
  topRow = office[topRow][startingCol..endingCol]
  lastFound = "."
  (startingCol..endingCol).each do |idx|
    next if topRow[idx-startingCol] == "." or topRow[idx-startingCol] == lastFound
    lastFound = topRow[idx-startingCol]
    found << lastFound
  end
end

if bottomRow < n
  bottomRow = office[bottomRow][startingCol..endingCol]
  lastFound = "."
  (startingCol..endingCol).each do |idx|
    next if bottomRow[idx-startingCol] == "." or bottomRow[idx-startingCol] == lastFound
    lastFound = bottomRow[idx-startingCol]
    found << lastFound
  end
end

if leftCol >= 0
  leftCol = office[startingRow..endingRow].map{|r| r[leftCol]}
  lastFound = "."
  (startingRow..endingRow).each do |idx|
    next if leftCol[idx-startingRow] == "." or leftCol[idx-startingRow] == lastFound
    lastFound = leftCol[idx-startingRow]
    found << lastFound
  end
end

if rightCol < m
  rightCol = office[startingRow..endingRow].map{|r| r[rightCol]}
  lastFound = "."
  (startingRow..endingRow).each do |idx|
    next if rightCol[idx-startingRow] == "." or rightCol[idx-startingRow] == lastFound
    lastFound = rightCol[idx-startingRow]
    found << lastFound
  end
end

puts "#{found.count}"

n, m, c = gets.chomp.split
n = n.to_i
m = m.to_i
room = Array.new
n.times { room << gets.chomp }
ret = 0
x = Hash.new
dy = [0, -1, 0, 1]
dx = [-1, 0, 1, 0]
x[c] = true
x["."] = true
(0...n).each do |i|
  (0...m).each do |j|
    if room[i][j] == c
      (0...(dy.length)).each do |k|
        ii, jj = i + dy[k], j + dx[k]
        next unless ii >= 0 && ii < n && jj >= 0 && jj < m
        unless x.has_key? room[ii][jj]
          x[room[ii][jj]] = true
          ret += 1
        end
      end
    end
  end
end

puts ret

s = gets.split

n = s[0].to_i
m = s[1].to_i

col = s[2]

dots = []
matrix = []
n.times do |x|
    matrix << gets.chomp
    k = -1
    while k = matrix[x].index(col, k+1) do
        dots << [x, k]
    end
end

t = {}

dots.size.times do |i|
    dot = dots[i]
    if dot[1]+1 < m then
        if matrix[dot[0]][dot[1]+1] != col then
            if t[matrix[dot[0]][dot[1]+1]] then
                t[matrix[dot[0]][dot[1]+1]] += 1
            else
                t[matrix[dot[0]][dot[1]+1]] = 1
            end
        end
    end
    if dot[1] > 0 then
        if matrix[dot[0]][dot[1]-1] != col then
            if t[matrix[dot[0]][dot[1]-1]] then
                t[matrix[dot[0]][dot[1]-1]] += 1
            else
                t[matrix[dot[0]][dot[1]-1]] = 1
            end
        end
    end
    if dot[0]+1 < n then
        if matrix[dot[0]+1][dot[1]] != col then
            if t[matrix[dot[0]+1][dot[1]]] then
                t[matrix[dot[0]+1][dot[1]]] += 1
            else
                t[matrix[dot[0]+1][dot[1]]] = 1
            end
        end
    end
    if dot[0] > 0 then
        if matrix[dot[0]-1][dot[1]] != col
            if t[matrix[dot[0]-1][dot[1]]] then
                t[matrix[dot[0]-1][dot[1]]] += 1
            else
                t[matrix[dot[0]-1][dot[1]]] = 1
            end
        end
    end
end

t.delete(".")

puts t.size

ns,ms,c = gets.chomp.split(/ /)
n = ns.to_i
m = ms.to_i
ar = Array.new
for i in 0..n-1
	ar[i] = gets.chomp.split(//)
end

s = Hash.new

for i in 0..n-1
	for j in 0..m-1
		if ar[i][j] == c
			s[ar[i-1][j]] = 1 if i > 0
			s[ar[i+1][j]] = 1 if i < n-1
			s[ar[i][j-1]] = 1 if j > 0
			s[ar[i][j+1]] = 1 if j < m-1
		end
	end
end

s.delete(c)
s.delete(".")
puts s.length

n, m, c = gets.chomp!.split
n = n.to_i
m = m.to_i
ary = []
hash = {}
n.times {ary << gets.chomp!}
n.times do |i|
	m.times do |j|
		if ary[i][j] == c
			hash[ary[i][j]] = 1
			hash[ary[i - 1][j]] = 1 if i - 1 >= 0
			hash[ary[i + 1][j]] = 1 if i + 1 < n
			hash[ary[i][j - 1]] = 1 if j - 1 >= 0
			hash[ary[i][j + 1]] = 1 if j + 1 < m
		end
	end
end
hash.delete c
hash.delete '.'
puts hash.size

n,m,r=gets.split()
r=r[0]
n=n.to_i
m=m.to_i

map=Array.new(n)
for i in 0..n-1
        map[i]=gets.split[0]
end

res=[]
dx=[1,0,-1,0]
for i in 0..n-1
        for j in 0..m-1
                next if map[i][j]!=r
                for k in 0..3
                        nx=i+dx[k]
                        ny=j+dx[k^1]
                        if nx>=0&&nx<n&&ny>=0&&ny<m then
                                res.push(map[nx][ny]) unless map[nx][ny]==r
                        end
                end
        end
end
ans=0

for i in 0..res.length-1
        ok=1
        for j in 0..i-1
                ok=0 if res[i]==res[j]
        end
        next if ok==0||res[i]=="."
        ans+=1
end

p ans

y,x,p = gets.split
y = y.to_i
x = x.to_i

arr = []

y.times do |i|
    line = gets.chomp
    line = line.split(//).join(' ');
    arr[i] = line.split
end
count = 0
exists = []

y.times do |i|
    x.times do |j|
        if arr[i][j] == p
            if  i != 0 && arr[i-1][j] != '.' && arr[i-1][j] != p
                if exists.index(arr[i-1][j]) == nil
                    count = count+1
                    exists << arr[i-1][j]
                end
            end
            if j != 0 && arr[i][j-1] != '.' && arr[i][j-1] != p
                if exists.index(arr[i][j-1]) == nil
                    count = count+1
                    exists << arr[i][j-1]
                end
            end
            if  i != y-1 && arr[i+1][j] != '.' && arr[i+1][j] != p
                if exists.index(arr[i+1][j]) == nil
                    count = count+1
                    exists << arr[i+1][j]
                end
            end
            if j != x-1 && arr[i][j+1] != '.' && arr[i][j+1] != p
                if exists.index(arr[i][j+1]) == nil
                    count = count+1
                    exists << arr[i][j+1]
                end
            end
        end
    end
end
puts count

s = gets.split

n = s[0].to_i
m = s[1].to_i

col = s[2]

i = []
j = []

matrix = []
n.times do |x|
    matrix << gets.chomp
    k = -1
    while k = matrix[x].index(col, k+1) do
        i << x
        j << k
    end
end

i = i.uniq
j = j.uniq

t = {}

i.size.times do |x|
    m.times do |y|
        if matrix[i[x]][y] != col then
            if t[matrix[i[x]][y]] then
                t[matrix[i[x]][y]] += 1
            else
                t[matrix[i[x]][y]] = 1
            end
        end
    end
end

j.size.times do |x|
    n.times do |y|
        if matrix[y][j[x]] != col then
            if t[(matrix[y][j[x]])] then
                t[(matrix[y][j[x]])] += 1
            else
                t[(matrix[y][j[x]])] = 1
            end
        end
    end
end

puts t.size-1

s = gets.split

n = s[0].to_i
m = s[1].to_i

col = s[2]

i = []
j = []

matrix = []
n.times do |x|
    matrix << gets.chomp
    k = -1
    while k = matrix[x].index(col, k+1) do
        i << x
        j << k
    end
end

i = i.uniq
j = j.uniq

t = {}

i.size.times do |x|
    m.times do |y|
        if matrix[i[x]][y] != col then
            if t[matrix[i[x]][y]] then
                t[matrix[i[x]][y]] += 1
            else
                t[matrix[i[x]][y]] = 1
            end
        end
    end
end

j.size.times do |x|
    n.times do |y|
        if matrix[y][j[x]] != col then
            if t[(matrix[y][j[x]])] then
                t[(matrix[y][j[x]])] += 1
            else
                t[(matrix[y][j[x]])] = 1
            end
        end
    end
end

t.delete(".")

puts t.size

s = gets.split

n = s[0].to_i
m = s[1].to_i

col = s[2]

dots = []
matrix = []
n.times do |x|
    matrix << gets.chomp
    k = -1
    while k = matrix[x].index(col, k+1) do
        dots << [x, k]
    end
end

t = {}

dots.size.times do |i|
    dot = dots[i]
    if matrix[dot[0]][dot[1]+1] then
        if matrix[dot[0]][dot[1]+1] != col then
            if t[matrix[dot[0]][dot[1]+1]] then
                t[matrix[dot[0]][dot[1]+1]] += 1
            else
                t[matrix[dot[0]][dot[1]+1]] = 1
            end
        end
    end
    if matrix[dot[0]][dot[1]-1] then
        if matrix[dot[0]][dot[1]-1] != col then
            if t[matrix[dot[0]][dot[1]-1]] then
                t[matrix[dot[0]][dot[1]-1]] += 1
            else
                t[matrix[dot[0]][dot[1]-1]] = 1
            end
        end
    end
    if matrix[dot[0]+1] then
        if matrix[dot[0]+1][dot[1]] != col then
            if t[matrix[dot[0]+1][dot[1]]] then
                t[matrix[dot[0]+1][dot[1]]] += 1
            else
                t[matrix[dot[0]+1][dot[1]]] = 1
            end
        end
    end
    if matrix[dot[0]-1] then
        if matrix[dot[0]-1][dot[1]] != col
            if t[matrix[dot[0]-1][dot[1]]] then
                t[matrix[dot[0]-1][dot[1]]] += 1
            else
                t[matrix[dot[0]-1][dot[1]]] = 1
            end
        end
    end
end

t.delete(".")

puts t.size

n, m, c = gets.chomp!.split
n = n.to_i
m = m.to_i
ary = []
hash = {}
n.times {ary << gets.chomp!}
n.times do |i|
	m.times do |j|
		if ary[i][j] == c
			hash[ary[i][j]] = 1
			hash[ary[i - 1][j]] = 1 if i - 1 >= 0
			hash[ary[i + 1][j]] = 1 if i + 1 < n
			hash[ary[i][j - 1]] = 1 if j - 1 >= 0
			hash[ary[i][j + 1]] = 1 if j + 1 < m
		end
	end
end
puts hash

n,m,r=gets.split()
r=r[0]
n=n.to_i
m=m.to_i

map=Array.new(n)
for i in 0..n-1
        map[i]=gets.split[0]
end

res=[]
dx=[1,0,-1,0]
for i in 0..n-1
        for j in 0..m-1
                next if map[i][j]!=r
                for k in 0..3
                        nx=i+dx[k]
                        ny=j+dx[k^1]
                        if nx>=0&&nx<n&&ny>=0&&ny<m then
                                res.push(map[nx][ny]) unless map[nx][ny]==r
                        end
                end
        end
end
ans=0

for i in 0..res.length-1
        ok=1
        for j in 0..i-1
                ok=0 if res[i]==res[j]
        end
        next if ok==0||res[i]=="."
        p res[i]
        ans+=1
end

p ans

tmp=gets.chomp.split
n=tmp[0].to_i
m=tmp[1].to_i
c=tmp[2]
memo=Array.new(n)
n.times do |j|
  memo[j]= gets.chomp!.each_char.map{|i| i}
end
ti=0
tj=0
h={}
n.times do |i|
  m.times do |j|
    if memo[i][j]==c
      ti=i
      tj=j
      if ti!=0 &&memo[ti-1][tj]!="."
        h[memo[ti-1][tj]]=true
      end
      if ti!=n-1&&memo[ti+1][tj]!="."
        h[memo[ti+1][tj]]=true
      end
      if tj!=0 && memo[ti][tj-1]!="."
        h[memo[ti][tj-1]]=true
      end
    end
  end
end
h.delete(c)
puts h.size

require 'Set'
height,width,color = gets.split
width = width.to_i
height = height.to_i
#p width
#p height
#p color
dh = [-1, 0, 0, 1]
dw = [0, -1, 1, 0]
data = Array.new()
for h in 0...height do
  data[h] = gets
end
ans = Set.new()
ans.add?(color)
for h in 0...height do
  for w in 0...width do
    if data[h][w,1] == color then
      #print h, w
      #puts
      for d in 0...4 do
        nh = h + dh[d]
        nw = w + dw[d]
        if nh >= 0 && nw >= 0 && nh < height && nw < width && data[nh][nw,1] != "." then
          ans.add?(data[h-1][w-1,1])
        end
      end
    end
  end
end
p ans.size()-1

def extractIndices(office, color)
  startingCol, endingCol, startingRow, endingRow = -1, -1, -1, -1
  office.each_with_index do |row, rIdx|
    if row.include? color
      if startingRow == -1 #we do this just for the first row
        startingCol = row.find_index(color)
        (startingCol..row.length-1).each do |idx|
          if row[idx] != color
            endingCol = idx-1
            break
          end
        end
        if endingCol == -1
          endingCol = row.length-1
        end
      end
      startingRow = rIdx if startingRow == -1
      endingRow = rIdx if rIdx >= endingRow
    end
  end
  return startingCol, endingCol, startingRow, endingRow
end

n, m, color = nil, nil, nil
office = []
# File.open("input1", "r") do |file|
#   n, m, color = file.readline.split(" ")
#   n = n.to_i
#   m = m.to_i
#   n.times do
#     office << file.readline.chomp.split("")
#   end
# end

n, m, color = gets.chomp.split(" ")
n = n.to_i
m = m.to_i
n.times do
  office << gets.chomp.split("")
end

startingCol, endingCol, startingRow, endingRow = extractIndices(office, color)
topRow = startingRow - 1
bottomRow = endingRow + 1
leftCol = startingCol - 1
rightCol = endingCol + 1
found = []

puts "#{topRow}, #{bottomRow}, #{leftCol}, #{rightCol}"

if topRow >= 0
  topRow = office[topRow][startingCol..endingCol]
  lastFound = "."
  (startingCol..endingCol).each do |idx|
    next if topRow[idx-startingCol] == "." or topRow[idx-startingCol] == lastFound
    lastFound = topRow[idx-startingCol]
    found << lastFound
  end
end

if bottomRow < n
  bottomRow = office[bottomRow][startingCol..endingCol]
  lastFound = "."
  (startingCol..endingCol).each do |idx|
    next if bottomRow[idx-startingCol] == "." or bottomRow[idx-startingCol] == lastFound
    lastFound = bottomRow[idx-startingCol]
    found << lastFound
  end
end

if leftCol >= 0
  leftCol = office[startingRow..endingRow].map{|r| r[leftCol]}
  lastFound = "."
  (startingRow..endingRow).each do |idx|
    next if leftCol[idx-startingRow] == "." or leftCol[idx-startingRow] == lastFound
    lastFound = leftCol[idx-startingRow]
    found << lastFound
  end
end

if rightCol < m
  rightCol = office[startingRow..endingRow].map{|r| r[rightCol]}
  lastFound = "."
  (startingRow..endingRow).each do |idx|
    next if rightCol[idx-startingRow] == "." or rightCol[idx-startingRow] == lastFound
    lastFound = rightCol[idx-startingRow]
    found << lastFound
  end
end

puts "#{found.count}"

def extractIndices(office, color)
  startingCol, endingCol, startingRow, endingRow = -1, -1, -1, -1
  office.each_with_index do |row, rIdx|
    if row.include? color
      if startingRow == -1 #we do this just for the first row
        startingCol = row.find_index(color)
        (startingCol..row.length-1).each do |idx|
          if row[idx] != color
            endingCol = idx-1
          end
        end
        if endingCol == -1
          endingCol = row.length-1
        end
      end
      startingRow = rIdx if startingRow == -1
      endingRow = rIdx if rIdx >= endingRow
    end
  end
  return startingCol, endingCol, startingRow, endingRow
end

n, m, color = nil, nil, nil
office = []
# File.open("input1", "r") do |file|
#   n, m, color = file.readline.split(" ")
#   n = n.to_i
#   m = m.to_i
#   n.times do
#     office << file.readline.chomp.split("")
#   end
# end

n, m, color = gets.chomp.split(" ")
n = n.to_i
m = m.to_i
n.times do
  office << gets.chomp.split("")
end

startingCol, endingCol, startingRow, endingRow = extractIndices(office, color)
topRow = startingRow - 1
bottomRow = endingRow + 1
leftCol = startingCol - 1
rightCol = endingCol + 1
found = []

if topRow >= 0
  topRow = office[topRow][startingCol..endingCol]
  lastFound = "."
  (startingCol..endingCol).each do |idx|
    next if topRow[idx-startingCol] == "." or topRow[idx-startingCol] == lastFound
    lastFound = topRow[idx-startingCol]
    found << lastFound
  end
end

if bottomRow < n
  bottomRow = office[bottomRow][startingCol..endingCol]
  lastFound = "."
  (startingCol..endingCol).each do |idx|
    next if bottomRow[idx-startingCol] == "." or bottomRow[idx-startingCol] == lastFound
    lastFound = bottomRow[idx-startingCol]
    found << lastFound
  end
end

if leftCol >= 0
  leftCol = office[startingRow..endingRow].map{|r| r[leftCol]}
  lastFound = "."
  (startingRow..endingRow).each do |idx|
    next if leftCol[idx-startingRow] == "." or leftCol[idx-startingRow] == lastFound
    lastFound = leftCol[idx-startingRow]
    found << lastFound
  end
end

if rightCol < m
  rightCol = office[startingRow..endingRow].map{|r| r[rightCol]}
  lastFound = "."
  (startingRow..endingRow).each do |idx|
    next if rightCol[idx-startingRow] == "." or rightCol[idx-startingRow] == lastFound
    lastFound = rightCol[idx-startingRow]
    found << lastFound
  end
end

puts "#{found.count}"

def extractIndices(office, color)
  startingCol, endingCol, startingRow, endingRow = -1, -1, -1, -1
  office.each_with_index do |row, rIdx|
    if row.include? color
      if startingRow == -1 #we do this just for the first row
        startingCol = row.find_index(color)
        (startingCol..row.length-1).each do |idx|
          if row[idx] != color
            endingCol = idx-1
          end
        end
        if endingCol == -1
          endingCol = row.length-1
        end
      end
      startingRow = rIdx if startingRow == -1
      endingRow = rIdx if rIdx >= endingRow
    end
  end
  return startingCol, endingCol, startingRow, endingRow
end

n, m, color = nil, nil, nil
office = []
# File.open("input2", "r") do |file|
#   n, m, color = file.readline.split(" ")
#   n = n.to_i
#   m = m.to_i
#   n.times do
#     office << file.readline.chomp.split("")
#   end
# end

n, m, color = gets.chomp.split(" ")
n = n.to_i
m = m.to_i
n.times do
  office << gets.chomp.split("")
end

startingCol, endingCol, startingRow, endingRow = extractIndices(office, color)
topRow = startingRow - 1
bottomRow = endingRow + 1
leftCol = startingCol - 1
rightCol = endingCol + 1
found = []

if topRow >= 0
  topRow = office[topRow][startingCol..endingCol]
  lastFound = "."
  (startingCol..endingCol).each do |idx|
    next if topRow[idx-startingCol] == "." or topRow[idx-startingCol] == lastFound
    lastFound = topRow[idx-startingCol]
    found << lastFound
  end
end

if bottomRow < n
  bottomRow = office[bottomRow][startingCol..endingCol]
  lastFound = "."
  (startingCol..endingCol).each do |idx|
    next if bottomRow[idx-startingCol] == "." or bottomRow[idx-startingCol] == lastFound
    lastFound = bottomRow[idx-startingCol]
    found << lastFound
  end
end

if leftCol >= 0
  leftCol = office[startingRow..endingRow].map{|r| r[leftCol]}
  lastFound = "."
  (startingRow..endingRow).each do |idx|
    next if leftCol[idx-startingRow] == "." or leftCol[startingRow] == lastFound
    lastFound = leftCol[idx]
    found << lastFound
  end
end

if rightCol < m
  rightCol = office[startingRow..endingRow].map{|r| r[rightCol]}
  lastFound = "."
  (startingRow..endingRow).each do |idx|
    next if rightCol[idx-startingRow] == "." or rightCol[startingRow] == lastFound
    lastFound = rightCol[idx]
    found << lastFound
  end
end

puts "#{found.length}"


gets.to_i.times do
  gets
  a = gets.split(" ").map(&:to_i)
  a.sort!

  a[5...a.size-5] = nil
  a.compact!

  best = -999999999999999999999999999999999999999999999999999999999

  (0...a.size).each do |i|
  ((i+1)...a.size).each do |j|
  ((j+1)...a.size).each do |k|
  ((k+1)...a.size).each do |l|
  ((l+1)...a.size).each do |t|
    x = a[i]*a[j]*a[k]*a[l]*a[t]
    best = x if x > best
  end
  end
  end
  end
  end
  p best
end

gets.to_i.times do
  gets
  a = gets.split(" ").map(&:to_i)
  a.sort!

  a[5...a.size-5] = nil
  a.compact!

  best = -9999999999999

  (0...a.size).each do |i|
  ((i+1)...a.size).each do |j|
  ((j+1)...a.size).each do |k|
  ((k+1)...a.size).each do |l|
  ((l+1)...a.size).each do |t|
    x = a[i]*a[j]*a[k]*a[l]*a[t]
    best = x if x > best
  end
  end
  end
  end
  end
  p best
end


la,lb = gets.split(" ").map(&:to_i)
k,m = gets.split(" ").map(&:to_i)

arr = gets.split(" ").map(&:to_i)
brr = gets.split(" ").map(&:to_i)
puts arr[k-1]<brr[-m] ? "YES" : "NO"

la,lb = gets.split(" ").map(&:to_i)
k,m = gets.split(" ").map(&:to_i)

arr = gets.split(" ").map(&:to_i)
brr = gets.split(" ").map(&:to_i)
for i in 0...lb
    if arr[k-1]<brr[i]
        break
    else
        i = lb
    end
end
if (lb-i)>=m
    puts "YES"
else
    puts "NO"
end

n = gets.chomp.split(" ").map!{|val| val=val.to_i}
na=n[0]
nb=n[1]

km = gets.chomp.split(" ").map!{|val| val=val.to_i}
k = km[0]
m = km[1]

Aarray = gets.chomp.split(" ").map! {|val| val=val.to_i}
Barray = gets.chomp.split(" ").map! {|val| val=val.to_i}

puts "YES" if Aarray[k-1] < Barray[nb-m]
puts "NO" unless Aarray[k-1] < Barray[nb-m]

# http://codeforces.com/contest/572/problem/A
x, y = gets.chomp.split.map(&:to_i)
n, m = gets.chomp.split.map(&:to_i)

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

puts a[n - 1] < b[y - m] ? 'YES' : 'NO'

class TaskA
  def initialize
    @nA, @nB = gets.split.map(&:to_i)
    @k, @m = gets.split.map(&:to_i)
    @a = gets.split.map(&:to_i)
    @b = gets.split.map(&:to_i)
  end

  def solve
    if @a[@k-1] < @b[-@m] then
      puts "YES"
    else
      puts "NO"
    end
  end
end

TaskA.new.solve

na, nb = gets.split.map(&:to_i)
k, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)[0..-1]
b = gets.split.map(&:to_i)[0..-1]
if(a[k-1] < b[nb - m])
    puts "YES"
else
    puts "NO"
end

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

puts (a[n-1]<b[b.size-m])?"YES":"NO"

gets
(k, m), a, b = 3.times.map { gets.split(' ').map(&:to_i) }

res = if k <= a.size && m <= b.size && a[k-1] < b[b.size-m]
  "YES"
else
  "NO"
end

puts res

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

q1 = gets.split.map(&:to_i)
q2 = gets.split.map(&:to_i)

puts q1[k-1] < q2[-m] ? "YES" : "NO"

na,nb = gets.split(' ').map(&:to_i)

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

a1 = gets.split(' ').map(&:to_i)
a2 = gets.split(' ').map(&:to_i)

if a1[k-1] < a2[-m]
  puts "YES"
else
   puts "NO"
 end

# coding: utf-8
C = Struct.new(:num, :tag)
na, nb = gets.split.map(&:to_i)
k, m = gets.split.map(&:to_i)
nar = gets.split.map(&:to_i) # non-desc order
nbr = gets.split.map(&:to_i) # non-desc order

if nar[k-1] < nbr[nb - m] # 이미 정렬되어있으므로 마지막에서 세번째 숫자만 비교하면 OK
    puts "YES"
else
    puts "NO"
end

na, nb = gets.chomp.split(' ').map(&:to_i)
k, m = gets.chomp.split(' ').map(&:to_i)
as = gets.chomp.split(' ').map(&:to_i)
bs = gets.chomp.split(' ').map(&:to_i)

puts as[k-1] < bs[nb-m] ? 'YES' : 'NO'

#!/usr/bin/ruby
z,(k,m),A,B=$<.map{|e|e.split.map &:to_i}
puts A.sort[k-1]<B.sort[-m] ? :YES : :NO

na, nb = gets.split.map(&:to_i)
k, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
puts a[k - 1] < b[-m] ? 'YES' : 'NO'

na, nb = gets.split.map(&:to_i)
k, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
bb = b.find_index{|x| x > a[k - 1]}
puts bb && b[bb..-1].size >= m ? 'YES' : 'NO'

na, nb = gets.split.map(&:to_i)
k, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
bb = b.find_index{|x| x > a[k - 1]}
c = []
c = b[bb..-1] if bb
puts c.size >= m ? 'YES' : 'NO'

nA,nB = gets.split.map(&:to_i)
k,m = gets.split.map(&:to_i)
x = gets.split.map(&:to_i)
y = gets.split.map(&:to_i)
puts x[(k-1)] < y[(nB - m)] ? "YES" : "NO"

n1, n2 = gets.split.map(&:to_i)
k, m = gets.split.map(&:to_i)
array1 = gets.split.map(&:to_i)
array2 = gets.split.map(&:to_i)
puts array1[k-1 ] < array2[n2 - m] ? 'YES' : 'NO'

# Ý tưởng:
# Để có khả năng k số trong array a luôn nhỏ hơn m số trong mảng b
# thì ta phải luôn chọn k số nhỏ nhất trong mảng a và m số lớn nhất trong b.
# Nói cách khác, ta phải luôn chọn k số đầu tiên ở a và m số cuối cùng ở b.
# Sau đó ta so sánh số lớn nhất được chọn trong k số ở a và số nhỏ nhất được chọn trong m số ở b.
# Nếu số lớn nhất trong k số đó nhỏ hơn số nhỏ nhất trong m số kia thì kết quả luôn là “YES”, ngược lại là “NO”

size_a, size_b = gets.chomp.split.map(&:to_i)
k, m = gets.chomp.split.map(&:to_i)

array_a = gets.chomp.split.map(&:to_i)
array_b = gets.chomp.split.map(&:to_i)

puts array_a[k - 1] < array_b[-m] ? "YES" : "NO"

def checks(sizes, selections, array_1, array_2)
  selections = selections.split.map(&:to_i)
  array_1 = array_1.split.map(&:to_i)
  array_2 = array_2.split.map(&:to_i)
  puts array_1.sort.first(selections[0]).max < array_2.sort.last(selections[1]).min ? 'YES' : 'NO'
end
checks(gets, gets, gets, gets)

# Array1: Find min elements base on selection after that find max element
# Array2: Find max elements base on selection after that find min element
# Condition value of 2 array if array 1 < array 2 then print 'Yes' otherwise print 'No'

#!/usr/bin/ruby

# def qselect(a, b, k, m)
#   def qloc(a, b)
#     l = a.length
#     return -1 if b <= a[0]
#     return l if b > a[-1]
#     i = 0
#     loop do
#       break -1 if i < 0 or i >= l
#       break i if a[i+1] and b > a[i] and b <= a[i+1]
#       break i if not a[i+1] and b > a[i]
#       i = b > a[i] ? (l + i + 1) / 2 : i / 2
#     end
#   end
#   return false if k > a.length or m > b.length
#   p = qloc(a, b[-m])
#   k <= p + 1
# end

def algo
  gets
  k, m = gets.split(' ').map { |e| e.to_i }
  a = gets.split(' ').map { |e| e.to_i }
  b = gets.split(' ').map { |e| e.to_i }
  # puts qselect(a, b, k, m) ? "YES" : "NO"
  puts b[-m] > a[k-1] ? "YES" : "NO"
end

algo

def checks(sizes, selections, array_1, array_2)
  selections = selections.split(' ').map(&:to_i)
  array_1 = array_1.split(' ').map(&:to_i)
  array_2 = array_2.split(' ').map(&:to_i)

  max_1 = array_1.sort.first(selections[0]).max
  max_2 = array_2.sort.last(selections[1]).min

  if max_1 < max_2
    r = 'YES'
  else
    r = 'NO'
  end
  puts r
end
checks(gets.chomp, gets.chomp, gets.chomp, gets.chomp)

def checks(sizes, selections, array_1, array_2)
  selections = selections
  array_1 = array_1.split
  array_2 = array_2.split
  puts 1 < 2 ? 'YES' : 'NO'
end
checks(gets.chomp, gets.chomp, gets.chomp, gets.chomp)

la,lb = gets.split(" ").map(&:to_i)
k,m = gets.split(" ").map(&:to_i)

arr = gets.split(" ").map(&:to_i)
brr = gets.split(" ").map(&:to_i)
for i in 0...lb
    if arr[k-1]<brr[i]
        break
    end
end
if (lb-(i+1))>=m
    puts "YES"
else
    puts "NO"
end

la,lb = gets.split(" ").map(&:to_i)
k,m = gets.split(" ").map(&:to_i)

arr = gets.split(" ").map(&:to_i)
brr = gets.split(" ").map(&:to_i)
if k<=la && m<=lb && arr[k-1]<brr[0]
    puts "YES"
else
    puts "NO"
end

la,lb = gets.split(" ").map(&:to_i)
k,m = gets.split(" ").map(&:to_i)

arr = gets.split(" ").map(&:to_i)
brr = gets.split(" ").map(&:to_i)
for i in 0...lb
    if arr[k-1]<brr[i]
        break
    end
end
if (lb-i)>=m
    puts "YES"
else
    puts "NO"
end

na,nb = gets.split(' ').map(&:to_i)

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

a1 = gets.split(' ').map(&:to_i)
a2 = gets.split(' ').map(&:to_i)

if a1[k-1] < a2[0]
  puts "YES"
else
   puts "NO"
 end

na, nb = gets.split.map(&:to_i)
k, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
bb = b.find_index{|x| x > a[k - 1]}
puts b[(bb || 0)..-1].size >= m ? 'YES' : 'NO'

skip = gets.chomp.to_i
number_choose = gets.split.map(&:to_i)
arrayA = gets.split.map(&:to_i)
arrayB = gets.split.map(&:to_i)
choosenA = arrayA.last(number_choose[0])
minB = arrayB.first(number_choose[1]).min
puts choosenA.all?{|x| x < minB}  ? 'YES' : 'NO'

skip = gets.chomp
select_numbers = gets.split.map(&:to_i)
array1 = gets.split.map(&:to_i)
array2 = gets.split.map(&:to_i)
count = 0
array = array1 + array2
(array1.length...array.length).each do|index|
  if array[index] <= array[index-1]
    count = index
    break
  end
end

puts ( count > select_numbers.max) ? 'YES' : 'NO'

skip = gets.chomp
select_numbers = gets.split.map(&:to_i)
array1 = gets.split.map(&:to_i)
array2 = gets.split.map(&:to_i)
count = 0
array = array1 + array2
(1...array.length).each do|index|
  if array[index] <= array[index-1]
    count = index
    break
  end
end
puts ( count > select_numbers.max) ? 'YES' : 'NO'

skip = gets.chomp.to_i
number_choosens = gets.split.map(&:to_i)
arrayA = gets.split.map(&:to_i)
arrayB = gets.split.map(&:to_i)
possile_of_a = arrayA.select{|x| x < arrayB.min}
possile_of_b = arrayB.select{|x| x > arrayA.max}
puts (possile_of_a.size >= number_choosens[0] && possile_of_b.size >= number_choosens[1]) ? 'YES' : 'NO'

#!/usr/bin/ruby

def qselect(a, b, k, m)
  def qloc(a, b)
    l = a.length
    i = 0
    loop do
      break -1 if i < 0 or i >= l
      break i if a[i+1] and b > a[i] and b <= a[i+1]
      break i if not a[i+1] and b > a[i]
      i = b >= a[i] ? (l + i + 1) / 2 : (i - 1) / 2
    end
  end

  p = qloc(a, b[0])
  return false if p < 0
  k <= p + 1 && m <= b.length
end

def algo
  gets
  k, m = gets.split(' ').map { |e| e.to_i }
  a = gets.split(' ').map { |e| e.to_i }
  b = gets.split(' ').map { |e| e.to_i }
  puts qselect(a, b, k, m) ? "YES" : "NO"
end

algo

#!/usr/bin/ruby

# def qselect(a, b, k, m)
#   def qloc(a, b)
#     l = a.length
#     return -1 if b <= a[0]
#     return l if b > a[-1]
#     i = 0
#     loop do
#       break -1 if i < 0 or i >= l
#       break i if a[i+1] and b > a[i] and b <= a[i+1]
#       break i if not a[i+1] and b > a[i]
#       i = b > a[i] ? (l + i + 1) / 2 : i / 2
#     end
#   end
#   return false if k > a.length or m > b.length
#   p = qloc(a, b[-m])
#   k <= p + 1
# end

def algo
  gets
  k, m = gets.split(' ').map { |e| e.to_i }
  a = gets.split(' ').map { |e| e.to_i }
  b = gets.split(' ').map { |e| e.to_i }
  # puts qselect(a, b, k, m) ? "YES" : "NO"
  puts b[-m] > a[-k] ? "YES" : "NO"
end

algo

#!/usr/bin/ruby

def qselect(a, b, k, m)
  def qloc(a, b)
    l = a.length
    return -1 if b <= a[0]
    return l if b > a[-1]
    i = 0
    loop do
      break -1 if i < 0 or i >= l
      break i if a[i+1] and b > a[i] and b <= a[i+1]
      break i if not a[i+1] and b > a[i]
      i = b > a[i] ? (l + i + 1) / 2 : i / 2
    end
  end

  p = qloc(a, b[0])
  return false if p < 0
  k <= p + 1 && m <= b.length
end

def algo
  gets
  k, m = gets.split(' ').map { |e| e.to_i }
  a = gets.split(' ').map { |e| e.to_i }
  b = gets.split(' ').map { |e| e.to_i }
  puts qselect(a, b, k, m) ? "YES" : "NO"
end

algo

def checks(sizes, selections, array_1, array_2)
  selections = selections
  puts 1 < 2 ? 'YES' : 'NO'
end
checks(gets.chomp, gets.chomp, gets.chomp, gets.chomp)

def checks(sizes, selections, array_1, array_2)

  puts 'YES'
end
checks(gets.chomp, gets.chomp, gets.chomp, gets.chomp)


gets.to_i.times{
    a,b=gets.split
    puts a.to_i*b.to_i
}

t = gets.chomp.to_i
t.times do
	a,b = gets.split()
	p a.to_i*b.to_i
end


t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    a = gets.chomp.split(" ")
    number_zero = a.count("0")
    number_ichi = n - number_zero
    if number_zero >= number_ichi
        a.delete("1")
        puts a.size
        puts a.join(" ")
    else
        a.delete("0")
        ookisa = a.size
        if ookisa % 2 == 0
            puts a.size
            puts a.join(" ")
        else
            a.pop
            puts a.size
            puts a.join(" ")
        end
    end
end

T = gets.to_i

T.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  rui_e = [0]
  rui_o = [0]

  a.each_with_index do |x, i|
    if i.even?
      rui_e << rui_e.last + x
      rui_o << rui_o.last
    else
      rui_e << rui_e.last
      rui_o << rui_o.last + x
    end
  end

  idx = -1

  n.times do |i|
    v1 = (rui_e[i] - rui_e[0]) + (rui_o[n] - rui_o[i + 1])
    v2 = (rui_o[i] - rui_o[0]) + (rui_e[n] - rui_e[i + 1])

    if v1 == v2
      idx = i
      break
    end
  end

  if idx != -1
    a.delete_at(idx)
  end

  puts a.size
  puts a.join(' ')
end

T = gets.to_i

T.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  s = a.sum
  t = s / 2
  ans = []
  sum = [0, 0]

  if n <= 2
    puts 1
    puts 0
    next
  end

  until a.empty?
    x = a.shift

    if ans.size.even?
      next if x + sum[0] > t

      sum[0] += x
      ans << x
    else
      next if x + sum[1] > t

      sum[1] += x
      ans << x
    end
  end

  puts ans.size
  puts ans.join(' ')
end

t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    a = gets.chomp.split(" ")
    number_zero = a.count("0")
    number_ichi = n - number_zero
    if number_zero >= number_ichi
        a.delete("1")
        puts a.join(" ")
    else
        a.delete("0")
        ookisa = a.size
        if ookisa % 2 == 0
            puts a.join(" ")
        else
            a.pop
            puts a.join(" ")
        end
    end
end

T = gets.to_i

T.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  s = a.sum
  t = s / 2
  ans = []
  sum = [0, 0]

  until a.empty?
    x = a.shift

    if ans.size.even?
      next if x + sum[0] > t

      sum[0] += x
      ans << x
    else
      next if x + sum[1] > t

      sum[1] += x
      ans << x
    end
  end

  puts ans.size
  puts ans.join(' ')
end

T = gets.to_i

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

  counter = a.tally
  counter.default = 0

  if counter[0] >= 1
    puts 1
    puts 0
  elsif counter[1] >= 2
    puts 2
    puts "1 1"
  end
end


def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end
def inpf() a=gets.chomp.split(" ").map(&:to_f)end
def inps() a=gets.chomp.split(" ")end
def copy(a) Marshal.load(Marshal.dump(a)) end
def kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end
def scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end
def na(n=0,d=0) Array.new(n,d)end
def na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end
def na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end
def bit(n) n.to_s(2).split("").map(&:to_i) end
def inp() a=gets.chomp.split(" ").map(&:to_i)end

n = inp[0]
a = inp
b = inp
table = na(n)
encoded = na(n)
n.times do |i|
  encoded[a[i]-1] = i
end

pl = 0
ans = 0
i = 0
while i < n
  if(table[i] == 1)
    pl -= 1
    i += 1
    next
  end
  if(a[i] != b[i+pl])
    ans += 1
    table[encoded[b[i+pl]-1]] = 1
    pl +=1
  else
    i += 1
  end
end
p ans

n = gets.to_i
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
out = [false]*(n+1)

ans = 0
j = 0
n.times do |i|
  if !out[a[i]]
    while b[j] != a[i]
      out[b[j]] = true
      j += 1
      ans += 1
    end
    out[b[j]] = true
    j += 1
  end
end
p ans


MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
q=gi
q.times do
  bat,turn,a,b=gsmi

  if bat <= turn*b
    puts -1
    next
  end

  b_bat = bat - turn*b
  puts [(b_bat-1)/(a-b),turn,((bat-a).to_f/a).ceil].min
end

q = gets.chomp.to_i

for ii in 1..q
  (k, n, a, b) = gets.chomp.split(" ").map {|aaa| aaa.to_i}
  _min = n*b
  k -= 1
  d = a-b
  if _min > k
    print -1
  else
    _max = (k -_min)/d
    print _max > n ? n : _max
  end
  print "\n"
end

q = gets.to_i
(1..q).each do
  res = 0
  read = gets.split(" ").map{|c| c.to_i}
  k = read[0]
  n = read[1]
  a = read[2]
  b = read[3]
  if n * b >= k
    res = -1
  else
    l = 0
    r = n
    while l < r
      m = (l + r + 1)/2
      if m * a + (n - m) * b < k
        l = m
      else
        r = m - 1
      end
    end
    res = l
  end
  puts res
end

MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
q=gi
q.times do
  bat,turn,a,b=gsmi

  if bat <= turn*b
    puts -1
    next
  end

  b_bat = bat - turn*b
  puts [b_bat/(a-b),turn,((bat-a).to_f/a).ceil].min
end


#!/usr/bin/env ruby

n=gets.chomp.to_i
str=[]
n.times{str.push(gets.chomp)}

str.sort!{|l, r| l.size<=>r.size}

(n-1).downto 1 do |i|
  s, t=str[i-1], str[i]
  if t.index(s).nil?
    puts 'NO'
    exit 0
  end
end

puts 'YES'
puts str.join("\n")

n = gets.to_i
s = (1..n).map { gets.chomp }.sort_by(&:size)
if s.each_cons(2).all? { |s, t| (0...t.size).any? { |i| s == t[i, s.size] } }
	puts 'YES', s.join("\n")
else
	puts 'NO'
end

n = gets.to_i
ar = []
n.times {ar << gets.chomp}
ar.sort!{|a,b| a.size <=> b.size}
n.times do |i|
  next if i == 0
  s = ar[i]
  s0 = ar[i-1]
  if !s.include? s0
    puts "NO"
    exit 0
  end
end
puts "YES"
puts ar

n = gets.to_i
a = []
for i in 0..n-1
      x = gets.chomp
      a.push(x)
      end
y = a.sort_by(&:length)
v = 1
for i in 0..n-2
      if y[i+1].include?y[i] or y[i+1] == y[i]
       v += 1
     end
  end
if v != a.length
   puts "NO"
else
   puts "YES"
   for i in 0..a.length-1
      puts "#{y[i]}"
      end
  end