#!/usr/bin/env ruby

livings_count = gets.chop.to_i

livings_relation = {}
trees = []

relations = gets.chop.split

relations.each.with_index do |b , a|
  a += 1
  b = b.to_i

  if !livings_relation[b] && !livings_relation[a]
    trees.push(Array.new)
    trees_index = trees.size - 1
    livings_relation[a] = trees_index
    livings_relation[b] = trees_index
    trees[trees_index] << a
    trees[trees_index] << b
  elsif !livings_relation[b] && livings_relation[a]
    tree_index = livings_relation[a]
    livings_relation[b] = livings_relation[a]
    trees[tree_index] << b
  elsif livings_relation[b] && !livings_relation[a]
    tree_index = livings_relation[b]
    livings_relation[a] = livings_relation[b]
    trees[tree_index] << a
  elsif livings_relation[a] != livings_relation[b]
    # merge
    tree_index = livings_relation[a]
    tree_index_del = livings_relation[b]
    #livings_relation[b] = livings_relation[a]
    trees[tree_index_del].each do |living|
      livings_relation[living] = tree_index
      trees[tree_index] << living
    end
    trees[tree_index_del] = nil
  end
end

puts trees.compact.size

require 'set'
n = gets.to_i
list = [0] + gets.chomp.split(" ").map(&:to_i)
set = Set.new
for i in 1..n
  if(list[list[i]] == i)
    set.add([i,list[i]].min)
  end
end
print set.length


require 'pp'

n = STDIN.gets.chomp.to_i
os = STDIN.gets.chomp.split(/ /).map(&:to_i)

ss = [(1..n).to_a, os].transpose
ss.sort!{|a, b| a.last <=> b.last }

s = os.sort
count=1
leg = -1
same3 = nil
same2 = []
(n-1).times do |i|
  if s[i] == s[i+1]
    count+=1
    if count >=3
      same3 = s[i]
      break
    elsif count >=2
      same2.push s[i]
    end
  else
    count = 1
  end
end

if same3
  ans = []
  ans.push ss.map{|a,b| a}

  pos = []
  ss.each_with_index do |sse,i|
    pos.push i if sse.last == same3
  end
  ss[pos[0]], ss[pos[1]], ss[pos[2]] = ss[pos[1]], ss[pos[2]], ss[pos[0]]
  ans.push ss.map{|a,b| a}
  ss[pos[0]], ss[pos[1]], ss[pos[2]] = ss[pos[1]], ss[pos[2]], ss[pos[0]]
  ans.push ss.map{|a,b| a}

  puts "YES"
  ans.each{|a| puts a.join(" ")}
elsif same2.length >= 2
  ans = []
  ans.push ss.map{|a,b| a}

  pos0 = []
  pos1 = []
  ss.each_with_index do |sse,i|
    pos0.push i if sse.last == same2[0]
    pos1.push i if sse.last == same2[1]
  end
  ss[pos0.first], ss[pos0.last] = ss[pos0.last], ss[pos0.first]
  ans.push ss.map{|a,b| a}
  ss[pos1.first], ss[pos1.last] = ss[pos1.last], ss[pos1.first]
  ans.push ss.map{|a,b| a}

  puts "YES"
  ans.each{|a| puts a.join(" ")}
else
  puts "NO"
end

a=gets.chomp.to_i
b=gets.chomp.split(" ").map {|i|  i.to_i }
inde=Hash.new {|hash,key|  hash[key]=[]  }
count=Array.new(2001,0)
0.upto(a-1) do |i|
inde[b[i]]<<i+1
count[b[i]]+=1
end
keys=inde.keys.sort
cp=count.max

if cp<2
puts "NO"

elsif cp>2
puts "YES"
p=count.index(cp)
3.times do
keys.each do |i|
print "#{inde[i].join(" ")} "
end
puts
temp=inde[p][0]
temp2=inde[p][1]
inde[p][1]=inde[p][2]
inde[p][2]=temp
inde[p][0]=temp2
end

elsif cp==2

if count.count(cp)<2
puts "NO"

else
index1=count.index(cp)
count.delete_at(index1)
index2=count.index(cp)+1

it=0
puts "YES"
3.times do

keys.each do |i|
print "#{inde[i].join(" ")} "
end
puts
if it==0
temp=inde[index1][0]
inde[index1][0]=inde[index1][1]
inde[index1][1]=temp
it+=1
else
temp=inde[index2][0]
inde[index2][0]=inde[index2][1]
inde[index2][1]=temp
end

end

end

end

#!/usr/bin/env ruby

n = gets.to_i
h = gets.split.map(&:to_i)
sol = []
100.times do
  x = h.map{|x| x * 10 + rand(10)}.map.with_index(1).sort.map(&:last)
  sol << x if !sol.include?(x)
  break if sol.size == 3
end

if sol.size == 3
  puts 'YES'
  puts sol.map{|x| x.map(&:to_s).join(' ')}
else
  puts 'NO'
end

n = STDIN.readline.to_i
values = STDIN.readline.split.map {|s| s.to_i }
value_indices = {}
values.each_with_index do |value, index|
	value_indices[value] ||= []
	value_indices[value].push(index+1)
end
perturb_two, perturb_three = [], nil
value_indices.each do |value, indices|
	if indices.length >= 3
		perturb_three = value
		break
	elsif indices.length == 2
		perturb_two.push(value)
		break if perturb_two.length == 2
	end
end
orders = 3.times.map { [] }
if perturb_three != nil
	value_indices.keys.sort.each do |value|
		indices = value_indices[value]
		if value == perturb_three
			orders[0].push(indices[0])
			orders[0].push(indices[1])
			orders[0].push(indices[2])
			orders[1].push(indices[1])
			orders[1].push(indices[2])
			orders[1].push(indices[0])
			orders[2].push(indices[2])
			orders[2].push(indices[0])
			orders[2].push(indices[1])
			indices[3..-1].each {|index| 3.times {|p| orders[p].push(index) } }
		else
			indices.each {|index| 3.times {|p| orders[p].push(index) } }
		end
	end
elsif perturb_two.length == 2
	value_indices.keys.sort.each do |value|
		indices = value_indices[value]
		if perturb_two.index(value) == 0
			orders[0].push(indices[0])
			orders[0].push(indices[1])
			orders[1].push(indices[0])
			orders[1].push(indices[1])
			orders[2].push(indices[1])
			orders[2].push(indices[0])
		elsif perturb_two.index(value) == 1
			orders[0].push(indices[0])
			orders[0].push(indices[1])
			orders[1].push(indices[1])
			orders[1].push(indices[0])
			orders[2].push(indices[1])
			orders[2].push(indices[0])
		else
			indices.each {|index| 3.times {|p| orders[p].push(index) } }
		end
	end
else
	puts 'NO'
	exit
end
puts 'YES'
3.times {|i| puts orders[i].join(' ') }

n = gets.to_i
h = gets.split.map(&:to_i)
if h.uniq.size > n - 2
  puts "NO"
else
  puts "YES"
  h.map!.with_index { |d, i| [d, i + 1] }.sort_by! { |d, i| d }

  puts h.map { |d, i| i }.join(" ")

  1.upto(n - 1).each do |i|
    if h[i - 1][0] == h[i][0]
      h[i - 1], h[i] = h[i], h[i - 1]
      break
    end
  end

  puts h.map { |d, i| i }.join(" ")

  (n - 1).downto(1).each do |i|
    if h[i - 1][0] == h[i][0]
      h[i - 1], h[i] = h[i], h[i - 1]
      break
    end
  end

  puts h.map { |d, i| i }.join(" ")
end


gets.to_i.times do
    n = gets.to_i
    puts n.downto(1).each_slice(2).to_a.reverse.join(' ')
end

gets.to_i.times {
	n = gets.to_i
	a = n.odd? ? 1 : 0
	if n.odd? ; print '1 '; end
	(n/2).times{|i| print i*2+2+a, ' ', i*2+1+a, ' '} ; puts ''
}

gets.to_i.times do
  n = gets.to_i
  if n % 2 == 0
    puts 1.upto(n).map { |i| i + (i % 2 == 0 ? -1 : 1) }.join(" ")
  else
    puts ([1] + (2.upto(n).map { |i| i + (i % 2 == 0 ? 1 : -1) })).join(" ")
  end
end

gets.to_i.times {
	n = gets.to_i
	if n.even?
		n.times{|i| print n-i, ' '}; puts ''
	else
		(n/2).times{|i| print n-i*2-1 , ' ', n-i*2, ' '}; puts 1
	end
}

gets.to_i.times { gets.to_i.times{|i| print i+1, ' '}; puts ''}

gets.to_i.times do
  n=gets.to_i
  puts ((2..n).to_a+[1])*' '
end

gets.to_i.times do
  n=gets.to_i
  puts ([n]+(1...n).to_a)*' '
end

gets.to_i.times do
    n = gets.to_i
    n.downto(1).each_slice(2).to_a.reverse.join(' ')
end


gets
a = gets.split.map(&:to_i).uniq.sort

puts (a.size <= 2 || (a.size == 3 && a[1]-a[0] == a[2]-a[1])) ? 'YES' : 'NO'

n = gets.to_i
a = gets.split.map(&:to_i)
au = a.uniq
goal = au.size == 1 ? au[0] : au.sort[1]
a1 = a.find{|e| e != goal}
x = a1 ? (goal - a1).abs : 0
puts a.all?{|e| (goal - e).abs == 0 || (goal - e).abs == x} ? 'YES' : 'NO'

n=readline.to_i
a=readline.split.map(&:to_i).uniq.sort

if a.size <= 2 || (a.size == 3 && a[1]-a[0] == a[2]-a[1])
  puts 'YES'
else
  puts 'NO'
end

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

min = arr.min
max = arr.max

alpha = ((max - min) / 2.0)

ans = 'YES'

correctNum = (min + alpha)
#puts correctNum
arr.each do |i|
	if i != correctNum && i + alpha != correctNum && i - alpha != correctNum
		ans = 'NO'
		break
	end
end

puts ans

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

max = a.max
min = a.min
mid = (max+min)/2.0

puts a.all?{|x| x==max || x==min || x==mid} ? "YES" : "NO"

gets
a=gets.split.map(&:to_i).group_by{|e|e}.map(&:first).sort
puts a.size<=2 || (a.size==3&&a[2]-a[1]==a[1]-a[0]) ? :YES : :NO

n =gets.to_i ; list = gets.chomp.split(" ").map(&:to_i).uniq.sort
puts (list.length <= 2 || (list.length <=3 && (2*list[1] == list[0] + list[2])))? "YES" : "NO"

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

a = arr[0]
arr.delete(a)
if arr.length > 0
  b = arr[0]
  arr.delete(b)
  if arr.length > 0
    c = arr[0]
    arr.delete(c)
    if arr.length > 0
      puts "NO"
    else
      lol = [a,b,c]
      x = lol.min
      z = lol.max
      lol.delete(x)
      lol.delete(z)
      if lol[0] * 2 == (x + z)
        puts "YES"
      else
        puts "NO"
      end
    end
  else
    puts "YES"
  end
else
  puts "YES"
end

n,*a=$<.read.split.map &:to_i
a.sort!.uniq!
n=a.size
x=a[-1]-a[0]
if n>3 or (n==3 and (x%2==1 or a[1]!=a[0]+x/2))
    puts :NO
else
    puts :YES
end

n = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
k = (a.max-a.min)/2
if a.uniq.count<3 || (a.uniq.count==3&&a.include?(k+a.min)&&a.include?(a.max-k)) then s="YES" else s="NO" end
puts s

n = gets
x = gets.split.map(&:to_i).sort.uniq
if (x.size <= 2) or (x.size == 3 and x[0]+x[2]==x[1]*2)
  puts "YES"
else
  puts"NO"
end

gets
a=gets.split.map(&:to_i).group_by{|e|e}.map(&:first).sort
puts (a.size==3&&a[2]-a[1]==a[1]-a[0]) || a.size==2 ? :YES : :NO

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

a = arr[0]
arr.delete(a)
if arr.length > 0
  b = arr[0]
  arr.delete(b)
  if arr.length > 0
    c = arr[0]
    arr.delete(c)
    if arr.length > 0
      puts "NO"
    else
      lol = [a,b,c]
      x = lol.min
      z = lol.max
      lol.delete(x)
      lol.delete(z)
      if lol[0] * 2 == (x + z)
        puts "YES"
      else
        puts "NO"
      end
    end
  else
    puts "NO"
  end
else
  puts "NO"
end

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

if a.uniq.count==3&&a.include?(((a.max-a.min)/2).to_i+a.min) then s="YES" else s="NO" end
puts s

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

if a.uniq.count<3 || (a.uniq.count==3&&a.include?(((a.max-a.min)/2)+a.min)) then s="YES" else s="NO" end
puts s

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

if a.uniq.count==3&&a.include?(((a.max-a.min)/2)+a.min) then s="YES" else s="NO" end
puts s

n = gets.to_i
a = gets.split.map(&:to_i)
sum = a.inject(&:+)
if sum % n == 0
  goal = sum / n
  a1 = a.find{|e| e != goal}
  if a1
    x = (goal - a1).abs
    puts a.all?{|e| (goal - e).abs == 0 || (goal - e).abs == x} ? 'YES' : 'NO'
  else
    puts 'YES'
  end
else
  puts 'NO'
end

n = gets.to_i
a = gets.split.map(&:to_i)
sum = a.inject(&:+)
if sum % n == 0
  goal = sum / n
  a1 = a.find{|e| e != goal}
  if a1
    x = (goal - a1).abs
    pos = a.count{|e| e - x == goal}
    neg = a.count{|e| e + x == goal}
    eq = a.count{|e| e == goal}
    puts pos == neg && pos + neg + eq == n ? 'YES' : 'NO'
  else
    puts 'YES'
  end
else
  puts 'NO'
end

n=readline.to_i
a=readline.split.map(&:to_i).uniq.sort

if a.size == 1 || (a.size == 3 && a[1]-a[0] == a[2]-a[1])
  puts 'YES'
else
  puts 'NO'
end

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

min = arr.min
max = arr.max

alpha = (max - min) / 2

ans = 'YES'

correctNum = min + alpha
#puts correctNum
arr.each do |i|
	if i != correctNum && i + alpha != correctNum && i - alpha != correctNum
		ans = 'NO'
		break
	end
end

puts ans


t = gets.to_i
t.times{
  x = gets.to_i
  (x..x+100).each{|x|if x.gcd(x.digits.sum)>1 then p x;break end}
}

def gs(n)
  s=n.to_s.chars.map(&:to_i).sum
  return n.gcd(s)
end
gets.to_i.times do
  n=gets.to_i
  i=n
  while
    g=gs(i)
    if g>1
      puts i
      break
    end
    i+=1
  end
end

def digitSum(num)
  s = 0
  while num > 0
    s += num % 10
    num = num / 10
  end
  s
end

gets.to_i.times {
  n = gets.to_i

  while true
    s = digitSum n
    o = n.gcd s
    if o > 1
      puts n
      break
    end
    n += 1
  end
}

def digitSum(num)
  s = 0
  while num > 0
    s += num % 10
    num = num / 10
  end
  s
end

def gcd1 (a, b)
  if b == 0
    return a
  end
  gcd1(b, a % b)
end

t = gets.to_i
(1..t).each { |i|
  n = gets.to_i

  while true
    s = digitSum n
    o = gcd1(n, s)
    if o > 1
      puts n
      break
    end
    n += 1
  end
}


gets.to_i.times do
  n,m=gets.split.map(&:to_i)
  a=gets.split.map(&:to_i)
  b=gets.split.map(&:to_i)
  a1=[]
  a2=[]
  n.times do |i|
    if b[i]==1
      a1 << a[i]
    else
      a2 << a[i]
    end
  end
  a1.sort!.reverse!
  a2.sort!.reverse!
  z1=a1.size
  z2=a2.size
  i=0
  k=0
  while i<z1&&k<m
    k+=a1[i]
    i+=1
  end
  i-=1
  if k<m
    j=0
    while j<z2&&k<m
      k+=a2[j]
      j+=1
    end
    min=i+1+j*2
    if k<m
      puts "-1"
      next
    end
  else
    min=i+1
    j=0
  end
  while i>=0
    k-=a1[i]
    i-=1
    while k<m&&j<z2
      k+=a2[j]
      j+=1
    end
    if k>=m&&(i+1)+j*2<min
      min=(i+1)+j*2
    end
  end
  puts min
end

INF = 10**18

gets.to_i.times do |testcase|
	n, m = gets.split.map(&:to_i)
	a = gets.split.map(&:to_i)
	b = gets.split.map(&:to_i)

	a1 = (0...n).select { b[_1] == 1 }.map { a[_1] }.sort.reverse
	a2 = (0...n).select { b[_1] == 2 }.map { a[_1] }.sort.reverse

	sum1 = [0] * -~a1.size
	a1.size.times { |i| sum1[i + 1] = sum1[i] + a1[i] }
	sum2 = [0] * -~a2.size
	a2.size.times { |i| sum2[i + 1] = sum2[i] + a2[i] }

	ans =
		sum2.each_with_index.map do |s2, cnt2|
			cnt1 = sum1.bsearch_index { _1 >= m - s2 }
			cnt1 ? cnt2 * 2 + cnt1 : INF
		end.min

	puts ans < INF ? ans : -1
end

#!/usr/bin/env ruby

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

def pfr(ar)
  res = (0..ar.length).map{|x| 0}
  for i in (1..ar.length)
    res[i] = res[i-1] + ar[i-1];
  end
  return res
end

class Opt

  def initialize(mem, cost, units)
    ar = mem.zip(cost)
    @one = pfr(ar.select{|x, y| (y == 1)}.map{|x, y| x}.sort.reverse)
    @two = pfr(ar.select{|x, y| (y == 2)}.map{|x, y| x}.sort.reverse)
    @units = units
  end

  def bsearch(val)
    # find ind of two where two[i] >= val
    if @two[@two.length-1] < val
      return nil
    end
    l,r = [0, @two.length-1]
    while true
      if r-l <= 1
        return(@two[l] >= val ? l : r)
      end
      mid = (l+r).div(2)
      if @two[mid] >= val
        r = mid
      else
        l = mid
      end
    end
  end

  def cost(ones)
    if ones >= @one.length
      return nil
    end
    ind = self.bsearch(@units - @one[ones])
    return (ind ? (ones + 2*ind) : nil)
  end

  def ones
    @one.length
  end

end

tc = getar()[0]

for t in 1..tc
  n, m = getar
  mem = getar
  cost = getar

  opt = Opt.new(mem, cost, m)
  possible = (0..opt.ones).map{|x| opt.cost(x)}.select{|x| x}.min
  puts "#{possible ? possible : -1}"
end

INF = 10**18

gets.to_i.times do |testcase|
	n, m = gets.split.map(&:to_i)
	a = gets.split.map(&:to_i)
	b = gets.split.map(&:to_i)

	a1 = (0...m).select { b[_1] == 1 }.map { a[_1] }.sort.reverse
	a2 = (0...m).select { b[_1] == 2 }.map { a[_1] }.sort.reverse

	sum1 = [0] * -~a1.size
	a1.size.times { |i| sum1[i + 1] = sum1[i] + a1[i] }
	sum2 = [0] * -~a2.size
	a2.size.times { |i| sum2[i + 1] = sum2[i] + a2[i] }

	ans2 =
		sum2.each_with_index.map do |s2, cnt2|
			cnt1 = sum1.bsearch_index { _1 >= m - s2 }
			cnt1 ? cnt2 * 2 + cnt1 : INF
		end.min
	ans1 =
		sum1.each_with_index.map do |s1, cnt1|
			cnt2 = sum2.bsearch_index { _1 >= m - s1 }
			cnt2 ? cnt2 * 2 + cnt1 : INF
		end.min
	ans = [ans1, ans2].min

	puts ans < INF ? ans : -1
end

INF = 10**18

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

	a1 = (0...m).select { b[_1] == 1 }.map { a[_1] }.sort.reverse
	a2 = (0...m).select { b[_1] == 2 }.map { a[_1] }.sort.reverse

	sum1 = [0] * -~a1.size
	a1.size.times { |i| sum1[i + 1] = sum1[i] + a1[i] }
	sum2 = [0] * -~a2.size
	a2.size.times { |i| sum2[i + 1] = sum2[i] + a2[i] }

	ans =
		sum2.each_with_index.map do |s2, cnt2|
			cnt1 = sum1.bsearch_index { _1 >= m - s2 }
			cnt1 ? cnt2 * 2 + cnt1 : INF
		end.min
	puts ans < INF ? ans : -1
end


input=gets.split.map(&:to_i)
h=input[0]
w=input[1]
k=input[2]
q=input[3]
cnt=Array.new(h,0)
waste=[]
k.times{
    input=gets.split.map(&:to_i)
    input[0]-=1
    input[1]-=1
    waste.push(input)
    cnt[input[0]]+=1
}
q.times{
    input=gets.split.map(&:to_i)
    input[0]-=1
    input[1]-=1
    x=input[1]
    y=input[0]
    no=y*w+x
    is_was=0
    waste.each{|was|
        if was[0]<y then
            no-=1
        end
        if was[0]==y then
            if was[1]<=x then
                no-=1
            end
            if was[1]==x then
                is_was+=1
            end
        end
    }
    if is_was>0 then
        puts "Waste"
    elsif
        puts ["Carrots","Kiwis","Grapes"][no%3]
    end
}

require 'set';def f c,m;x,y=c;(x-1)*m+y;end;n,m,k,t=gets.split.map(&:to_i);d=[];z=Set.new;k.times{p=f gets.split.map(&:to_i), m;d<<p;z<<p};g=[];t.times{g<<(f gets.split.map(&:to_i), m)};g.each{|b| puts(z.include?(b) ? :Waste : [:Carrots,:Kiwis,:Grapes][(b-d.count{|v|v<b})%3-1])}

def f c,m;x,y=c;(x-1)*m+y;end;n,m,k,t=gets.split.map(&:to_i);d=[];k.times{p=f gets.split.map(&:to_i),m;d<<p};g=[];t.times{g<<(f gets.split.map(&:to_i),m)};g.each{|b|puts(d.include?(b) ? :Waste : [:Carrots,:Kiwis,:Grapes][(b-d.count{|v|v<b})%3-1])}

#=================================
# Better be sure than sorry.
#=================================
$stdin=File.open("./input.txt","r") if ENV["LOGNAME"]=="skydos"
require 'prime'
require 'mathn'
require 'matrix'
require 'set'
include Math
#=================================

def to_single_number coords, m
	x, y=coords
	(x-1)*m+y
end

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

bad_cells=[]
bad_cells_set=Set.new
k.times{
	hash_code = to_single_number gets.split.map(&:to_i), m

	bad_cells << hash_code
	bad_cells_set << hash_code
}
bad_cells.sort!

tests=[]
t.times{
	tests << (to_single_number gets.split.map(&:to_i), m)
}

fr = [:Carrots, :Kiwis, :Grapes]
tests.each{|coord|
	if bad_cells_set.include? coord
		puts :Waste
		next
	end

	bad_cells_before = bad_cells.count{|v| v<coord}
	remaining_cells = coord - bad_cells_before

	puts fr[remaining_cells%3-1]
}

def b_search(a, v)

  x0 = 0
  x1 = a.size - 1
  return nil if a[x0] == v || a[x1] == v

  return 0 if v < a[0]
  return a.size if v > a[x1]

  while x1 - x0 > 1
    x2 = (x0+x1)/2
    return nil if a[x2] == v
    if v > a[x2]
      x0 = x2
    else
      x1 = x2
    end
  end

  x1
end

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

a = []
k.times  do
  i, j = gets.split.map(&:to_i)
  a << (i-1)*m+(j-1)
end
a.uniq!
a.sort!

t.times do
  i, j = gets.split.map(&:to_i)
  pos = (i-1)*m+(j-1)

  v = b_search(a, pos)
  if v
    puts %w(Carrots Kiwis Grapes)[(pos-v)%3]
  else
    puts "Waste"
  end
end

n, m, k, t = gets.split.map(&:to_i)
A = Hash.new(0)
k.times do
  a, b = gets.split.map { |x| x.to_i - 1}
  A[a * m + b] = 1
end
M = Array.new
(A.keys.sort).each do |key|
  M << key
end
t.times do
  a, b = gets.split.map { |x| x.to_i - 1}
  key = a * m + b
  if A.has_key? key
    puts "Waste"
  else
    u = key - (M.select { |x| x < key }).size
    x = u % 3
    if x == 0
      puts "Carrots"
    elsif x == 1
      puts "Kiwis"
    elsif x == 2
      puts "Grapes"
    end
  end
end

n, m, k, t = gets.split.map(&:to_i)
A = Hash.new(0)
k.times do
  a, b = gets.split.map { |x| x.to_i - 1}
  A[a * m + b] = 1
end
#p A
M = Array.new
(A.keys.sort).each do |key|
  M << key
end
#p M
def search(a, l, h, b)
  #puts "Search l#{l} h#{h} b#{b}"
  mid = l + ((h - l) / 2).to_i

  if l == h
    if b < a[l]
      return (l - 1 < 0) ? 0 : a[l - 1]
    elsif b > a[l]
      return a[l]
    end
  end

  if l - h == 1
    l, h = h, l
  end

  if h - l == 1
    if b < a[l]
      return 0
    elsif b >= a[l] && b < a[h]
      return l + 1
    else
      return h + 1
    end
  elsif b > a[mid]
    return search(a, mid+1, h, b)
  elsif b < a[mid]
    return search(a, l, mid-1, b)
  else
    return 0
  end
end
t.times do
  a, b = gets.split.map { |x| x.to_i - 1}
  key = a * m + b
  if A.has_key? key
    puts "Waste"
  else
#    puts "Key #{key}"
#    bb = search(M, 0, M.size - 1, key)
#    puts "Key = #{key} -> #{bb}"
    u = key - search(M, 0, M.size - 1, key)
    x = u % 3
    if x == 0
      puts "Carrots"
    elsif x == 1
      puts "Kiwis"
    elsif x == 2
      puts "Grapes"
    end
  end
end

n, m, k, t = gets.split.map(&:to_i)
A = Hash.new(0)
k.times do
  a, b = gets.split.map { |x| x.to_i - 1}
  A[a * m + b] = 1
end
#p A
M = Array.new
(A.keys.sort).each do |key|
  M << key
end
#p M
def search(a, l, h, b)
  #puts "Search l#{l} h#{h} b#{b}"
  mid = l + ((h - l) / 2).to_i

  if l > h

    if l >= a.size
      if b < a.last
        return a.size - 1
      else
        return a.size
      end
    end

    if b < a[l]
      return l
    elsif b > a[l]
      return l + 1
    end
  end

  if h - l == 1
    if b < a[l]
      return 0
    elsif b >= a[l] && b < a[h]
      return l + 1
    else
      return h + 1
    end
  elsif b > a[mid]
    return search(a, mid+1, h, b)
  elsif b < a[mid]
    return search(a, l, mid-1, b)
  else
    return 0
  end
end
t.times do
  a, b = gets.split.map { |x| x.to_i - 1}
  key = a * m + b
  if A.has_key? key
    puts "Waste"
  else
#    puts "Key #{key}"
#    bb = search(M, 0, M.size - 1, key)
#    puts "Key = #{key} -> #{bb}"
    u = key - search(M, 0, M.size - 1, key)
    x = u % 3
    if x == 0
      puts "Carrots"
    elsif x == 1
      puts "Kiwis"
    elsif x == 2
      puts "Grapes"
    end
  end
end


n,k,*b=gets(p).split.map(&:to_i)
a=[]
(0...n).reverse_each {|i|
    x=0
    while x<a.size
        break if b[i]<1
        b[i]-=1 if a[x]>i+k
        x+=1
    end
    a.insert x,i+1
}
puts a*" "

n,k,*b=gets(p).split.map(&:to_i)
a=[]
b.each_with_index.reverse_each{|c,i|
	j=a.size
	a.each_with_index{|d,l|
		if c<1
			j=l
			break
		end
		c-=1 if d>i+k
	}
	a.insert j,i+1
}
puts a*" "

n, k, *b = gets(p).split.map(&:to_i)
a = []
(0...n).reverse_each { |i|
    x = 0
    while x < a.size
        break if b[i] < 1
        b[i] -= 1 if a[x] > i + k
        x += 1
    end
    a.insert x, i + 1
}
puts a * " "

n, k, *b = gets(p).split.map(&:to_i)
a = []
for i in (n - 1).downto(0)
    x = a.size
    for j in 0...x
        if b[i] < 1
            x = j
            break
        end
        b[i] -= 1 if a[j] > i + k
    end
    a.insert x, i + 1
end
puts a * " "

def generate(a, k)
  1.upto(a.size).map do |i|
    pos = a.index(i)
    (0..pos-1).count{|j| a[j] >= i+k}
  end
end

def ok?(a, b, k)
  generate(a, k) == b
end

def hoge
  n = 1000
  k = 5
  b = generate((1..n).to_a.shuffle, k)
  puts [n, k]*" "
  puts b*" "
  exit
end

def solve(n, k, b)
  o = ((n-k+1)..n).to_a
  (n-k).downto(1) do |i|
    c = 0
    j = 0
    while c < b[i-1]
      c += 1 if o[j] >= i+k
      j += 1
    end
    o[j,0] = i
  end
  o
end

def test
  n = 9
  k = 4
  h = {}
  (1..n).to_a.permutation do |a|
    b = generate(a, k)
    unless h[b]
      a2 = solve(n, k, b)
      p [a, b, a2] unless a == a2
      h[b] = true
    end
  end

  exit
end

#test

n, k = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
o = solve(n, k, b)
puts o*" "


def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |el|
    ev << el if el.even?
    od << el if el.odd?
  }
  od = od.sort
  ev = ev.sort
  flg = false
  od.each { |el|
    ev.each { |el2|
      if (el - el2).abs == 1
        flg = true
        break
      end
    }
  }
  if od.size.even? && ev.size.even?
    puts "YES"
  elsif flg == true
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times do
  solve
end

gets.to_i.times {
    gets
    a, b=gets.split.map(&:to_i).partition(&:odd?)
    puts ((a.size.even? or a.any?{|x| b.index{|y| (y-x).abs==1}}) ? :YES : :NO)
}

gets.to_i.times {
    gets
    a, b=gets.split.map(&:to_i).partition(&:odd?)
    if a.size.even?
        puts :YES
    else
        puts (a.any?{|x| b.index{|y| (y-x).abs==1}} ? :YES : :NO)
    end
}

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |el|
    ev << el if el.even?
    od << el if el.odd?
  }
  flg = false
  od.each { |el|
    ev.each { |el2|
      if (el - el2).abs == 1
        flg = true
        break
      end
    }
  }
  if od.size.even? && ev.size.even?
    puts "YES"
  elsif flg == true
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times do
  solve
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i).sort
  even = []
  odd = []
  for i in 0...n do
    if a[i] % 2 == 0
      even.push(a[i])
    else
      odd.push(a[i])
    end
  end
  ok = false
  if odd.length % 2 == 1
    odd.each do |v|
      if even.include?(v - 1) || even.include?(v + 1)
        ok = true
        break
      end
    end
  else
    ok = true
  end
  puts ok ? "YES" : "NO"
end

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map(&:to_i).sort
	odd = a.count { _1 % 2 == 1 }
	if odd.odd?
		puts a.each_cons(2).any? { _2 - _1 == 1 } ? 'YES' : 'NO'
	else
		puts 'YES'
	end
end

gets.strip.to_i.times do |t|
  _ = gets.strip.to_i
  a = gets.strip.split(' ').map(&:to_i).sort
  # puts a.group_by { |e| e % 2 }.inspect
  diffs = 0
  a.each_with_index do |e, i|
    next if i.zero?
    diffs += 1 if a[i] - a[i-1] == 1
  end
  even = a.select { |e| e.even? }.size
  odd = a.select { |e| e.odd? }.size
  if even == a.size or odd == a.size
    puts "YES"
  elsif even.even? && odd.even?
    puts "YES"
  elsif diffs > 0
    puts "YES"
  else
    puts "NO"
  end
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = 0
  ev = 0
  mn_ev = 101
  mn_od = 101
  arr.each { |el|
    od += 1 if el.even?
    ev += 1 if el.odd?
    mn_ev = [mn_ev, el].min if el.even?
    mn_od = [mn_od, el].min if el.odd?
  }

  if od.even? && ev.even?
    puts "YES"
  elsif (mn_ev - mn_od).abs == 1
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |vl|
    ev << vl if vl.even?
    od << vl if vl.odd?
  }
  od = od.sort
  ev = ev.sort.reverse
  done = 0
  od.each { |vl|
    if !ev.empty?
      if (ev.last - vl).abs == 1
        ev.pop
        done += 1
        done = od.size if done > od.size
      end
    end
  }
  if ev.size.even? && (od.size - done).even?
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []

  arr.each { |el|
    ev << el if el.even?
    od << el if el.odd?
  }

  od = od.sort
  ev = ev.sort

  flg = false
  od.each_with_index { |el, idx|
    if !ev[idx].nil?
      if (el - ev[idx]).abs == 1
          flg = true
          break
      end
    end
  }

  if od.size.even? && ev.size.even?
    puts "YES"
  elsif flg == true
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |vl|
    ev << vl if vl.even?
    od << vl if vl.odd?
  }
  od = od.sort
  ev = ev.sort.reverse
  done = 0
  od.each { |vl|
    if (od.size - done).even? and ev.size.even?
      puts "YES"
      return nil
    end
    if !ev.empty?
      if (ev.last - vl).abs == 1
        ev.pop
        done += 1
        done = od.size if done > od.size
      end
    end
  }
  puts "NO"
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |vl|
    ev << vl if vl.even?
    od << vl if vl.odd?
  }
  od = od.sort
  ev = ev.sort.reverse
  od.each { |vl|
    if !ev.empty?
      if (ev.last - vl).abs != 1
        puts "NO"
        return nil
      end
      ev.pop
    end
  }
  puts "YES"
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |vl|
    ev << vl if vl.even?
    od << vl if vl.odd?
  }
  od = od.sort
  ev = ev.sort.reverse
  od.each { |vl|
    if !ev.empty?
      if (ev.last - vl).abs == 1
        ev.pop
      end
    end
  }
  puts "YES" if ev.size.even? && od.size.even?
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |vl|
    ev << vl if vl.even?
    od << vl if vl.odd?
  }
  if od.size.even? && ev.size.even?
    puts "YES"
    return nil
  end
  od = od.sort
  ev = ev.sort.reverse
  done = 0
  od.each { |vl|
    if !ev.empty?
      if (ev.last - vl).abs == 1
        ev.pop
        done += 1
        done = od.size if done > od.size
      end
    end
  }
  if ev.size.even? && (od.size - done).even?
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = 0
  ev = 0
  mn_ev = 101
  mn_od = 101
  arr.each { |el|
    od += 1 if el.even?
    ev += 1 if el.odd?
    mn_ev = [mn_ev, el].min if el.even?
    mn_od = [mn_od, el].min if el.odd?
  }
  if od.size.even? && ev.size.even?
    puts "YES"
  elsif (mn_ev - mn_od).abs == 1
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |vl|
    ev << vl if vl.even?
    od << vl if vl.odd?
  }
  od = od.sort
  ev = ev.sort.reverse
  done = 0
  od.each { |vl|
    if (od.size - done).even? && ev.size.even?
      puts "YES"
      return nil
    end
    if !ev.empty?
      if (ev.last - vl).abs == 1
        ev.pop
        done += 1
        done = od.size if done > od.size
      end
    end
  }
  puts "NO"
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  p arr
  od = 0
  ev = 0
  arr.each { |vl|
    ev += 1 if vl.even?
    od += 1 if vl.odd?
  }
  if ev >= od && (ev - od).even?
    puts "YES"
  else
    puts "NO"
  end

end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |vl|
    ev << vl if vl.even?
    od << vl if vl.odd?
  }
  if od.size > ev.size
    puts "NO"
    #return nil
  end
  od = od.sort
  ev = ev.sort.reverse
  od.each { |vl|
    if !ev.empty?
      if (ev.last - vl).abs != 1
        puts "NO"
        return nil
      end
    end
  }
  puts "YES"
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = 0
  ev = 0
  arr.each { |vl|
    ev += 1 if vl.even?
    od += 1 if vl.odd?
  }
  if ev >= od && (ev - od).even?
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |vl|
    ev << vl if vl.even?
    od << vl if vl.odd?
  }
  if od.size > ev.size
    puts "NO"
    return nil
  end
  od = od.sort
  ev = ev.sort.reverse
  od.each { |vl|
    if !ev.empty?
      if (ev.last - vl).abs != 1
        puts "NO"
        return nil
      end
    end
  }
  puts "YES"
end

gets.strip.to_i.times do
  solve
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i).sort
  skip = false
  even = 0
  for i in 0...n do
    if !skip
      if i < n - 1 && a[i + 1] - a[i] == 1
        skip = true
      else
        even += 1 if a[i] % 2 == 0
      end
    else
      skip = false
    end
  end
  puts (even % 2 == 0) ? "YES" : "NO"
end

gets.strip.to_i.times do |t|
  _ = gets.strip.to_i
  a = gets.strip.split(' ').map(&:to_i).sort
  # puts a.group_by { |e| e % 2 }.inspect
  diffs = 0
  a.each_with_index do |e, i|
    next if i.zero?
    diffs += 1 if a[i] - a[i-1] == 1
  end
  even = a.select { |e| e.even? }.size
  odd = a.select { |e| e.odd? }.size
  if even == a.size or odd == a.size
    puts "YES"
  elsif even == odd && even.even?
    puts "YES"
  elsif diffs > 0
    puts "YES"
  else
    puts "NO"
  end
end

gets.to_i.times {
    gets
    a, b=gets.split.map(&:to_i).partition(&:odd?)
    puts (a.size.even? or a.any?{|x| b.index{|y| (y-x).abs==1}} ? :YES : :NO)
}

gets.to_i.times {
    gets
    puts (gets.split.map(&:to_i).sort.each_slice(2).all?{|x,y|y-x==1 or (x+y)%2==0} ? :YES : :NO)
}

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |vl|
    ev << vl if vl.even?
    od << vl if vl.odd?
  }
  od = od.sort
  ev = ev.sort.reverse
  done = 0
  od.each { |vl|
    if !ev.empty?
      if (ev.last - vl).abs == 1
        ev.pop
        done += 1
      end
    end
  }
  if ev.size.even? && (od.size - done).even?
    puts "YES"
  else
    puts "NO"
  end
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |vl|
    ev << vl if vl.even?
    od << vl if vl.odd?
  }
  od = od.sort
  ev = ev.sort.reverse
  done = 0
  od.each { |vl|
    if (od.size - done).even? && ev.size.even?
      puts "YES"
      return nil
    end
    if !ev.empty?
      if (ev.last - vl).abs == 1
        ev.pop
        done += 1
        done = od.size if done > od.size
      end
    end
  }
  if (od.size - done).even? && ev.size.even?
    puts "YES"
    return nil
  end
  puts "NO"
end

gets.strip.to_i.times do
  solve
end

def solve
  _ = gets
  arr = gets.strip.split(' ').map(&:to_i);
  od = []
  ev = []
  arr.each { |vl|
    ev << vl if vl.even?
    od << vl if vl.odd?
  }
  od = od.sort
  ev = ev.sort.reverse
  done = 0
  od.each { |vl|
    if (od.size - done).even? & ev.size.even?
      puts "YES"
      return nil
    end
    if !ev.empty?
      if (ev.last - vl).abs == 1
        ev.pop
        done += 1
        done = od.size if done > od.size
      end
    end
  }
  puts "NO"
end

gets.strip.to_i.times do
  solve
end


def done(a)
    for i in 3..1000
      if (a * i) % 180 == 0 and (a * i)/180  < i - 1
         return i
      end
    end
    return -1
end
for _ in 1..gets.to_i
    a = gets.to_i
    puts done(a)
    end

gets.to_i.times do
    a = gets.to_i
    b = (a*2).gcd(360)
    if 360-a*2 == b
        puts 720/b
    else
        puts 360/b
    end
end

gets.to_i.times do
    a = gets.to_i
    if a < 90
        puts 360/(a*2).gcd(360)
    else
        puts 360/(180-a)
    end
end


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

a = a.sort!
x = 1
y = 0
z = 0
stop = false
for i in 1...n
    if a[i] == a[0]
        x += 1
    else
        y += 1
        for j in i+1...n
            if a[j] == a[i]
                y += 1
            else
                z += 1
                for k in j+1...n
                    if a[k] == a[j]
                        z += 1
                    else
                        stop = true
                        break
                    end
                end
            end
            if stop || x + y + z == n
                break
            end
        end
    end
    if stop || x + y + z == n
        break
    end
end
quantity = 0
if x >= 3
    quantity = x*(x-1)*(x-2)/6
elsif x < 3 && x + y >= 3
    if x == 1
        quantity = y*(y-1)/2
    elsif x == 2
        quantity = y
    end
elsif x + y < 3 && x + y + z >= 3
    if x + y == 1
        quantity = z*(z-1)*(z-2)
    elsif x + y == 2
        quantity = z
    end
end

puts quantity

def ascan; gets.split.map(&:to_i); end
def scan; gets.to_i; end

# combination nCr
def combi(n, r)
    q = 1
    1.upto(r){|k|q=q*(n-k+1)/k}
    return q
end

gets
ha = ascan.inject({}){|h,e| h[e]||=0; h[e]+=1; h}.sort

n = 0
a = []
while n < 3
    v,c = ha.shift
    n += c
    a << c
end
s = n-a[-1]
p 3-s <= a[-1] ? combi(a[-1],3-s) : a[-1]

def ascan; gets.split.map(&:to_i); end
def scan; gets.to_i; end

gets
ha = ascan.inject({}){|h,e| h[e]||=0; h[e]+=1; h}.sort

n = 0
a = []
while n < 3
    v,c = ha.shift
    n += c
    a << c
end

p a[-1]

def ascan; gets.split.map(&:to_i); end
def scan; gets.to_i; end

# combination nCr
def combi(n, r)
    q = 1
    1.upto(r){|k|q=q*(n-k+1)/k}
    return q
end

gets
ha = ascan.inject({}){|h,e| h[e]||=0; h[e]+=1; h}.sort

n = 0
a = []
while n < 3
    v,c = ha.shift
    n += c
    a << c
end

p 3 <= a[-1] ? combi(a[-1],3) : a[-1]


a=gets.to_i.times.map{gets.chomp.chars}
l=a[0].size
b=a.map{|e|
	idx=(0...l).select{|i|e.rotate(i)==a[0]}
	if idx.empty?
		p -1
		exit
	end
	idx
}
p (0...l).map{|i|b.map{|e|e.map{|f|(f+i)%l}.min}.reduce(:+)}.min

n = gets.strip.to_i
data = Array.new(n) { gets.strip }

inf = 10 ** 9
ans = data.map { |s|
    data.map do |ss|
        for i in (0..ss.length)
            break i if ss == s
            ss = ss[1..-1] + ss[0]
            break inf if i+1 == ss.length
        end
    end.reduce(:+)
}.min

puts (ans >= inf ? -1 : ans)

def isEqualStr(word1, word2)
	nStr = word2.length
	for i in 0 ... nStr
		isEqual = true
		j = 0
		while j < nStr and isEqual
			isEqual &= word1[j] == word2[(i + j) % nStr]
			j += 1
		end
		if isEqual
			return i
		end
	end

	return -1
end

n = gets.chomp.to_i
grid = []
for i in 0 ... n
	grid << gets.chomp
end

answer = true
for i in 0 ... n - 1
	if isEqualStr(grid[i], grid[i + 1]) == -1
		answer = false
	end
end

if not answer
	puts "-1"
else
	answer = (1 << 31)
	for i in 0 ... n

		auxAnswer = 0
		j = (i + 1) % n
		while i != j
			auxAnswer += isEqualStr(grid[i], grid[j])
			j = (j + 1) % n
		end
		answer = [answer, auxAnswer].min
	end
	puts answer
end

a=gets.to_i.times.map{gets.chomp.chars}
l=a[0].size
b=a.map{|e|
	idx=(0...l).find{|i|e.rotate(i)==a[0]}
	if !idx
		p -1
		exit
	end
	idx
}
p (0...l).map{|i|b.map{|e|(e+i)%l}.reduce(:+)}.min


def ga
	return gets.split.map &:to_i
end
n,k = ga
a = ga
p [a.min, [a[-1],a[0]].max,a.max][k>2?2:k-1]

class Solver2
  def self.solve(arr, k)
    return arr.min if k == 1
    return arr.max if k > 2
    return [arr.first, arr.last].max
  end
end
string = ARGF.read
arr = string.split("\n")
arr.map! { |elem| elem.strip }
arr1 = arr[0].split
arr2 = arr[1].split
arr1.map! { |elem| elem.split }
arr2.map! { |elem| elem.to_i }
puts Solver2.solve(arr2, arr1[1].first.to_i)
exit(true)

n, k = gets.split.map &:to_i
a = gets.split.map &:to_i
p [a.min, [a[0], a[-1]].max, a.max][[k, 3].min - 1]

def ga
	return gets.split.map &:to_i
end
n,k = ga
a = ga
p [a.min, [a[-1],a[0]].max,a.max][k[0]>2?2:k[0]-1]


n = gets.chomp.to_i
cells = []
n.times do
  cells << gets.chomp
end
ans = 0
for i in 0...n
  r = 0
  c = 0
  for j in 0...n
    r += 1 if cells[i][j] == 'C'
    c += 1 if cells[j][i] == 'C'
  end
  ans += r * (r - 1) / 2
  ans += c * (c - 1) / 2
end

puts ans

#!/usr/bin/ruby
gets;a=$<.map{|e|e.chomp.chars}
r=0
[a,a.transpose].each{|b|b.each{|e|n=e.count('C');r+=n*(n-1)/2}}
p r

nn=gets.to_i
a = (1..nn).map{gets.strip}

def f(n)
  return 0 unless n
  return 0 if n < 2
  n == 2 ? 1 : (n-1)+f(n-1)
end

g1 = a.map{|x| n = x.gsub(/[^C]/, "").size; f(n)}
#p a
a = (0...nn).map{|i| (0...nn).map{|j| a[j][i] }.join }
#p a
g2 = a.map{|x| n = x.gsub(/[^C]/, "").size; f(n)}
#p g1, g2
g = (g1+g2).reduce(:+)
p g

n = gets.chomp.to_i
rows = []
n.times { rows << gets.chomp.chars }
c = 0
rows.each { |row| c += (0..(row.count('C') - 1)).to_a.combination(2).to_a.length }
rows.transpose.each { |row| c += (0..(row.count('C') - 1)).to_a.combination(2).to_a.length }

puts c

n = gets.to_i
cake = []
n.times do
	cake.push(gets.split(''))
end

t = 0
for i in 0...n
	choc = 0
	for j in 0...n
		choc += 1 if cake[i][j] == 'C'
	end
	t += choc*(choc-1)/2
end

for i in 0...n
	choc = 0
	for j in 0...n
		choc += 1 if cake[j][i] == 'C'
	end
	t += choc*(choc-1)/2
end

puts t

a=gets.chomp.to_i
row=[]
clm=[]
a.times do
clm <<0
end
a.times do
temp=gets.chomp.split("")
t=0
0.upto(a-1) do |i|
if temp[i]=="C"
t+=1
clm[i]+=1
end
end
row << t
end
ans=0
row.each do |i|
if i>1
ans=ans+(i*(i-1))/2
end
end

clm.each do |i|
if i>1
ans=ans+(i*(i-1))/2
end
end
puts "#{ans}"

def combination_count(n, m)
  return 1 if n == m
  (1..n).inject(:*)/((1..m).inject(:*) * (1..n-m).inject(:*))
end

n = gets.to_i
cake = []
n.times do
  cake << gets.chomp.chars
end

cake_t = cake.transpose

ans = 0
(0..n-1).each do |i|
  if (c = cake[i].count('C')) >= 2
    ans += combination_count(c, 2)
  end
  if (c = cake_t[i].count('C')) >= 2
    ans += combination_count(c, 2)
  end
end

puts ans

class Integer
  def combination(k)
    if self < k
      return 0
    end
    self.factorial/(k.factorial*(self-k).factorial)
  end

  def permutation(k)
    self.factorial/(self-k).factorial
  end

  def factorial
    return 1 if self == 0
    (1..self).inject(:*)
  end
end

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

n.times do
  a.push(gets.chomp.split(''))
end
r = Array.new(n, 0)
c = Array.new(n, 0)

n.times do |i|
  n.times do |j|
    if a[i][j] == 'C'
      r[j] += 1
    end
    if a[j][i] == 'C'
      c[j] += 1
    end
  end
end
ans = 0
n.times do |i|
  ans += r[i].combination(2) + c[i].combination(2)
end
puts ans

n = gets.to_i
g = []
for i in 0...n
  r = gets.split('')
  g << r
end

def bin(n, k)
  if n < k
    0
  elsif k == 0 || n == k
    1
  else
    bin(n-1, k-1) + bin(n-1, k)
  end
end

ct = 0
for i in 0...n
  col = 0
  row = 0
  for j in 0...n
    row += g[i][j] == 'C' ? 1 : 0
    col += g[j][i] == 'C' ? 1 : 0
  end

  ct += bin(row, 2) + bin(col, 2)
end

puts ct

n = gets.to_i
a = n.times.map{gets.chomp.split('')}
sum = 0
def calc(arr, sum)
  arr.each do |x|
    c = x.count('C')
    sum += c * (c - 1) / 2
  end
  sum
end
r = calc(a, sum)
c = calc(a.transpose, r)
puts c

n = gets.to_i
a = []
n.times { a << gets.chomp.split('') }
b = a.transpose
joy = 0
(0...n).each do |i|
  c = a[i].count('C') - 1
  joy += (1 + c)*c/2
  c = b[i].count('C') - 1
  joy += (1 + c)*c/2
end
puts joy

def combination_count(n, m)
  (1..n).inject(:*)/(1..m).inject(:*)
end

n = gets.to_i
cake = []
n.times do
  cake << gets.chomp.chars
end

cake_t = cake.transpose

ans = 0
(0..n-1).each do |i|
  if (c = cake[i].count('C')) > 0
    ans += combination_count(c, 2)
  end
  if (c = cake_t[i].count('C')) > 0
    ans += combination_count(c, 2)
  end
end

puts ans


n = gets.to_i
a = gets.split.map &:to_i
if a.any?{|i|i % a[0] > 0} then p -1 else puts 2 * n - 1, a * (" %d " % a[0]) end


T = gets.to_i

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

  if au.size == 1
    puts 'NO'
  else
    v1 = a.index(au[0])
    v2 = a.index(au[1])
    edges = []
    edges << [v1, v2]

    a.each.with_index do |x, i|
      next if i == v1
      next if i == v2

      if x == a[v1]
        edges << [v2, i]
      else
        edges << [v1, i]
      end
    end

    puts 'YES'
    edges.each do |u, v|
      puts "#{u + 1} #{v + 1}"
    end
  end
end

def solve(n, a)
  result = []
  first = a.first
  second = -1
  a.each_with_index do |element, i|
    second = 1+i if element!=first
  end
  if second!=-1
    1.upto(n-1) do |i|
      if(a[i]!=first)
        result.push([1,i+1])
      else
        result.push([second, i+1])
      end
    end
  end
  result
end

(gets.to_i).times do
  n = gets.to_i
  a = gets.split(' ').map(&:to_i)
  b = solve(n,a)
  if(b.length == 0)
    puts "NO"
  else
    puts "YES"
    b.each do |i|
      puts "#{i[0]} #{i[1]}"
    end
  end
end

gets.to_i.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  next puts 'NO' if a.count(a[0]) == n
  puts 'YES'
  i = 0
  j = a.index { |x| x != a[i] }
  puts "#{i + 1} #{j + 1}"
  n.times do |k|
    if i != k and j != k
      if a[i] == a[k]
        puts "#{j + 1} #{k + 1}"
      else
        puts "#{i + 1} #{k + 1}"
       end
    end
  end
end

gets.to_i.times do
  n = gets.to_i
  v = gets.split.map(&:to_i)
  p = (0...n).sort { |i, j| v[i] <=> v[j] }
  w = p.chunk { |i| v[i] }.map(&:last)
  if w.length == 1
    puts 'NO'
  else
    puts 'YES'
    w.drop(1).flatten.each do |x|
      puts "#{p[0] + 1} #{x + 1}"
    end
    w[0].drop(1).each do |x|
      puts "#{w[1][0] + 1} #{x + 1}"
    end
  end
end

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  a = gets.chomp.split(" ").map(&:to_i)
  hash = Hash.new(0)
  max = 0
  a.each do |v|
    hash[v] += 1
    if max < hash[v]
      max = hash[v]
    end
  end
  if max > n-1
    ans << "NO"
  else
    ans << "YES"
    one = 1
    two = 0
    for i in 1..n-1
      next if a[i] == a[0]
      two = i+1
      break
    end
    ans << "#{one} #{two}"
    for i in 1..n-1
      next if i+1 == two
      if a[i] != a[one-1]
        ans << "#{one} #{i+1}"
      else
        ans << "#{two} #{i+1}"
      end
    end
  end
end

puts ans

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  a = gets.chomp.split(" ").map(&:to_i)
  hash = Hash.new(0)
  max = 0
  city = 0
  a.each do |v|
    hash[v] += 1
    if max < hash[v]
      max = hash[v]
      city = v
    end
  end
  if max > n/2
    ans << "NO"
  else
    ans << "YES"
    pre = 0
    dist = Array.new(n+1,0)
    arr = Array.new(n,0)
    e = []
    n.times do |i|
      if a[i] == city
        e << i+1
        dist[i+1] = 1
      end
    end
    max.times do |i|
      arr[i*2] = e[i]
    end
    pre = 0
    for i in (max*2-1)..n-1
      for j in 0..n-1
        if a[j] != pre && dist[j+1] == 0
          arr[i] = j+1
          dist[j+1] = 1
          pre = a[j]
          break
        end
      end
    end
    (max-1).times do |i|
      for j in 0..n-1
        if dist[j+1] == 0
          arr[i*2+1] = j+1
          dist[j+1] = 1
          break
        end
      end
    end
    (n-1).times do |i|
      ans << "#{arr[i]} #{arr[i+1]}"
    end
  end
end

puts ans

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  a = gets.chomp.split(" ").map(&:to_i)
  hash = Hash.new(0)
  max = 0
  a.each do |v|
    hash[v] += 1
    if max < hash[v]
      max = hash[v]
    end
  end
  if max > (n+1)/2
    ans << "NO"
  else
    ans << "YES"
    one = 1
    two = 0
    for i in 1..n-1
      next if a[i] == a[0]
      two = i+1
      break
    end
    ans << "#{one} #{two}"
    for i in 1..n-1
      next if i+1 == two
      if a[i] != one
        ans << "#{one} #{i+1}"
      else
        ans << "#{two} #{i+1}"
      end
    end
  end
end

puts ans

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  a = gets.chomp.split(" ").map(&:to_i)
  hash = Hash.new(0)
  max = 0
  a.each do |v|
    hash[v] += 1
    if max < hash[v]
      max = hash[v]
    end
  end
  if max > n-1
    ans << "NO"
  else
    ans << "YES"
    one = 1
    two = 0
    for i in 1..n-1
      next if a[i] == a[0]
      two = i+1
      break
    end
    ans << "#{one} #{two}"
    for i in 1..n-1
      next if i+1 == two
      if a[i] != one
        ans << "#{one} #{i+1}"
      else
        ans << "#{two} #{i+1}"
      end
    end
  end
end

puts ans

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  a = gets.chomp.split(" ").map(&:to_i)
  hash = Hash.new(0)
  max = 0
  a.each do |v|
    hash[v] += 1
    if max < hash[v]
      max = hash[v]
    end
  end
  if max > n-1
    ans << "NO"
  else
    ans << "YES"
    one = 1
    two = 0
    for i in 1..n-1
      next if a[i] == a[0]
      two = i+1
      break
    end
    ans << "#{one} #{two}"
    for i in 1..n-1
      next if i+1 == two
      if a[i+1] != one
        ans << "#{one} #{i+1}"
      else
        ans << "#{two} #{i+1}"
      end
    end
  end
end

puts ans

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  a = gets.chomp.split(" ").map(&:to_i)
  hash = Hash.new(0)
  max = 0
  city = 0
  a.each do |v|
    hash[v] += 1
    if max < hash[v]
      max = hash[v]
      city = v
    end
  end
  if max > (n+1)/2
    ans << "NO"
  else
    ans << "YES"
    pre = 0
    dist = Array.new(n+1,0)
    arr = Array.new(n,0)
    e = []
    n.times do |i|
      if a[i] == city
        e << i+1
        dist[i+1] = 1
      end
    end
    max.times do |i|
      arr[i*2] = e[i]
    end
    pre = 0
    for i in (max*2-1)..n-1
      for j in 0..n-1
        if a[j] != pre && dist[j+1] == 0
          arr[i] = j+1
          dist[j+1] = 1
          pre = a[j]
          break
        end
      end
    end
    (max-1).times do |i|
      for j in 0..n-1
        if dist[j+1] == 0
          arr[i*2+1] = j+1
          dist[j+1] = 1
          break
        end
      end
    end
    (n-1).times do |i|
      ans << "#{arr[i]} #{arr[i+1]}"
    end
  end
end

puts ans


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

l.sort!
max = l.pop
sum = l.reduce(:+)

puts [max - sum + 1, 0].max

n = readline.to_i
arr = readline.split(' ').collect(&:to_i).sort.reverse
puts 1 + arr[0] - arr[1..-1].inject(0, :+)

def solve(a)
  2*a.max - a.inject(:+) + 1
end

gets

puts solve(gets.split.map(&:to_i))

lodNum = gets.chomp.to_i
input = gets.chomp.split(' ').map {|i| i.to_i}
input.sort!

sum = 0
flag = false
max = input[input.length - 1]
for i in (0...(input.length - 1))
	sum += input[i]
end

if !flag && sum > max
	puts '0'
else
	puts (max - sum + 1)
end

n = gets

parse_int = lambda { gets.split.map{|x| x.to_i} }

m = parse_int.call

m.sort!

last = m.pop
sum = 0
m.each { |a| sum+=a }
puts last-sum+1


# T = gets.chomp.to_i
# (0..T-1).map do |i|
#     N = gets.chomp.to_i
#     pN = (N/2).ceil
#     b = gets.chomp
#     max_bs = []
#     (0..(b.length - pN + 1)).map do |j|
#         sub_b = b[j..(j + pN - 1)]
#         sum_b = sub_b.split('').inject 0 do |s, e|
#             s += e.to_i
#         end
#         max_bs << sum_b
#     end
#     p "Case ##{i + 1}: #{max_bs.max}"
# end

n, k = gets.chomp.split " "
n = n.to_i
k = k.to_i
p (k.to_r / n).ceil

def solution
  n, k = read_ints
  s = k/n
  s += 1 if s*n < k
  puts s
end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

N, K = gets.split.map(&:to_i)
puts (K-1)/N+1

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

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

ans = k / n
if k % n != 0
    ans += 1
end

puts ans

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

n, k = gets.split.map &:to_i
print (k+n-1)/n

n, k = gets.chomp.split.map(&:to_i)
puts (n.to_f/k).ceil

def solution
  n, k = read_ints
  s = k/n
  s += 1 if s*k < n
  puts s
end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

n, k = gets.chomp.split.map(&:to_i)
puts (k.to_f/n).ceil

# T = gets.chomp.to_i
# (0..T-1).map do |i|
#     N = gets.chomp.to_i
#     pN = (N/2).ceil
#     b = gets.chomp
#     max_bs = []
#     (0..(b.length - pN + 1)).map do |j|
#         sub_b = b[j..(j + pN - 1)]
#         sum_b = sub_b.split('').inject 0 do |s, e|
#             s += e.to_i
#         end
#         max_bs << sum_b
#     end
#     p "Case ##{i + 1}: #{max_bs.max}"
# end

n, k = gets.chomp.split " "
n = n.to_i
k = k.to_i
p (k.to_f / n).ceil


$_ = gets.chomp + ' '
[%w(lios etr initis), %w(liala etra inites)].each do |r|
	r.map!{|_| "\\S*#{_}\\s+"}
	r << "(#{r[0]})*(#{r[1]})(#{r[2]})*";
	if /^(#{r.join('|')})$/
		puts :YES
		exit
	end
end
puts :NO

def get_info(s)
  case s
  when /lios$/
    return 1, 0
  when /liala$/
    return 1, 1
  when /etr$/
    return 2, 0
  when /etra$/
    return 2, 1
  when /initis$/
    return 3, 0
  when /inites$/
    return 3, 1
  else
    return -1, -1
  end
end

def verify(words)
  if words.size == 0
    return false
  end
  if words.size == 1
    return words[0][0] >= 0
  end
  last_type = -1
  count_noun = 0
  words.each { |word|
    # Must be valid word
    if word[0] < 0
      return false
    end
    # Must have same gender
    if word[1] != words[0][1]
      return false
    end
    # Must be increasing in type
    if word[0] == 2
      count_noun += 1
    end
    if word[0] < last_type
      return false
    end
    last_type = word[0]
  }
  return count_noun == 1
end

[*$<].each { |line|
  words = line.split
  if verify(words.map { |s| get_info(s) })
    puts 'YES'
  else
    puts 'NO'
  end
}

words = gets.split

if words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/
  puts "YES"
elsif words.size == 1
  puts "NO"
else
  first = words.first
  if first =~ /(lios|etr)$/
    gender = :male
  elsif first =~ /(liala|etra)$/
    gender = :female
  else
    puts "NO"
    Process.exit
  end

  for i in 1...words.size
    if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)
      puts "NO"
      Process.exit
    end
  end

  pos = 1
  type = (first =~ /(etr|etra)$/) ? :noun : :adj

  if type == :adj
    pos += 1 while words[pos] =~ /(lios|liala)$/
    unless words[pos] =~ /(etr|etra)$/
      puts "NO"
      Process.exit
    end
    pos += 1
  end

  for i in pos...words.size
    unless (gender == :male and words[i] =~ /initis$/) or (gender == :female and words[i] =~ /inites$/)
      puts "NO"
      Process.exit
    end
  end

  puts "YES"
end

s = gets.strip
[%w(lios etr initis), %w(liala etra inites)].each do |g|
  r = g.map { |t| "\\S*#{t}(\\s+|$)" }
  r << "(#{r[0]})*#{r[1]}(#{r[2]})*"
  if /^(#{r.join ?|})$/.match s
    puts :YES
    exit
  end
end
puts :NO

def solve(ws)
  ts = ws.map do |w|
    case
    when w.end_with?('lios') then [:adj, :mas]
    when w.end_with?('liala') then [:adj, :fem]
    when w.end_with?('etr') then [:noun, :mas]
    when w.end_with?('etra') then [:noun, :fem]
    when w.end_with?('initis') then [:verb, :mas]
    when w.end_with?('inites') then [:verb, :fem]
    when true then nil
    end
  end
  return false if ts.any?(&:nil?)
  return true if ts.size == 1
  return false unless ts.all?{|x| x[1] == ts[0][1]}
  cb = :adj
  ts.each do |t, _|
    case cb
    when :adj
      case t
      when :noun then cb=:noun
      when :verb then return false
      end
    when :noun,:verb
      case t
      when :adj, :noun then return false
      when :verb then cb=:verb
      end
    end
  end
  return cb == :noun || cb == :verb
end

words = gets.split
puts solve(words) ? 'YES' : 'NO'

class String
  def end_with(s)
    i = self.rindex s
    !i.nil? && self.length - i == s.length
  end
end

$w = [
   %w(lios ma),
   %w(liala fa),
   %w(etr mn),
   %w(etra fn),
   %w(initis mv),
   %w(inites fv),
  ]
def f(str)
  $w.each {|suf, s|
    return s if str.end_with suf
  }
  nil
end

def valid(words)
  return false if words.index nil
  return true if words.length == 1

  return false if words.map{|w|w[0]}.uniq.length > 1
  a = words.map{ |w| w[1] }
  i = 0
  i += 1 while i<a.length && a[i] == 'a'
  return false if i==a.length || a[i] != 'n'
  i += 1
  i += 1 while i<a.length && a[i] == 'v'
  i == a.length
end
words = gets.chomp.split.map{ |s| f s }
puts (valid(words) ? 'YES': 'NO')

w = gets.split
bool = false
if w.size == 1
  w = w[0]
  if w[-3..-1] == 'etr' || w[-4..-1] == 'lios' || w[-4..-1] == 'etra' || w[-5..-1] == 'liala' || w[-6..-1] == 'initis' || w[-6..-1] == 'inites'
    bool = true
  end
else
  if w[0][-4..-1] == 'lios' || w[0][-3..-1] == 'etr'
    while w[0][-4..-1] == 'lios'
      w.shift
      break if w.empty?
    end
    if !w.empty?
      if w[0][-3..-1] == 'etr'
        w.shift
        if !w.empty?
          while w[0][-6..-1] == 'initis'
            w.shift
            break if w.empty?
          end
        end
        bool = true if w.size == 0
      end
    end
  elsif w[0][-5..-1] == 'liala' || w[0][-4..-1] == 'etra'
    while w[0][-5..-1] == 'liala'
      w.shift
      break if w.empty?
    end
    if !w.empty?
      if w[0][-4..-1] == 'etra'
        w.shift
        if !w.empty?
          while w[0][-6..-1] == 'inites'
            w.shift
            break if w.empty?
          end
        end
        bool = true if w.size == 0
      end
    end
  end
end
if bool
  puts "YES"
else
  puts "NO"
end

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

class GrammarError < StandardError; end

class String
  def gender
    x, _ = categorize
    x
  end

  def speech
    _, x = categorize
    x
  end

  def categorize
    if end_with? 'lios'
      [:masculine, :adj]
    elsif end_with? 'liala'
      [:feminine, :adj]
    elsif end_with? 'etr'
      [:masculine, :noun]
    elsif end_with? 'etra'
      [:feminine, :noun]
    elsif end_with? 'initis'
      [:masculine, :verb]
    elsif end_with? 'inites'
      [:feminine, :verb]
    else
      raise GrammarError
    end
  end

  def correct_sentence?
    cs = split(/\s+/).map(&:categorize)
    gs = cs.map { |c| c[0] }
    if gs.find { |g| g != gs[0] }
      return false
    end

    if cs.size == 1
      return true
    end
    ss = cs.map { |c| c[1] }
    sss = ss.drop_while { |c| c == :adj }
    if sss[0] != :noun
      return false
    end
    return sss.drop(1).all? { |c| c == :verb }
  rescue
    false
  end
end

puts (gets.correct_sentence? ? 'YES' : 'NO')

def get_word(w)
  ret = Hash.new
  ret[:gender] = nil
  ret[:type] = nil
  ret[:valid] =  false
  if w.end_with?("lios") then
    ret[:gender] = :m
    ret[:type] = :adjective
    ret[:valid] = true
  elsif w.end_with?("liala") then
    ret[:gender] = :f
    ret[:type] = :adjective
    ret[:valid] = true
  elsif w.end_with?("etr") then
    ret[:gender] = :m
    ret[:type] = :noun
    ret[:valid] = true
  elsif w.end_with?("etra") then
    ret[:gender] = :f
    ret[:type] = :noun
    ret[:valid] = true
  elsif w.end_with?("initis") then
    ret[:gender] = :m
    ret[:type] = :verb
    ret[:valid] = true
  elsif w.end_with?("inites") then
    ret[:gender] = :f
    ret[:type] = :verb
    ret[:valid] = true
  end

  ret
end

str = $stdin.readline.split(' ')

words = Array.new
str.each do |w|
  words.push get_word(w)
end

gender = words[0][:gender]
words.each do |w|
  if not w[:gender] == gender then
    puts "NO"
    exit
  end

  if not w[:valid] then
    puts "NO"
    exit
  end
end

if words.count == 1 then
  puts "YES"
  exit
end

i = 0
while words[i][:type] == :adjective
  i += 1
  if i >= words.count then
    puts "NO"
    exit
  end
end

unless words[i][:type] == :noun
  puts "NO"
  exit
end
i += 1
if i >= words.count then
  puts "YES"
  exit
end

while i < words.count
  if words[i][:type] != :verb then
    puts "NO"
    exit
  end
  i += 1
end

puts "YES"

def g(s)
  case s
  when /lios$/
    return 1, 0
  when /liala$/
    return 1, 1
  when /etr$/
    return 2, 0
  when /etra$/
    return 2, 1
  when /initis$/
    return 3, 0
  when /inites$/
    return 3, 1
  else
    return -1, -1
  end
end

def v(words)
  if words.size == 0
    return false
  end
  if words.size == 1
    return words[0][0] >= 0
  end
  last_type = -1
  c = 0
  words.each { |w|
    return false if w[0] < 0
    return false if w[1] != words[0][1]
    c += 1 if w[0] == 2
    return false if w[0] < last_type
    last_type = w[0]
  }
  return c == 1
end

words = gets.split
if v(words.map { |s| g(s) })
  puts 'YES'
else
  puts 'NO'
end

def g(s)
  case s
  when /lios$/
    return 1, 0
  when /liala$/
    return 1, 1
  when /etr$/
    return 2, 0
  when /etra$/
    return 2, 1
  when /initis$/
    return 3, 0
  when /inites$/
    return 3, 1
  else
    return -1, -1
  end
end

def v(words)
  return false if words.size==0
  return words[0][0] >= 0 if words.size==1
  l=-1
  c=0
  words.each { |w|
    return false if w[0]<0
    return false if w[1]!=words[0][1]
    c+=1 if w[0]==2
    return false if w[0]<l
    l=w[0]
  }
  return c == 1
end

words = gets.split
if v(words.map {|s| g(s) })
  puts 'YES'
else
  puts 'NO'
end

words = gets.split

if words.size == 1 and words.first =~ /(etr|etra)$/
  puts "YES"
elsif words.size == 1
  puts "NO"
else
  first = words.first
  if first =~ /(lios|etr)$/
    gender = :male
  elsif first =~ /(liala|etra)$/
    gender = :female
  else
    puts "NO"
    Process.exit
  end

  for i in 1..words.size
    if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)
      puts "NO"
      Process.exit
    end
  end

  pos = 1
  type = (first =~ /(etr|etra)$/) ? :noun : :adj

  if type == :adj
    pos += 1 while words[pos] =~ /(lios|liala)$/
    unless words[pos] =~ /(etr|etra)$/
      puts "NO"
      Process.exit
    end
    i += 1
  end

  for i in pos..words.size
    if (gender == :male and words[i] =~ /inites$/) or (gender == :female and words[i] =~ /initis$/)
      puts "NO"
      Process.exit
    end
  end

  puts "YES"
end

words = gets.split

if words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/
  puts "YES"
elsif words.size == 1
  puts "NO"
else
  first = words.first
  if first =~ /(lios|etr)$/
    gender = :male
  elsif first =~ /(liala|etra)$/
    gender = :female
  else
    puts "NO"
    Process.exit
  end

  for i in 1..words.size
    if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)
      puts "NO"
      Process.exit
    end
  end

  pos = 1
  type = (first =~ /(etr|etra)$/) ? :noun : :adj

  if type == :adj
    pos += 1 while words[pos] =~ /(lios|liala)$/
    unless words[pos] =~ /(etr|etra)$/
      puts "NO"
      Process.exit
    end
    i += 1
  end

  for i in pos..words.size
    unless words[i] =~ /(initis|inites)$/
      puts "NO"
      Process.exit
    end
  end

  puts "YES"
end

words = gets.split

if words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/
  puts "YES"
elsif words.size == 1
  puts "NO"
else
  first = words.first
  if first =~ /(lios|etr)$/
    gender = :male
  elsif first =~ /(liala|etra)$/
    gender = :female
  else
    puts "NO"
    Process.exit
  end

  for i in 1..words.size
    if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)
      puts "NO"
      Process.exit
    end
  end

  pos = 1
  type = (first =~ /(etr|etra)$/) ? :noun : :adj

  if type == :adj
    pos += 1 while words[pos] =~ /(lios|liala)$/
    unless words[pos] =~ /(etr|etra)$/
      puts "NO"
      Process.exit
    end
    pos += 1
  end

  for i in pos..words.size
    if (gender == :male and words[i] =~ /inites$/) or (gender == :female and words[i] =~ /initis$/)
      puts "NO"
      Process.exit
    end
  end

  puts "YES"
end

words = gets.split

if words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/
  puts "YES"
elsif words.size == 1
  puts "NO"
else
  first = words.first
  if first =~ /(lios|etr)$/
    gender = :male
  elsif first =~ /(liala|etra)$/
    gender = :female
  else
    puts "NO"
    Process.exit
  end

  for i in 1..words.size
    if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)
      puts "NO"
      Process.exit
    end
  end

  pos = 1
  type = (first =~ /(etr|etra)$/) ? :noun : :adj

  if type == :adj
    pos += 1 while words[pos] =~ /(lios|liala)$/
    unless words[pos] =~ /(etr|etra)$/
      puts "NO"
      Process.exit
    end
    i += 1
  end

  for i in pos..words.size
    if (gender == :male and words[i] =~ /inites$/) or (gender == :female and words[i] =~ /initis$/)
      puts "NO"
      Process.exit
    end
  end

  puts "YES"
end

$_ = gets.chomp + ' '
[%w(lios etr initis), %w(liala etra inites)].each do |r|
	r.map!{|_| "\\S*#{_}\\s+"}
	r << "(#{r[0]})*(#{r[1]})(#{r[2]})*";
	if /^(#{r.join('|')})$/
		puts $&
		puts :YES
		exit
	end
end
puts :NO

$_ = gets.chomp + ' '
[%w(lios etr initis), %w(liala etra inites)].each do |r|
	r.map!{|_| ".*#{_}\s+"}
	r << "(#{r[0]})*(#{r[1]})(#{r[2]})*";
	if /^#{r.join('|')}$/
		puts :YES
		exit
	end
end
puts :NO

s = gets.strip
[%w(lios etr initis), %w(liala etra inites)].each do |g|
  r = g.map { |t| "\\S*#{t}(\\s+|$)" }
  r << "(#{r[0]})*#{r[1]}(#{r[2]})*"
  if /^#{r.join ?|}$/.match s
    puts :YES
    exit
  end
end
puts :NO

def solve(ws)
  ts = ws.map do |w|
    return [:adj, :mas] if w.end_with?('lios')
    return [:adj, :fem] if w.end_with?('liala')
    return [:noun, :mas] if w.end_with?('etr')
    return [:noun, :fem] if w.end_with?('etra')
    return [:verb, :mas] if w.end_with?('initis')
    return [:verb, :fem] if w.end_with?('inites')
    return nil
  end
  return false if ts.any?(&:nil)
  return true if ts.size == 1
  return false unless ts.all?{|x| x[1] == ts[0][1]}
  cb = :adj
  ts.each do |t, _|
    case cb
    when :adj
      case t
      when :noun then cb=:noun
      when :verb then return false
      end
    when :noun,:verb
      case t
      when :adj, :noun then return false
      when :verb then cb=:verb
      end
    end
  end
  return cb == :noun || cb == :verb
end

words = gets.split
puts solve(words) ? 'YES' : 'NO'

w = gets.split
bool = false
if w.size == 1
  w = w[0]
  if w[-3..-1] == 'etr' || w[-4..-1] == 'lios' || w[-4..-1] == 'etra' || w[-5..-1] == 'liala' || w[-6..-1] == 'initis' || w[-6..-1] == 'inites'
    bool = true
  end
else
  if w[0][-4..-1] == 'lios' || w[0][-3..-1] == 'etr'
    while w[0][-4..-1] == 'lios'
      w.shift
      break if w.empty?
    end
    if !w.empty?
    w.shift if w[0][-3..-1] == 'etr'
    if !w.empty?
    while w[0][-6..-1] == 'initis'
      w.shift
      break if w.empty?
    end
    end
    bool = true if w.size == 0
    end
  elsif w[0][-5..-1] == 'liala' || w[0][-4..-1] == 'etra'
    while w[0][-5..-1] == 'liala'
      w.shift
      break if !w.empty?
    end
    if !w.empty?
    w.shift if w[0][-4..-1] == 'etra'
    if !w.empty?
    while w[0][-6..-1] == 'inites'
      w.shift
      break if w.empty?
    end
    bool = true if w.size == 0
    end
    end
  end
end
if bool
  puts "YES"
else
  puts "NO"
end

def get_info(s)
  case s
  when /lios$/
    return 1, 0
  when /liala$/
    return 1, 1
  when /etr$/
    return 2, 0
  when /etra$/
    return 2, 1
  when /initis$/
    return 3, 0
  when /inites$/
    return 3, 1
  else
    return -1, -1
  end
end

def verify(words)
  if words.size == 0
    return false
  end
  last_type = -1
  count_noun = 0
  words.each { |word|
    # Must be valid word
    if word[0] < 0
      return false
    end
    # Must have same gender
    if word[1] != words[0][1]
      return false
    end
    # Must be increasing in type
    if word[0] == 2
      count_noun += 1
    end
    if word[0] < last_type
      return false
    end
    last_type = word[0]
  }
  return count_noun == 1
end

[*$<].each { |line|
  words = line.split
  if verify(words.map { |s| get_info(s) })
    puts 'YES'
  else
    puts 'NO'
  end
}

def get_info(s)
  case s
  when /lios$/
    return 1, 0
  when /liala$/
    return 1, 1
  when /etr$/
    return 2, 0
  when /etra$/
    return 2, 1
  when /initis$/
    return 3, 0
  when /inites$/
    return 3, 1
  else
    return -1, -1
  end
end

def verify(words)
  if words.size == 0
    return false
  end
  if words.size == 1
    return true
  end
  last_type = -1
  count_noun = 0
  words.each { |word|
    # Must be valid word
    if word[0] < 0
      return false
    end
    # Must have same gender
    if word[1] != words[0][1]
      return false
    end
    # Must be increasing in type
    if word[0] == 2
      count_noun += 1
    end
    if word[0] < last_type
      return false
    end
    last_type = word[0]
  }
  return count_noun == 1
end

[*$<].each { |line|
  words = line.split
  if verify(words.map { |s| get_info(s) })
    puts 'YES'
  else
    puts 'NO'
  end
}

words = gets.split

if words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/
  puts "YES"
elsif words.size == 1
  puts "NO"
else
  first = words.first
  if first =~ /(lios|etr)$/
    gender = :male
  elsif first =~ /(liala|etra)$/
    gender = :female
  else
    puts "NO"
    Process.exit
  end

  for i in 1..words.size
    if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)
      puts "NO"
      Process.exit
    end
  end

  pos = 1
  type = (first =~ /(etr|etra)$/) ? :noun : :adj

  if type == :adj
    pos += 1 while words[pos] =~ /(lios|liala)$/
    unless words[pos] =~ /(etr|etra)$/
      puts "NO"
      Process.exit
    end
    pos += 1
  end

  for i in pos..words.size
    unless words[i] =~ /(initis|inites)$/
      puts "NO"
      Process.exit
    end
  end

  puts "YES"
end

words = gets.split

if words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/
  puts "YES"
else
  first = words.first
  if first =~ /(lios|etr)$/
    gender = :male
  elsif first =~ /(liala|etra)$/
    gender = :female
  else
    puts "NO"
    Process.exit
  end

  for i in 1..words.size
    if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)
      puts "NO"
      Process.exit
    end
  end

  pos = 1
  type = (first =~ /(etr|etra)$/) ? :noun : :adj

  if type == :adj
    pos += 1 while words[pos] =~ /(lios|liala)$/
    unless words[pos] =~ /(etr|etra)$/
      puts "NO"
      Process.exit
    end
    i += 1
  end

  for i in pos..words.size
    if (gender == :male and words[i] =~ /inites$/) or (gender == :female and words[i] =~ /initis$/)
      puts "NO"
      Process.exit
    end
  end

  puts "YES"
end

words = gets.split

if words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/
  puts "YES"
elsif words.size == 1
  puts "NO"
else
  first = words.first
  if first =~ /(lios|etr)$/
    gender = :male
  elsif first =~ /(liala|etra)$/
    gender = :female
  else
    puts "NO"
    Process.exit
  end

  for i in 1...words.size
    if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)
      puts "NO"
      Process.exit
    end
  end

  pos = 1
  type = (first =~ /(etr|etra)$/) ? :noun : :adj

  if type == :adj
    pos += 1 while words[pos] =~ /(lios|liala)$/
    unless words[pos] =~ /(etr|etra)$/
      puts "NO"
      Process.exit
    end
    pos += 1
  end

  for i in pos...words.size
    if words[i] =~ /(initis|inites)$/
      puts "NO"
      Process.exit
    end
  end

  puts "YES"
end

words = gets.split

if words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/
  puts "YES"
elsif words.size == 1
  puts "NO"
else
  first = words.first
  if first =~ /(lios|etr)$/
    gender = :male
  elsif first =~ /(liala|etra)$/
    gender = :female
  else
    puts "NO"
    Process.exit
  end

  for i in 1..words.size
    if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)
      puts "NO"
      Process.exit
    end
  end

  pos = 1
  type = (first =~ /(etr|etra)$/) ? :noun : :adj

  if type == :adj
    pos += 1 while words[pos] =~ /(lios|liala)$/
    unless words[pos] =~ /(etr|etra)$/
      puts "NO"
      Process.exit
    end
    i += 1
  end

  for i in pos..words.size
    if words[i] =~ /(initis|inites)$/
      puts "NO"
      Process.exit
    end
  end

  puts "YES"
end

words = gets.split

if words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/
  puts "YES"
elsif words.size == 1
  puts "NO"
else
  first = words.first
  if first =~ /(lios|etr)$/
    gender = :male
  elsif first =~ /(liala|etra)$/
    gender = :female
  else
    puts "NO"
    Process.exit
  end

  for i in 1..words.size
    if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)
      puts "NO"
      Process.exit
    end
  end

  pos = 1
  type = (first =~ /(etr|etra)$/) ? :noun : :adj

  if type == :adj
    pos += 1 while words[pos] =~ /(lios|liala)$/
    unless words[pos] =~ /(etr|etra)$/
      puts "NO"
      Process.exit
    end
    pos += 1
  end

  for i in pos..words.size
    unless (gender == :male and words[i] =~ /initis$/) or (gender == :female and words[i] =~ /inites$/)
      puts "NO"
      Process.exit
    end
  end

  puts "YES"
end


#1. Accomodate Inputs & Initialize
n = gets(" ").to_i
m = gets.chomp.to_i

relations = Array.new(n+1){Array.new()}
(1..m).each do |i|
  a = gets(" ").to_i
  b = gets.chomp.to_i
  relations[a].push(b)
  relations[b].push(a)
end

amigos = Array.new(n+1, false)
back = Array.new()
stack = Array.new()

#2. Build equivalent classes
(1..n).each do |i|
  next if relations[i].empty?

  back.each{ |n| amigos[n] = false }
  back.clear()
  stack.clear()

  amigos[i] = true
  stack.push(i)
  back.push(i)
  ne = 1
  nr = 0

  until (stack.empty?)
    head = stack.pop()
    relations[head].each do |e|
      if !amigos[e]
        ne += 1
        stack.push(e)
        back.push(e)
        amigos[e] = true
      end
    end
    nr += relations[head].length()
    relations[head].clear()
  end

  if (ne-1)*ne != nr
    puts "NO"
    exit
  end
end

puts "YES"

#我现在的想法是：把相连的点放在一个集合里，然后统计相连的点至少要多少条边，
#如果总边数不够，就打印no，如果总边数够就打印yes！
#今天算是知道了dsu的意思！
#dsu==disjont set！?
nm=gets.chomp.split.map{|x| x.to_i }
n=nm[0]
m=nm[1]
$parent=Array.new(1+n)
$rank=Array.new(1+n)
$size=Array.new(1+n,1)
for i in 0..n
	$parent[i]=i
	$rank[i]=0
end
def find(a)
	while $parent[a]!=a
		a=$parent[a]
	end
	return a
end
def union(a,b)
	ra=find(a)
	rb=find(b)
	if ra==rb
		return
	end
	if $rank[ra]<$rank[rb]
		$parent[ra]=rb
		$size[rb]+=$size[ra];
		$size[ra]=0;
	elsif $rank[ra]>$rank[rb]
		$parent[rb]=ra
		$size[ra]+=$size[rb];
		$size[rb]=0;
	else
		$parent[rb]=ra
		$rank[ra]+=1
		$size[ra]+=$size[rb];
		$size[rb]=0;
	end
end
m.times do
	ab=gets.chomp.split.map{|x| x.to_i }
	union(ab[0],ab[1])
end
see=Array.new(1+n,0)
def all_connect(n)
	n*(n-1)/2
end
#puts $size.join(" ")
total_edge=0
#puts $parent.join(" ")
for i in 1..n
	total_edge+=all_connect($size[i])
	#puts all_connect($size[i])
end
#puts total_edge
#puts m
if total_edge==m
	puts "YES"
else
	puts "NO"
end

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

#friends = Set.new # friends with... er... friends. For .delete() only
friendships = Hash.new do |hash, key|
	hash[key] = []
end # id => [f1, f2, ...]
#friends[A][B], A<B == (A is friend to B)

m.times do
	f1, f2 = gets.split.map(&:to_i)
	friendships[f1] << f2
	friendships[f2] << f1
end
#p friends
#p m

while friendships.size>0
	#p friendships
	f_id, friends = friendships.first
	friendships.delete(f_id)
	next if friends.size == 0
	friends_to_check = friends
	friends_checked = Set.new([f_id])
	friends_count = friends.length
	#p "#> #{f_id} #{friends_count} #{friends}"
	until friends_to_check.empty?
		f = friends_to_check.pop
		friends = friendships[f]
		friendships.delete(f)
		next if friends_checked.include? f
		#p ">>> #{f} #{friends}"
		if friends.size != friends_count
			#OOPS.
			puts "NO"
			exit
		end
		friends.each do |f_new|
			next if friends_checked.include? f_new
			friends_to_check << f_new
		end
		friends_checked << f
	end
	#p "#: #{friends_checked}; #{friends_count}"
	if friends_checked.size!=friends_count+1
		#OOPS!
		puts "NO"
		exit
	end
end
puts "YES"

#我现在的想法是：把相连的点放在一个集合里，然后统计相连的点至少要多少条边，
#如果总边数不够，就打印no，如果总边数够就打印yes！
#今天算是知道了dsu的意思！
#dsu==disjont set！?
nm=gets.chomp.split.map{|x| x.to_i }
n=nm[0]
m=nm[1]
$parent=Array.new(1+n)
$rank=Array.new(1+n)
$size=Array.new(1+n,0)
$see=Array.new(1+n,0)
for i in 0..n
	$parent[i]=i
	$rank[i]=0
end
def find(a)
	if $parent[a]!=a
		$parent[a]=find($parent[a])
	end
	return $parent[a]
end
def union(a,b)
	ra=find(a)
	rb=find(b)
	if ra==rb
		return
	end
	if $rank[ra]<$rank[rb]
		$parent[ra]=rb
	elsif $rank[ra]>$rank[rb]
		$parent[rb]=ra
	else
		$parent[rb]=ra
		$rank[ra]+=1
	end
	if $see[a]==0 && $see[b]==0
		$size[find(a)]+=2
		$see[a]=1
		$see[b]=1
	elsif $see[a]==1 && $see[b]==0
		$size[find(a)]+=1
		$see[b]=1
	elsif $see[a]==0 && $see[b]==1
		$size[find(b)]+=1
		$see[a]=1
	end
end
m.times do
	ab=gets.chomp.split.map{|x| x.to_i }
	union(ab[0],ab[1])
end
see=Array.new(1+n,0)
def all_connect(n)
	n*(n-1)/2
end
total_edge=0
for i in 1..n
	total_edge+=all_connect($size[i])
end
if total_edge==m
	puts "YES"
else
	puts "NO"
end

#1. Accomodate inputs & Initialize
n = gets(" ").to_i
m = gets.chomp.to_i

relations = Array.new(n+1){Array.new()}
(1..m).each do |i|
  a = gets(" ").to_i
  b = gets.chomp.to_i
  relations[a].push(b)
end

amigos = Array.new(n+1, 0)
stack = Array.new()

#2. Build equivalent classes
(1..n).each do |i|
  next if relations[i].empty?

  ne = 1
  nr = 0
  amigos.fill(false)
  stack.clear()

  amigos[i] = true
  stack.push(i)

  while (!stack.empty?)
    head = stack.pop()
    relations[head].each do |e|
      if !amigos[e]
        ne += 1
        stack.push(e)
        amigos[e] = true
      end
    end
    nr += relations[head].length()
    relations[head].clear()
  end

  if ((ne-1)*ne)/2 != nr
    puts "NO"
    exit
  end
end

puts "YES"


def solve(a,b)
    ans=999999999
    $w.times{|x|
        $h.times{|y|
            if x+a-1<$w && y+b-1<$h then
                tans=0
                a.times{|i|
                    b.times{|j|
                        if $map[y+j][x+i]==1 then
                            tans+=1
                        end
                    }
                }
                ans=[ans,tans].min
            end
        }
    }
    return ans
end
$h,$w=gets.split.map(&:to_i)
$map=[]
$h.times{ $map.push(gets.split.map(&:to_i)) }
a,b=gets.split.map(&:to_i)
puts [solve(a,b),solve(b,a)].min

def anal(memo,n,m,a,b)
	min=n*m
	1.upto(n-a+1){|i|1.upto(m-b+1){|j|
		temp=memo[i-1][j-1]+memo[i+a-1][j+b-1]-memo[i+a-1][j-1]-memo[i-1][j+b-1]
		min=temp if temp<min
	}}
	min
end
n,m=gets.split.map(&:to_i)
input=(1..n).map{gets.split.map(&:to_i)}
a,b=gets.split.map(&:to_i)
memo=Array.new(n+1){Array.new(m+1,0)}
1.upto(n){|i|sum=0
1.upto(m){|j|
	sum+=input[i-1][j-1]
	memo[i][j]=memo[i-1][j]+sum
}}
min=anal(memo,n,m,a,b)
ans=[min,anal(memo.transpose,m,n,a,b)].min
p ans

n = gets(' ').to_i
m = gets.to_i
s = Array.new(n) do
    gets.split
end
a = gets(' ').to_i
b = gets.to_i

l = []

if a<=n && b<=m
    for i in 0..(n-a)
    for j in 0..(m-b)
        l << s[i,a].map{|k|k[j,b]}.flatten.count('1')
    end
    end
end

if a<=m && b<=n
    for i in 0..(n-b)
    for j in 0..(m-a)
        l << s[i,b].map{|k|k[j,a]}.flatten.count('1')
    end
    end
end

p l.min

n = gets(' ').to_i
m = gets.to_i
s = Array.new(n) do
    gets.split
end
a = gets(' ').to_i
b = gets.to_i

l = []

if a<=m && b<=n
    for i in 0..(n-a)
    for j in 0..(m-b)
        l << s[i,a].map{|k|k[j,b]}.flatten.count('1')
    end
    end
end

if a<=m && b<=n
    for i in 0..(n-b)
    for j in 0..(m-a)
        l << s[i,b].map{|k|k[j,a]}.flatten.count('1')
    end
    end
end

p l.min


#!/usr/bin/ruby

n, m = $stdin.readline.split.map(&:to_i)
map = []
n.times {
  s = $stdin.readline
  gnome = 0
  sweaty = 0
  for pos in 0..s.length - 1
    c = s[pos].chr
    case c
      when 'G'
        gnome = pos
      when 'S'
        sweaty = pos
    end
  end
  if gnome > sweaty
    puts -1
    exit
  end
  map.push(sweaty-gnome)
}

map.sort!

ans = 1

map.each_index{ |i|
  if i != 0
    if map[i-1] != map[i]
      ans += 1
    end
  end
}

puts "#{ans}"

gets
d = []
while s = gets do
  g = s.index 'G'
  f = s.index 'S'
  if g > f
    p -1
    exit
  end
  d << f - g
end
p d.uniq.length

gets
d = []
while s = gets do
  g = s.index 'G'
  f = s.index 'S'
  if !g or !f or g > f
    p -1
    exit
  end
  d << f - g
end
p d.uniq.length

n, m = gets.split(" ").map &:to_i
distances = []
n.times do
  s = gets
  gnome = s.index 'G'
  flower = s.index 'S'
  if !gnome or !flower or gnome > flower
    p -1
    exit
  end
  distances << flower - gnome
end
p distances.uniq.length

n,m=gets.split.map(&:to_i)
b=[]
n.times{
	a=gets.chomp
	b<<a.index('S')-a.index('G')
}
b.sort!.uniq!
if b[0]<0
	puts -1
else
	puts b.size
end

n, m = gets.split.map &:to_i
map = Array.new(n)
all = []
f = Array.new(m) { 0 }
last = Array.new(m) { Array.new }
f[0] = 1
n.times do |i|
    map[i] = gets.chomp.split(//)
    g = -1; s = -1
    map[i].each_with_index do |x, i|
        g = i if x == 'G'
        s = i if x == 'S'
    end
    if s == -1 || g == -1 || g > s
        puts -1
        exit
    end
    all << s - g
end
puts all.uniq.size

n, m = gets.split.collect{|i| i.to_i}
ans = Array.new(n)
n.times do |i|
    s = gets.chomp
    d = s.index("G")
    c = s.index("S")
    if d > c
        puts -1
        exit
    end
    ans[i] = c-d
end
puts ans.uniq.size

def check_min_distance (board)
	distance = []
	board.each do |row|
		dwarf = 0
		candy = 0
		row.each_with_index do |c, index|
			if c == "G"
				dwarf = index
			elsif c == "S"
				candy = index
			end
		end
		if dwarf < candy
			distance << (candy - dwarf)
		else
			return -1
		end
	end

	return (distance.uniq).length

end

board = []
row = (((gets()).split(" ")).shift).to_i - 1

for i in 0..row
	ary = []
	(gets()).each_char{ |c| ary << c }
	ary.pop
	board << ary
end

puts check_min_distance(board)

n, m = gets.split.map &:to_i
map = Array.new(n)
all = []
f = [true]
n.times do |i|
    map[i] = gets.chomp.split(//)
    g = -1; s = -1
    map[i].each_with_index do |x, i|
        g = i if x == 'G'
        s = i if x == 'S'
    end
    if s == -1 || g == -1 || g > s
        puts -1
        exit
    end
    all << s - g
end
ans = 0
all = all.uniq.sort
all.each do |x|
    if !f[x]
        ans += 1
        m.downto(x) do |i|
            f[i] = true if f[i - x]
        end
    end
end
puts ans

n, m = gets.split.collect{|i| i.to_i}
ans = 0
n.times do
    s = gets.chomp
    d = s.index("G")
    c = s.index("S")
    if d > c
        ans = -1
        break
    end
    ans = c-d-1 if ans < c-d-1
end
puts ans

def check_min_distance (board, n)
    min = -1
    board.each do |row|
        dwarf = 0
        candy = 0
        row.each_with_index do |c, index|
            if c == "G"
                dwarf = index
            elsif c == "S"
                candy = index
            end
        end
        if dwarf < candy
            if min == -1
                min = candy - dwarf
            elsif (candy - dwarf) < min
                min = candy - dwarf
            end
        elsif dwarf != n
            if (n - dwarf) < min
                min = n - dwarf
            end
        elsif dwarf == n
            return -1
        end
    end

    return min
end

board = []
first_line = (gets()).split(" ")
row = (first_line.shift).to_i - 1
n = (first_line.shift).to_i - 1

for i in 0..row
    ary = []
    (gets()).each_char{ |c| ary << c }
    ary.pop
    board << ary
end

puts check_min_distance(board, n)

def check_min_distance (board, n)
    min = -1
    far_goal = 0
    over_dwarf = 0
    board.each do |row|
        dwarf = 0
        candy = 0
        row.each_with_index do |c, index|
            if c == "G"
                dwarf = index
            elsif c == "S"
                candy = index
            end
        end
        if dwarf < candy
            if far_goal < (candy-1 - dwarf)
                far_goal = (candy-1 - dwarf)
            end
        elsif dwarf != n and (candy < dwarf)
            if over_dwarf < dwarf
                over_dwarf = dwarf
            end
        elsif dwarf == n
            return -1
        end
    end

    if (over_dwarf != 0)
        return (n - over_dwarf)
    else
        return far_goal
    end
end

board = []
first_line = (gets()).split(" ")
row = (first_line.shift).to_i - 1
n = (first_line.shift).to_i - 1

for i in 0..row
    ary = []
    (gets()).each_char{ |c| ary << c }
    ary.pop
    board << ary
end

puts check_min_distance(board, n)

def check_min_distance (board)
    min = -1
    board.each do |row|
        dwarf = 0
        candy = 0
        row.each_with_index do |c, index|
            if c == "G"
                dwarf = index
            elsif c == "S"
                candy = index
            end
        end
        if dwarf < candy
            if min == -1
                min = candy - dwarf
            elsif (candy - dwarf) < min
                min = candy - dwarf
            end
        end
    end

    return min
end

board = []
first_line = (gets()).split(" ")
row = (first_line.shift).to_i - 1

for i in 0..row
    ary = []
    (gets()).each_char{ |c| ary << c }
    ary.pop
    board << ary
end

puts check_min_distance board

def check_min_distance (board, n)
    min = -1
    far_goal = 0
    over_dwarf = 0
    board.each do |row|
        dwarf = 0
        candy = 0
        row.each_with_index do |c, index|
            if c == "G"
                dwarf = index
            elsif c == "S"
                candy = index
            end
        end
        if dwarf < candy
            if far_goal < (candy-1 - dwarf)
                far_goal = (candy-1 - dwarf)
            elsif dwarf == candy-1
                return -1
            end
        elsif dwarf != n and (candy < dwarf)
            if over_dwarf < dwarf
                over_dwarf = dwarf
            end
        elsif dwarf == n
            return -1
        end
    end

    if (over_dwarf != 0)
        return (n - over_dwarf)
    else
        return far_goal
    end
end

board = []
first_line = (gets()).split(" ")
row = (first_line.shift).to_i - 1
n = (first_line.shift).to_i - 1

for i in 0..row
    ary = []
    (gets()).each_char{ |c| ary << c }
    ary.pop
    board << ary
end

puts check_min_distance(board, n)

def check_min_distance (board, n)
    min = -1
    board.each do |row|
        dwarf = 0
        candy = 0
        row.each_with_index do |c, index|
            if c == "G"
                dwarf = index
            elsif c == "S"
                candy = index
            end
        end
        if dwarf < candy
            if min == -1
                min = candy - dwarf
            elsif (candy - dwarf) < min
                min = candy - dwarf
            end
        elsif dwarf != n
            if (n - dwarf) < min
                min = n - dwarf
            end
        end
    end

    return min
end

board = []
first_line = (gets()).split(" ")
row = (first_line.shift).to_i - 1
n = (first_line.shift).to_i - 1

for i in 0..row
    ary = []
    (gets()).each_char{ |c| ary << c }
    ary.pop
    board << ary
end

puts check_min_distance(board, n)

def check_min_distance (board, n)
    min = -1
    near_goal = n
    over_dwarf = 0
    board.each do |row|
        dwarf = 0
        candy = 0
        row.each_with_index do |c, index|
            if c == "G"
                dwarf = index
            elsif c == "S"
                candy = index
            end
        end
        if dwarf < candy
            if (candy - dwarf) < near_goal
                near_goal = (candy - dwarf)
            end
        elsif dwarf != n and (candy < dwarf)
            if over_dwarf < dwarf
                over_dwarf = dwarf
            end
        elsif dwarf == n
            return -1
        end
    end

    if (over_dwarf != 0) and (over_dwarf < near_goal)
        return (n - over_dwarf)
    end
    return near_goal

end

board = []
first_line = (gets()).split(" ")
row = (first_line.shift).to_i - 1
n = (first_line.shift).to_i - 1

for i in 0..row
    ary = []
    (gets()).each_char{ |c| ary << c }
    ary.pop
    board << ary
end

puts check_min_distance(board, n)


class Solver
	def main
		n, k = gets.split.map { |x| x.to_i }
		s = gets.split.map { |x| x.to_i }
		k = n if k > n

		ans = 0
		while 2 * k != n do
			ans = [ans, s.pop].max
			k -= 1
			n -= 1
		end

		s = s.zip(s.reverse)
		ans = (s.map { |x| x[0] + x[1] } << ans).max
		puts ans
	end
end

Solver.new.main

_,n=gets.split.map(&:to_i)
a=gets.split.map(&:to_i).reverse
r=0
n.times.map{|i|r=[r,(a[i]||0)+(a[2*n-1-i]||0)].max}
p r

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

h, k = gis
s = gis

x = s.pop k

p x.zip(s.reverse + [0] * (k - s.size)).map{|x,y| x+y}.max

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

# b.rb

def main()
    n, k = gets.chomp().split(" ").map{|si| next si.to_i() }
    s = gets.chomp().split(" ").map{|si| next si.to_i() }

    m = s[-1]
    t = n - k
    t.times do |tt|
        mm = s[tt] + s[2 * t - 1 - tt]
        m = mm if m < mm
    end
    puts m
end

if __FILE__ == $0 then
    main()
end

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

puts (0...(n - k)).to_a.inject(ss[-1]) { |m, i| [m, ss[i] + ss[2 * (n - k) - i - 1]].max }

def main()
	a = gets.split(" ").map{|i| i.to_i}
	n = a[0]
	k = a[1]
	s = gets.split(" ").map{|i| i.to_i}
	s = [-1]+s
	if n <= k
		puts s[n]
	else
		sol = s[n]
		for i in 0...n-k
			x = s[n-k-i]+s[n-k+1+i]
			if x > sol
				sol = x
			end
		end
		puts sol
	end
end
main()

n, k = gets.chomp.split(" ").map(&:to_i)
ss = gets.chomp.split(" ").map(&:to_i)
if n.even? then
  min_box = n / 2
  d = (k - min_box) * 2
else
  min_box = n / 2 + 1
  d = (k - min_box) * 2 + 1
end

res = ss.pop(d)

half = ss.size / 2
right = ss.pop(half).reverse
ss.zip(right).each do |a, b|
  res << a + b
end
puts res.max

def can_put?(weights, k, s)
  return false if weights.last > s
  return weights[0] <= s if weights.size == 1

  used = 0

  ind = 0
  last_ind = weights.size - 1
  while ind <= last_ind
    if weights[ind] + weights[last_ind] > s
      if last_ind > ind
        used += 1
        last_ind -= 1
      else
        used += 1
        ind +=1
        last_ind -= 1
      end
    else
      used += 1
      ind +=1
      last_ind -= 1
    end
  end
  # p [s, used]
  used <= k
end

n,k = $stdin.readline.split.map(&:to_i)
weights = $stdin.readline.split.map(&:to_i)
s = ((0) .. (2*weights.last)).bsearch{|sz|
  can_put?(weights, k, sz)
}

puts s

def can_put?(weights, k, s)
  return false if weights.last > s
  used = 0
  last_ind = weights.size - 1
  weights.each_index do |ind|
    while weights[ind] + weights[last_ind] > s && last_ind > ind
      last_ind -= 1
      used += 1
    end
    if last_ind == ind
      used += 1
    end
  end
  used <= k
end

n,k = $stdin.readline.split.map(&:to_i)
weights = $stdin.readline.split.map(&:to_i)
s = ((0) .. (2*weights.last)).bsearch{|sz|
  can_put?(weights, k, sz)
}

puts s

def can_put?(weights, k, s)
  return false if weights.last > s

  used = 0

  ind = 0
  last_ind = weights.size - 1
  while ind < last_ind
    while weights[ind] + weights[last_ind] > s && last_ind > ind
      last_ind -= 1
      used += 1
    end
    if ind < last_ind && weights[ind] + weights[last_ind] <= s
      used += 1
      ind +=1
      last_ind -= 1
    elsif ind == last_ind
      used += 1
      last_ind -= 1
    end
  end
  # p [s, used]
  used <= k
end

n,k = $stdin.readline.split.map(&:to_i)
weights = $stdin.readline.split.map(&:to_i)
s = ((0) .. (2*weights.last)).bsearch{|sz|
  can_put?(weights, k, sz)
}

puts s

def can_put?(weights, k, s)
  return false if weights.last > s
  return weights[0] <= s if weights.size == 1

  used = 0

  ind = 0
  last_ind = weights.size - 1
  while ind < last_ind
    while weights[ind] + weights[last_ind] > s && last_ind > ind
      last_ind -= 1
      used += 1
    end
    if ind < last_ind && weights[ind] + weights[last_ind] <= s
      used += 1
      ind +=1
      last_ind -= 1
    elsif ind == last_ind
      used += 1
      last_ind -= 1
    end
  end
  # p [s, used]
  used <= k
end

n,k = $stdin.readline.split.map(&:to_i)
weights = $stdin.readline.split.map(&:to_i)
s = ((0) .. (2*weights.last)).bsearch{|sz|
  can_put?(weights, k, sz)
}

puts s

class Solver
	def main
		n, k = gets.split.map { |x| x.to_i }
		s = gets.split.map { |x| x.to_i }

		ans = 0
		while 2 * k != n do
			ans = [ans, s.pop].max
			k -= 1
			n -= 1
		end

		s = s.zip(s.reverse)
		ans = [s.map { |x| x[0] + x[1] } << ans].max
		ans = s.max
	end
end

Solver.new.main

class Solver
	def main
		n, k = gets.split.map { |x| x.to_i }
		s = gets.split.map { |x| x.to_i }

		ans = 0
		while 2 * k != n do
			ans = [ans, s.pop].max
			k -= 1
			n -= 1
		end

		s = s.zip(s.reverse)
		ans = [s.map { |x| x[0] + x[1] } << ans].max
		puts ans
	end
end

Solver.new.main


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

c = t = 0
while c < a.length
  0.upto(a.length - 1) do |i|
    if a[i] >= 0 and a[i] <= c
      c += 1
      a[i] = -1
    end
  end

  break if c == a.length
  t += 1

  (a.length - 1).downto(0) do |i|
    if a[i] >= 0 and a[i] <= c
      c += 1
      a[i] = -1
    end
  end

  t += 1 if c < a.length
end

puts t.to_s

n = gets.chomp.to_i
arr = gets.chomp.split(' ').map(&:to_i)
done = Array.new(n, false)
count = 0
ans = 0
n.times do |time|
  next if count == n

  n.times do |p|
    pos = (time % 2 == 0) ? p : (n - 1) - p
    next if done[pos]
    next if count < arr[pos]
    done[pos] = true
    count += 1
  end

  ans += 1
end

puts ans - 1

n = gets.to_i
s = [nil]+gets.split(" ").map(&:to_i)
p = 0
i = 1
d = 1
c = 0

while p < n

	if s[i] && s[i] <= p
		s[i] = nil
		p += 1
	end

	break if p == n
	if i == n && d == 1
		d = -1
		c += 1
	elsif i == 1 && d == -1
		d = 1
		c += 1
	end
	i += d
end

p c

pieces_of_info, change_dir, position, increasing, cleared_computers = 0,0,0,true,0
n, computers = gets.chomp.to_i, gets.chomp.split(" ").map(&:to_i)
loop do
	if computers[position] <= pieces_of_info and computers[position] != -1
		pieces_of_info += 1
		cleared_computers += 1
		computers[position] = -1
	end

	break if n == cleared_computers

	if increasing
		if position == n-1
			change_dir += 1
			position -= 1
			increasing = false
		else
			position +=1
		end
	else
		if position == 0
			change_dir += 1
			position += 1
			increasing = true
		else
			position -=1
		end
	end
end
puts change_dir

n = gets.chomp.to_i;
arr = gets.chomp.split.map(&:to_i);
info = 0;
ans = 0;
n.times do |i|

  n.times do |j|
    if (arr[j] <= info)
      info += 1
      arr[j] = n + 2;
    end
  end;
  break if (info >= n);
  arr = arr.reverse;
  ans += 1;
end

puts ans;

n=gets.to_i
x=gets.split.map{|e| e.to_i}
ans=-1
i=0
while !x.empty?
	ans+=1
	y=[]
	while !x.empty?
		if x[0]<=i then
			i+=1
		else
			y.unshift(x[0])
		end
		x.shift()
	end
	x=y
end
puts ans

def done?(h, n)
  h.length == n
end

def main
  n = gets.chomp.to_i
  a = gets.chomp.split.map {|i| i.to_i}

  count = 0
  info = 0
  h = {}
  while true
    n.times do |i|
      if not h[i]
        if a[i] <= info
          h[i] = :done
          info += 1
        end
      end
    end

    if done? h, n
      break
    end

    count += 1

    n.times do |i|
      if not h[n-i-1]
        if a[n-i-1] <= info
          h[n-i-1] = :done
          info += 1
        end
      end
    end

    if done? h, n
      break
    end

    count += 1

  end
  puts count

end

main

n = gets.to_i
a = gets.split.map(&:to_i)
info, ans = 0, 0
n.times do |i|
  b = i.even? ? 0.upto(n - 1) : (n - 1).downto(0)
  b.each do |i|
    if a[i] <= info
      info += 1
      a[i] = 1010
    end
  end
  break if info == n
  ans += 1
end
puts ans


gets
$<.each do
  n,k=_1.split.map &:to_i
  if n<2*k-1
    puts -1
    next
  end
  n.times do |i|
    s="."*n
    s[i]=?R if i.even? && (i+1)/2<k
    puts s
  end
end

def solve(n, k)
  limit = k

  if (n - (1 - n % 2)) / 2 + 1< k
    return puts '-1'
  else
    result = []

    for i in (1..n)
      row = []

      for j in (1..n)

        if i == j && i % 2 == 1 && limit > 0
          row << 'R'
          limit -= 1
        else
          row << '.'
        end
      end

      result << row
    end

    result.each { puts _1.join }
  end

end

tests = STDIN.gets.to_i
inputs = []

for i in (1..tests)
  n, k = STDIN.gets.split(' ').map(&:to_i)

  inputs << [n, k]
end

inputs.each do |(n, k)|
  solve(n, k)
end

#!/usr/bin/env ruby

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

  if (n + 1) / 2 < k
    puts -1
    next
  end

  fld = Array.new(n) { '.' * n }
  k.times { |i|
    fld[i * 2][i * 2] = 'R'
  }

  puts fld.join "\n"
end

def solve(n, k)
  limit = k

  if (n / 3) + 1 < k
    return puts '-1'
  else
    result = []

    for i in (1..n)
      row = []

      for j in (1..n)

        if i == j && i % 2 == 1 && limit > 0
          row << 'R'
          limit -= 1
        else
          row << '.'
        end
      end

      result << row
    end

    result.each { puts _1.join }
  end

end

tests = STDIN.gets.to_i
inputs = []

for i in (1..tests)
  n, k = STDIN.gets.split(' ').map(&:to_i)

  inputs << [n, k]
end

inputs.each do |(n, k)|
  solve(n, k)
end

def solve
  n, k = STDIN.gets.split(' ').map(&:to_i)

  limit = k

  if (n / 3) + 1 < k
    return puts '-1'
  else
    result = []

    for i in (1..n)
      row = []

      for j in (1..n)

        if i == j && i % 2 == 1 && limit > 0
          row << 'R'
          limit -= 1
        else
          row << '.'
        end
      end

      result << row
    end

    result.each { puts _1.join }
  end

end

tests = STDIN.gets.to_i

for i in (1..tests)
  solve
end


input = $stdin.readline.split(' ')
n = input[0].to_i; x = input[1].to_i; y=input[2].to_i
a = Array.new(n, 1)

square_sum = n; sum = n
i = 0

until square_sum >= x do
  if sum > y then
    puts -1
    exit
  end
  square_sum -= a[i] * a[i]; a[i]+= 1; sum += 1; square_sum += a[i] * a[i]

end

if sum > y then
  puts -1
  exit
end

a.each do |i| print "#{i} " end
print "\n"

n, x, y = gets.split.map(&:to_i)
t = n - 1
u = y - t
if u * u + t >= x and y >= n
  puts "1\n" * (n - 1) + "#{u}"
else
  puts -1
end

n,x,y=gets.split.map(&:to_i)
if y<n
	a=-1
else
	if (n-1)+(y-n+1)**2>=x
		a=[1]*(n-1)
		a<<y-n+1
	else
		a=-1
	end

end
puts a