@local = false
@answer = 1
@gamecnt = 100

def continue_game
    if @local
        return false if @gamecnt <= 0
        @gamecnt -= 1
        @querycnt = 0
        return true
    else
        line = gets.chomp
        exit if line == "mistake"
        return line == "start"
    end
end

def query_game(x,y)
    if @local
        @querycnt += 1
        return x%@answer >= y%@answer
    else
        STDOUT.puts "? #{x} #{y}"
        STDOUT.flush
        return gets[0] == "x"
    end
end

def answer_game(a)
    if @local
        abort "#{@answer} != #{a}" if @answer != a
        puts "AC:#{@querycnt}"
        @answer = rand(1..(1e9.to_i))
    else
        STDOUT.puts "! #{a}"
        STDOUT.flush
    end
end

#

while continue_game

    x = 1
    y = 0

    while x <= 2e9.to_i && query_game(x, y)
        x, y = x*2, x
    end
    if x > 2e9
        answer_game(1)
        next
    end
    while x-y > 1
        m = (x+y)/2
        if query_game(m, y)
            y = m
        else
            x = m
        end
    end
    answer_game(x)
end

require 'prime'

p = Prime.each(10**5).to_a

loop do
    s = gets.chomp
    break if s == "end"
    min = 0
    max = 10**9
    while max-min > 1
        mid = (min+max)/2
        puts "? #{mid} #{max}"
        STDOUT.flush
        r = gets.chomp
        if r == "x"
            min = mid
        else
            max = mid
        end
    end
    m = max
    ret = 1
    p.each do |i|
        a = 0
        while m%i == 0
            a += 1
            puts "? #{max} #{max/(i**a)}"
            STDOUT.flush
            r = gets.chomp
            if r == "y"
                ret *= i
            end
            m /= i
        end
    end
    puts "? #{max} #{max/m}"
    STDOUT.flush
    r = gets.chomp
    if r == "y"
        ret *= m
    end

    puts "! #{ret}"
    STDOUT.flush
end

DBG = !true
def outp(str)
  puts str
  STDOUT.flush
end

def lt(x,y)
  hashkey = x.to_s + "." + y.to_s
  if $h.has_key?(hashkey)
    return $h[hashkey]
  end
  outp("? #{x} #{y}")
  s = gets.chomp
  exit(0) if s == 'mistake'
  $h[hashkey] = (s=='y')
  return (s=='y')
end

$h = {}
R = (DBG ? 10 : 1000000000)
MONO = 1
SPLIT = 2
S1N = 3
while true
  $h.clear()
  inp = gets.chomp
  exit(0) if inp == "end" || inp == "mistake"
  next if inp != 'start'
  st = MONO
  s = R
  b = t = -1
  while true
    puts "st #{st} s #{s} b #{b} t #{t}" if DBG
    if st == MONO
      if s == 2
        st = S1N
        b = t = 2
        next
      end
      p = s - 2*(s/3)
      q = s - s/3
      if lt(p, q)
        st = SPLIT
        t = s
        s = p
        b = q + 1
      else
        s = q
      end

    elsif st == SPLIT
      if s == 1
        st = S1N
        next
      end
      p = s - s/2
      if lt(p,s)
        s = p
      else
        st = MONO
        b = t = -1
      end
    else # st = S1N
      if b < t
        p = (b+t)/2
        if lt(p,t)
          t = p
        else
          b = (b==t-1 ? t : p+1)
        end
      else  # b == t
        if lt(t,1)
          outp("! #{t}")
        else
          outp("! 1")
        end
        break
      end
    end
  end
end

loop do
    s = gets.chomp
    break if s == "end"
    min = 0
    max = 10**9
    while max-min > 1
        mid = (min+max)/2
        puts "? #{mid} #{max}"
        STDOUT.flush
        r = gets.chomp
        if r == "x"
            min = mid
        else
            max = mid
        end
    end
    m = max
    for i in 2..31623
        while m%i == 0
            puts "? #{max} #{max/i}"
            STDOUT.flush
            r = gets.chomp
            if r == "y"
                break
            else
                max /= i
            end
            m /= i
        end
        while m%i == 0
            m /= i
        end
    end
    puts "! #{max}"
    STDOUT.flush
end

require 'prime'

p = Prime.each(10**5).to_a

loop do
    s = gets.chomp
    break if s == "end"
    min = 0
    max = 10**9
    while max-min > 1
        mid = (min+max)/2
        puts "? #{mid} #{max}"
        STDOUT.flush
        r = gets.chomp
        if r == "x"
            min = mid
        else
            max = mid
        end
    end
    m = max
    ret = 1
    p.each do |i|
        a = 0
        while m%i == 0
            a += 1
            puts "? #{max} #{max/i}"
            STDOUT.flush
            r = gets.chomp
            if r == "y"
                ret *= i
            end
            m /= i
        end
    end
    puts "? #{max} #{max/m}"
    STDOUT.flush
    r = gets.chomp
    if r == "y"
        ret *= m
    end

    puts "! #{max}"
    STDOUT.flush
end

require 'prime'

p = Prime.each(10**5).to_a

loop do
    s = gets.chomp
    break if s == "end"
    min = 0
    max = 10**9
    while max-min > 1
        mid = (min+max)/2
        puts "? #{mid} #{max}"
        STDOUT.flush
        r = gets.chomp
        if r == "x"
            min = mid
        else
            max = mid
        end
    end
    m = max
    ret = 1
    p.each do |i|
        a = 0
        while m%i == 0
            a += 1
            puts "? #{max} #{max/i}"
            STDOUT.flush
            r = gets.chomp
            if r == "y"
                ret *= i
            end
            m /= i
        end
    end
    puts "? #{max} #{max/m}"
    STDOUT.flush
    r = gets.chomp
    if r == "y"
        ret *= m
    end

    puts "! #{ret}"
    STDOUT.flush
end

def ret(a,x,y)
    if x%a >= y%a
        return "x"
    else
        return "y"
    end
end

loop do
    s = gets.chomp
    break if s == "end"
    min = 0
    max = 10**9
    #ans = rand(1..10**9)
    #p ans
    while max-min > 1
        mid = (min+max)/2
        puts "? #{mid} #{max}"
        STDOUT.flush
        r = gets.chomp
        #r = ret(ans,mid,max)
        if r == "x"
            min = mid
        else
            max = mid
        end
    end
    for i in 2..31623
        while max%i == 0
            puts "? #{max} #{max/i}"
            STDOUT.flush
            r = gets.chomp
            #r = ret(ans,max,max/i)
            if r == "y"
                break
            else
                max /= i
            end
        end
    end
    puts "! #{max}"
    STDOUT.flush
end

require 'prime'

loop do
    s = gets.chomp
    break if s == "end"
    min = 0
    max = 10**9
    while max-min > 1
        mid = (min+max)/2
        puts "? #{mid} #{max}"
        STDOUT.flush
        r = gets.chomp
        if r == "x"
            min = mid
        else
            max = mid
        end
    end
    m = max
    Prime.each(10**5) do |i|
        break if i > m || m%i != 0
        while m%i == 0
            puts "? #{max} #{max/i}"
            STDOUT.flush
            r = gets.chomp
            if r == "x"
                max /= i
            else
                break
            end
            m /= i
        end
        while m%i == 0
            m /= i
        end
        puts "? #{max} #{i}"
        STDOUT.flush
        r = gets.chomp
        if r == "x"
            max = i
            m = i
        end
    end
    puts "! #{max}"
    STDOUT.flush
end

def ret(a,x,y)
    if x%a >= y%a
        return "x"
    else
        return "y"
    end
end

loop do
    s = gets.chomp
    break if s == "end"
    min = 0
    max = 10**9
    #ans = rand(1..10**9)
    #p ans
    while max-min > 1
        mid = (min+max)/2
        puts "? #{mid} #{min}"
        STDOUT.flush
        r = gets.chomp
        #r = ret(ans,mid,max)
        if r == "x"
            min = mid
        else
            max = mid
        end
    end
    for i in 2..31623
        while max%i == 0
            puts "? #{max} #{max/i}"
            STDOUT.flush
            r = gets.chomp
            #r = ret(ans,max,max/i)
            if r == "y"
                break
            else
                max /= i
            end
        end
    end
    puts "! #{max}"
    STDOUT.flush
end

DBG = !true
def outp(str)
  puts str
  STDOUT.flush
end

def lt(x,y)
  outp("? #{x} #{y}")
  s = gets.chomp
  exit(0) if s == 'mistake'
  return (s=='y')
end

R = (DBG ? 10 : 1000000000)
MONO = 1
SPLIT = 2
S1N = 3
while true
  inp = gets.chomp
  exit(0) if inp == "end" || inp == "mistake"
  next if inp != 'start'
  st = MONO
  s = R
  b = t = -1
  while true
    puts "st #{st} s #{s} b #{b} t #{t}" if DBG
    if st == MONO
      if s == 2
        st = S1N
        b = t = 2
        next
      end
      p = s - 2*(s/3)
      q = s - s/3
      if lt(p, q)
        st = SPLIT
        t = s
        s = p
        b = q
      else
        s = q
      end

    elsif st == SPLIT
      if s == 1
        st = S1N
        next
      end
      p = s - s/2
      if lt(p,s)
        s = p
      else
        st = MONO
        b = t = -1
      end
    else # st = S1N
      if b < t
        p = (b+t)/2
        if lt(p,t)
          t = p
        else
          b = (b==t-1 ? t : p)
        end
      else  # b == t
        if lt(t,1)
          outp("! #{t}")
        else
          outp("! 1")
        end
        break
      end
    end
  end
end

DBG = !true
def outp(str)
  puts str
  STDOUT.flush
end

def lt(x,y)
  hashkey = x.to_s + "." + y.to_s
  if $h.has_key?(hashkey)
    return $h[hashkey]
  end
  outp("? #{x} #{y}")
  s = gets.chomp
  exit(0) if s == 'mistake'
  $h[hashkey] = (s=='y')
  return (s=='y')
end

$h = {}
R = (DBG ? 10 : 1000000000)
MONO = 1
SPLIT = 2
S1N = 3
while true
  $h.clear()
  inp = gets.chomp
  exit(0) if inp == "end" || inp == "mistake"
  next if inp != 'start'
  st = MONO
  s = R
  b = t = -1
  while true
    puts "st #{st} s #{s} b #{b} t #{t}" if DBG
    if st == MONO
      if s == 2
        st = S1N
        b = t = 2
        next
      end
      p = s - 2*(s/3)
      q = s - s/3
      if lt(p, q)
        st = SPLIT
        t = s
        s = p
        b = q + 1
      else
        s = q
      end

    elsif st == SPLIT
      if s == 1
        st = S1N
        next
      end
      p = s - s/2
      if lt(p,s)
        s = p
      else
        st = MONO
        b = t = -1
      end
    else # st = S1N
      if b < t
        p = (b+t)/2
        if lt(p,t)
          t = p
        else
          b = (b==t-1 ? t : p)
        end
      else  # b == t
        if lt(t,1)
          outp("! #{t}")
        else
          outp("! 1")
        end
        break
      end
    end
  end
end

DBG = !true
def outp(str)
  puts str
  STDOUT.flush
end

def lt(x,y)
  hashkey = x.to_s + "." + y.to_s
  if $h.has_key?(hashkey)
    return $h[hashkey]
  end
  outp("? #{x} #{y}")
  s = gets.chomp
  exit(0) if s == 'mistake'
  $h[hashkey] = (s=='y')
  return (s=='y')
end

$h = {}
R = (DBG ? 10 : 1000000000)
MONO = 1
SPLIT = 2
S1N = 3
while true
  $h.clear()
  inp = gets.chomp
  exit(0) if inp == "end" || inp == "mistake"
  next if inp != 'start'
  st = MONO
  s = R
  b = t = -1
  while true
    puts "st #{st} s #{s} b #{b} t #{t}" if DBG
    if st == MONO
      if s == 2
        st = S1N
        b = t = 2
        next
      end
      p = s - 2*(s/3)
      q = s - s/3
      if lt(p, q)
        st = SPLIT
        t = s
        s = p
        b = q
      else
        s = q
      end

    elsif st == SPLIT
      if s == 1
        st = S1N
        next
      end
      p = s - s/2
      if lt(p,s)
        s = p
      else
        st = MONO
        b = t = -1
      end
    else # st = S1N
      if b < t
        p = (b+t)/2
        if lt(p,t)
          t = p
        else
          b = (b==t-1 ? t : p)
        end
      else  # b == t
        if lt(t,1)
          outp("! #{t}")
        else
          outp("! 1")
        end
        break
      end
    end
  end
end

DBG = true
def outp(str)
  puts str
  STDOUT.flush
end

def lt(x,y)
  outp("? #{x} #{y}")
  s = gets.chomp
  exit(0) if s == 'mistake'
  return (s=='y')
end

R = (DBG ? 10 : 1000000000)
MONO = 1
SPLIT = 2
S1N = 3
while true
  inp = gets.chomp
  exit(0) if inp == "end" || inp == "mistake"
  next if inp != 'start'
  st = MONO
  s = R
  b = t = -1
  while true
    puts "st #{st} s #{s} b #{b} t #{t}" if DBG
    if st == MONO
      if s == 2
        st = S1N
        b = t = 2
        next
      end
      p = s - 2*(s/3)
      q = s - s/3
      if lt(p, q)
        st = SPLIT
        t = s
        s = p
        b = q
      else
        s = q
      end

    elsif st == SPLIT
      if s == 1
        st = S1N
        next
      end
      p = s - s/2
      if lt(p,s)
        s = p
      else
        st = MONO
        b = t = -1
      end
    else # st = S1N
      if b < t
        p = (b+t)/2
        if lt(p,t)
          t = p
        else
          b = (b==t-1 ? t : p)
        end
      else  # b == t
        if lt(t,1)
          outp("! #{t}")
        else
          outp("! 1")
        end
        break
      end
    end
  end
end

@local = false
@answer = 1
@trycnt = 29

def continue_game
    if @local
        return false if @trycnt <= 0
        @trycnt -= 1
        return true
    else
        line = gets.chomp
        exit if line == "mistake"
        return line == "start"
    end
end

def query_game(x,y)
    if @local
        return x%@answer >= y%@answer
    else
        STDOUT.puts "? #{x} #{y}"
        STDOUT.flush
        return gets[0] == "x"
    end
end

def answer_game(a)
    if @local
        abort if @answer != a
        puts "AC"
        @answer = rand(1..100000)
    else
        STDOUT.puts "! #{a}"
        STDOUT.flush
    end
end

#

while continue_game

    if query_game(0, 1)
        answer_game(1)
        next
    end
    x = 1
    y = 0

    while query_game(x, y)
        x, y = x*2, x
    end
    while x-y > 1
        m = (x+y)/2
        if query_game(m, y)
            y = m
        else
            x = m
        end
    end
    answer_game(x)
end


def execute(n, k, arr)
  return [1, 1] if k == 1
  return [-1, -1] if k > n

  @lelf, @right = 0
  @arr_count = [0] * (10**5 + 1)
  @count = 0

  for j in 0..n-1
    @arr_count[arr[j]] += 1

    if @arr_count[arr[j]] == 1
      @count += 1
      @right = j
    end
    # puts "puts arr[#{j}]: #{arr[j]}, number count: #{@arr_count[arr[j]]},  @count: #{@count}, }"

    if @count == k
      break
    end

  end

  return [-1, -1] if @count < k

  while @count == k
    @arr_count[arr[@lelf]] -= 1
    if @arr_count[arr[@lelf]] == 0
      break
    end
    @lelf += 1
  end

  return [@lelf + 1, @right + 1]
end

n, k = gets.chomp.split(' ').map(&:to_i)
arr = gets.chomp.split(' ').map(&:to_i)
result = execute(n, k, arr)
puts result.join(' ')

#!/usr/bin/ruby

n, k = STDIN.readline.split.collect {|s| s.to_i }
arr = STDIN.readline.split.collect {|s| s.to_i }
if arr.uniq.length < k
	puts '-1 -1'
	exit
end
if k == 1
	puts '1 1'
	exit
end
count = 1
unique_hash = { arr[0] => true }
right = 0
while count < k
	right += 1
	x = arr[right]
	if not unique_hash.include? x
		unique_hash[x] = true
		count += 1
	end
end
count = 1
left = right
unique_hash = { arr[right] => true }
while count < k
	left -= 1
	x = arr[left]
	if not unique_hash.include? x
		unique_hash[x] = true
		count += 1
	end
end
puts '%d %d' % [left+1, right+1]

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

tmp = []
l = r = 0

count = 0

while r < n do
  tmp[arr[r]] ? tmp[arr[r]] += 1 : tmp[arr[r]] = 1
  count += 1 if tmp[arr[r]] == 1
  if count == k
    while l <= r do
      tmp[arr[l]] -= 1
      if tmp[arr[l]] == 0
        puts "#{l + 1} #{r + 1}"
        exit
      end
      l += 1
    end
  end
  r += 1
end

puts "-1 -1"

require 'set'
buf = gets.split(' ')
n = buf[0].to_i
k = buf[1].to_i
st = Set.new
buf = gets.split(' ')
0.upto(n - 1) { |ii|
    i = buf[ii]
    st.add(i)
    if st.size == k
        st.clear
        ii.downto(0) { |jj|
            j = buf[jj]
            st.add(j)
            if st.size == k
                print jj + 1, " ", ii + 1
                exit 0
            end
        }
    end
}
print "-1 -1"

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

def aaa(list, k)
  count = Hash.new { |hash, key| hash[key] = 0 }

  for r in 0..(list.length - 1)
    count[list[r]] += 1

    if count[list[r]] == 1
      k -= 1
    end

    if k == 0
      for l in 0..r
        count[list[l]] = count[list[l]] - 1

        if count[list[l]] == 0
          return [l + 1, r + 1]
        end
      end
    end
  end

  return [-1, -1]
end

result = aaa(list, k)

print result[0], " ", result[1]

require 'set'

$incr = Proc.new {|st,x| st[x].nil? ? (st[x] = 1) : (st[x] += 1) }
$decr = Proc.new {|st,x| st[x].nil? ? 0 : (st[x] == 1 ? (st.delete x) : (st[x] -= 1) ) }
$dis  = Proc.new {|ls| st = {}; ls.each {|x| $incr.(st,x) } ; st }

def good(ls,m,k)

  l = ls.length - m - 1
  st = $dis.(ls.take(m))
  cnt = st.size
  if cnt >= k
    return [true,0,ls]
  else
    (0..l).each do |i|
      $incr.(st,ls[m+i])
      $decr.(st,ls[i])
      if st.size >= k
        ar = Array.new(m)
        i += 1
        m.times do |j|
          ar[j] = ls[j+i]
        end
        return [true,i,ar]
      end
    end
  end
  return [false]
end

def doIt(n,k,ls)
  off = 0
  h = n
  l = [k - 1,0].max
  a = [false]
  while (h - l > 1)
    m = (h+l) / 2
    a = good(ls,m,k)
    if a.first
      h = m
      ls = a[2]
      off += a[1]
    else
      l = m
    end
  end
  a = good(ls,h,k)
  puts "#{a[1]+1+off} #{a[1]+h+off}"

end

xs = gets.split.map(&:to_i)
n, k = xs[0], xs[1]
ls = gets.split.map(&:to_i)

st = Set.new

ls.each {|x| st.add x}

if st.size < k then
  puts "-1 -1" #cant be done
else
  doIt(n,k,ls)
end

require 'set'

def good(ls,m,k)

  incr = Proc.new {|st,x| st[x].nil? ? (st[x] = 1) : (st[x] += 1) }
  decr = Proc.new {|st,x| st[x].nil? ? 0 : (st[x] == 1 ? (st.delete x) : (st[x] -= 1) ) }
  dis  = Proc.new {|ls| st = {}; ls.each {|x| incr.(st,x) } ; st }

  l = ls.length - m - 1
  st = dis.(ls.take(m))
  cnt = st.size
  if cnt >= k
    return [true,0,ls]
  else
    (0..l).each do |i|
      incr.(st,ls[m+i])
      decr.(st,ls[i])
      if st.size >= k
        ar = Array.new(m)
        i += 1
        m.times do |j|
          ar[j] = ls[j+i]
        end
        return [true,i,ar]
      end
    end
  end
  return [false]
end

def doIt(n,k,ls)
  off = 0
  h = n
  l = [k - 1,0].max
  a = [false]
  while (h - l > 1)
    m = (h+l) / 2
    a = good(ls,m,k)
    if a.first
      h = m
      ls = a[2]
      off += a[1]
    else
      l = m
    end
  end
  a = good(ls,h,k)
  puts "#{a[1]+1+off} #{a[1]+h+off}"

end

xs = gets.split.map(&:to_i)
n, k = xs[0], xs[1]
ls = gets.split.map(&:to_i)

st = Set.new

ls.each {|x| st.add x}

if st.size < k then
  puts "-1 -1" #cant be done
else
  doIt(n,k,ls)
end

def execute(n, k, arr)
  @count = 0
  @arr_count = [0] * n
  @arr_count[0] = 1
  @lelf, @right = 0

  for j in 1..n-1
    @i = 0

    # skip if arr[j] count before
    # next if @arr_count[arr.index(@arr_count[j])] == 0

    while @i < j do
      if arr[@i] == arr[j]
        @arr_count[j] = @arr_count[j - 1]
        break
      end
      @i += 1
    end

    # item no exist [i..j-2]
    if @i == j
      @arr_count[j] = @arr_count[j - 1] + 1
      @right = j
      @count = @arr_count[j]
    end

    # puts "j: #{j}, @count: #{@count}, #{@arr_count.join(' ')}"

    break if @arr_count[j] == k
  end

  # check @count, shifl @lelf + 1

  if @count < k
    return [-1, -1]
  else
    while @arr_count[@lelf + 1] == 1
      @lelf += 1
    end
    return [@lelf + 1, @right + 1] if @right > 0
  end

end

# puts "test case 1"
# puts execute(4, 2,  %w(1 2 2 3).map(&:to_i)).inspect
# #arr_count             1 2 0 0

# puts "test case 2"
# puts execute(8, 3, %w(1 1 2 2 3 3 4 5).map(&:to_i)).inspect
# #arr_count            1 1 2 2 3 0 0 0

# puts "test case 3"
# puts execute(7, 4, %w(4 7 7 4 7 4 7).map(&:to_i)).inspect
# #arr_count            1 2 2 2 2 2 2

n, k = gets.chomp.split(' ').map(&:to_i)
arr = gets.chomp.split(' ').map(&:to_i)
result = execute(n, k, arr)
puts result.join(' ')

#!/usr/bin/ruby

n, k = STDIN.readline.split.collect {|s| s.to_i }
arr = STDIN.readline.split.collect {|s| s.to_i }
if arr.uniq.length < k
	puts '-1 -1'
	exit
end
if k == 1
	puts '1 1'
	exit
end
start = 0
start += 1 while arr[start] == arr[start+1]
count = 1
unique_hash = { arr[start] => true }
pos = start
while count < k
	pos += 1
	x = arr[pos]
	if not unique_hash.include? x
		unique_hash[x] = true
		count += 1
	end
end
puts '%d %d' % [start+1, pos+1]

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

distinct_arr = arr.uniq
tmp = []

if distinct_arr.size < k
  puts "-1 -1"
else
  k.times.each do |i|
    tmp.push(distinct_arr[i])
  end

  res = []
  max = tmp.max
  tmp.each do |v|
    if v == max
      res.push(arr.each_index.select { |i| arr[i] == v }.min)
    else
      res.push(arr.each_index.select { |i| arr[i] == v }.max)
    end
  end

  puts "#{res.first + 1} #{res.last + 1}"
end

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

distinct_arr = arr.uniq
tmp = []

if distinct_arr.size < k
  puts "-1 -1"
else
  k.times.each do |i|
    tmp.push(distinct_arr[i])
  end

  last = arr.each_index.select { |i| arr[i] == tmp[-1] }.min
  first = arr.each_index.select { |i| arr[i] == tmp[0] && i < last }.max

  puts "#{first.to_i + 1} #{last.to_i + 1}"
end

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

distinct_arr = arr.uniq
tmp = []

if distinct_arr.size < k
  puts "-1 -1"
else
  k.times.each do |i|
    tmp.push(distinct_arr[i])
  end

  res = []
  tmp.each do |v|
    if v == tmp[-1]
      res.push(arr.each_index.select { |i| arr[i] == v }.min)
    else
      res.push(arr.each_index.select { |i| arr[i] == v }.max)
    end
  end

  puts "#{res.first + 1} #{res.last + 1}"
end

require 'set'
buf = gets.split(' ')
n = buf[0].to_i
k = buf[1].to_i
st = Set.new
buf = gets.split(' ')
0.upto(n - 1) { |ii|
    i = buf[ii]
    st.add(i)
    if st.size == k
      print 1, " ", ii + 1
      exit 0
    end
}
print "-1 -1"

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

def aaa(list, k)
  hash = Hash.new { |hash, key| hash[key] = 0 }
  for i in 0..(list.length - 1)
    hash[list[i]] += 1
  end

  if hash.keys.count < k
    return [-1, -1]
  else
    b = []
    for i in 0..(list.length - 1)
      b << [i, list[i]]

      if list[i] != list[i + 1]
        k = k - 1
      end

      if k == 0
        break
      end
    end
    b
    c = []
    d = []
    for i in 0..(b.length - 1)
      if b[i][1] == b[0][1]
        c << i
      end
      if b[i][1] == b[-1][1]
        d << i
      end
    end
    return [c.max + 1, d.min + 1]
  end
end

result = aaa(list, k)
result.each do |i|
  print i
  print " " if i != result[-1]
end

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

def aaa(list, k)
  hash = Hash.new { |hash, key| hash[key] = 0 }
  for i in 0..(list.length - 1)
    hash[list[i]] += 1
  end

  if hash.keys.count < k
    "-1 -1"
  else
    b = []
    for i in 0..(list.length - 1)
      b << [i, list[i]]

      if list[i] != list[i + 1]
        k = k - 1
      end

      if k == 0
        break
      end
    end
    b
    c = []
    d = []
    for i in 0..(b.length - 1)
      if b[i][1] == b[0][1]
        c << i
      end
      if b[i][1] == b[-1][1]
        d << i
      end
    end
    "#{c.max + 1} #{d.min + 1}"
  end
end

p aaa(list, k)

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

def aaa(list, k)
  hash = Hash.new { |hash, key| hash[key] = 0 }
  for i in 0..(list.length - 1)
    hash[list[i]] += 1
  end

  if hash.keys.count < k
    return [-1, -1]
  else
    b = []
    for i in 0..(list.length - 1)
      b << [i, list[i]]

      if list[i] != list[i + 1]
        k = k - 1
      end

      if k == 0
        break
      end
    end
    b
    c = []
    d = []
    for i in 0..(b.length - 1)
      if b[i][1] == b[0][1]
        c << i
      end
      if b[i][1] == b[-1][1]
        d << i
      end
    end
    return [c.max + 1, d.min + 1]
  end
end

result = aaa(list, k)
result.each.with_index do |element, index|
  print element
  print " " if index != result.length - 1
end

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

def aaa(list, k)
  hash = Hash.new { |hash, key| hash[key] = 0 }
  for i in 0..(list.length - 1)
    hash[list[i]] += 1
  end

  if hash.keys.count < k
    return [-1, -1]
  else
    b = []
    for i in 0..(list.length - 1)
      b << [i, list[i]]

      if list[i] != list[i + 1]
        k = k - 1
      end

      if k == 0
        break
      end
    end
    b
    c = []
    d = []
    for i in 0..(b.length - 1)
      if b[i][1] == b[0][1]
        c << i
      end
      if b[i][1] == b[-1][1]
        d << i
      end
    end
    return [c.max, d.min]
  end
end

result = aaa(list, k)
result.each do |i|
  print i
  print " " if i != result[-1]
end

require 'set'

def good(ls,m,k)

  incr = Proc.new {|st,x| st[x].nil? ? (st[x] = 1) : (st[x] += 1) }
  decr = Proc.new {|st,x| st[x].nil? ? 0 : (st[x] == 1 ? (st.delete x) : (st[x] -= 1) ) }
  dis  = Proc.new {|ls| st = {}; ls.each {|x| incr.(st,x) } ; st }

  l = ls.length - m - 1
  tk  = ls.take(m)
  st = dis.(tk)
  cnt = st.size
  if cnt >= k
    return [true,0,tk]
  else
    (0..l).each do |i|
      tk.delete_at 0; tk.push(ls[m+i])
      incr.(st,ls[m+i])
      decr.(st,ls[i])
      if st.size >= k
        return [true,i+1,tk]
      end
    end
  end
  return [false]
end

def doIt(n,k,ls)

  h = n
  l = [k - 1,0].max
  a = [false]
  while (h - l > 1)
    m = (h+l) / 2
    a = good(ls,m,k)
    if a.first
      h = m
      ls = a[2]
    else
      l = m
    end
  end
  a = good(ls,h,k)
  puts "#{a[1]+1} #{a[1]+h}"

end

xs = gets.split.map(&:to_i)
n, k = xs[0], xs[1]
ls = gets.split.map(&:to_i)

st = Set.new

ls.each {|x| st.add x}

if st.size < k then
  puts "-1 -1" #cant be done
else
  doIt(n,k,ls)
end


def run
  n = gets.chomp.to_i

  if n == 1
    puts "0"
    return
  end

  nc = n
  twos = 0
  while true
    if nc % 2 == 0
      nc /= 2
      twos += 1
    else
      break
    end
  end

  threes = 0
  while true
    if nc % 3 == 0
      nc /= 3
      threes += 1
    else
      break
    end
  end

  if nc != 1
    puts "-1"
    return
  end

  if threes >= twos
    puts threes + threes - twos
  else
    puts "-1"
  end
end

def main
  t = gets.chomp.to_i

  t.times do
    run
  end
end

main

gets.to_i.times do
  n = gets.to_i
  ans = 0
  while n % 6 == 0 or (n != 1 and n.odd?) do
    ans += 1
    n = if n % 6 == 0 then n / 6 else n * 2 end
  end
  puts -1 if n != 1
  puts ans if n == 1
end

gets.to_i.times do
  n = gets.to_i

  d3 = 0
  while n.modulo(3).zero?
    n  /= 3
    d3 += 1
  end

  d2 = 0
  while n.modulo(2).zero?
    n  /= 2
    d2 += 1
  end

  if n > 1 || d2 > d3
    puts -1
    next
  end

  puts (d3 - d2) + d3
end

gets.to_i.times do
	n = gets.to_i
	cnt2, cnt3 = 0, 0
	while n % 2 == 0
		n /= 2
		cnt2 += 1
	end
	while n % 3 == 0
		n /= 3
		cnt3 += 1
	end
	if n == 1 && cnt2 <= cnt3
		puts (cnt3 - cnt2) + cnt3
	else
		puts -1
	end
end

t = gets.to_i
t.times do
	n = gets.to_i
	div2 = 0
	div3 = 0
	while (n & 1) == 0
		div2 += 1
		n >>= 1
	end
	while (n % 3) == 0
		div3 += 1
		n /= 3
	end
	if n != 1 or div2 > div3
		puts "-1"
	else
		p div3*2 - div2
	end
end


t = gets.chomp.to_i
(0...t).each do |_|
  l = "*#{gets.chomp}*"
  # puts l
  d, c = 0, 0
  (0...l.size).each do |i|
    (c+=1; next) if i.zero?
    if l[i] == 'R'
      d = [d, c].max
      c = 0
    elsif l[i] == '*'
      d = [d, c].max
    end
    c+=1
  end
  puts d
end

# coding: utf-8

t = gets.to_i
t.times {
    #         L     R
    # | 0 | | 1 | | 2 | ............ | n | | n+1 |
    # frog
    #   ------------>
    #   jump by [1, d] size, you can choose 1 for the smallest one
    # minimize size 'd'
    # range of d, min = 1 'RRR...RRRR', max = n+1 'LLL...LLL'
    # every sequence has a solution.

    # find the longest sequence of 'L' and + 1 to reach 'R' again
    max_l, l = 0, 0
    gets.chomp.split(//).each { |c|
        if c == 'R'
            if max_l < l
                max_l = l
            end
            l = 0
        else
            l += 1
        end
    }

    if max_l < l
        max_l = l # clean up
    end

    puts max_l + 1
}

tc = gets.to_i

tc.times do
    s1 =  gets.chomp
    s = "R" + s1 + "R"
    maxi = -1
    prev = 0
    l = s.size
    (1...l).each do |i|
        if(s[i] == 'R')
            now = i - prev
            maxi = [maxi,now].max
            prev = i
        end
    end
    puts maxi
end

t = gets.to_i
t.times do
  s = gets.chomp
  run_len = 0
  max_run_len = 0
  s.each_char do |c|
    case c
    when "L"
      run_len += 1
    when "R"
      max_run_len = run_len if max_run_len < run_len
      run_len = 0
    end
  end
  max_run_len = run_len if max_run_len < run_len
  puts max_run_len + 1
end


# Warning: This code sucks.

require "set"

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

aijs = n.times.map do
  gets.split.map(&:to_i).drop(1)
end

groups = []
aijs.each_with_index do |ai, i|
  groups[i] = Set.new ai
end

know_nothing = groups.select(&:empty?).count

loop do
  new = []
  0.upto(groups.size - 1) do |i|
    found = false
    0.upto(new.size - 1) do |j|
      if (groups[i] & new[j]).any?
        new[j] += groups[i]
        found = true
        break
      end
    end
    if !found
      new << groups[i]
    end
  end
  break if new == groups
  groups = new
end

ans = groups.count - 1
if know_nothing == groups.count
  ans += 1
end
p ans

n, m = gets.split.map(&:to_i)
$list = Array.new(n)
class Emp
  attr_reader :lang, :group, :edge
  def initialize(lang)
    @lang = lang
    @group = nil
	@edge = Array.new
  end #def init

  def edgeplus(n)
    @edge << n
  end #def edge

  def search(g)
    @group = g
    for i in @edge
	  if $list[i].group == nil
	    $list[i].search(g)
	  end
	end
  end #def search
end #class

allnone = true
for i in 0...n
  lang = gets.split.map(&:to_i).drop(1)
  $list[i] = Emp.new(lang)
  if lang.count > 0 then allnone = false end
  for j in 0...i
    if ($list[i].lang & $list[j].lang).count > 0
	  $list[j].edgeplus(i)
	  $list[i].edgeplus(j)
	end
  end
end

g = 0
for i in 0...n
  if $list[i].group == nil
    $list[i].search(g)
	g += 1
  end
end

if !allnone then g -= 1 end
puts g

#!/usr/bin/ruby1.9

n, m = STDIN.readline.split.collect {|s| s.to_i }
seen = (m+1).times.collect { false }
connected = (m+1).times.collect { {} }

mute_count = 0

n.times do
	as = STDIN.readline.split.collect {|s| s.to_i }
	count = as.shift
	if count == 0
		mute_count += 1
		next
	end
	as.length.times do |i|
		u = as[i]
		seen[u] = true
		(i..as.length-1).each do |j|
			v = as[j]
			connected[u][v] = true
			connected[v][u] = true
		end
	end
end

marked = (m+1).times.collect { false }
component_count = 0

def explore(u, marked, connected)
	marked[u] = true
	connected[u].keys.each do |v|
		next if marked[v]
		explore(v, marked, connected)
	end
end

(1..m).each do |start|
	next if marked[start] or not seen[start]
	component_count += 1
	explore(start, marked, connected)
end

puts [component_count-1, 0].max + mute_count

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
lang = []
for i in 0...n
    lang[i] = []
    lang[i] = gets.chomp.split.map{|j| Integer(j)}
end
pointer = 0
for i in 0...n
    if lang[i] != [0]
        pointer = 1
        break
    end
end
for i in 0...n
    lang[i].shift
end
boolean = []
match = []
for i in 0...n
    boolean[i] = []
    for j in 0...n
        match = (lang[i] & lang[j]).length
        if match == 0
            boolean[i].push(false)
        else
            boolean[i].push(true)
        end
    end
end

def fill_unit point, unit_id, unit, n, boolean
    if unit[point]
        return
    end

    unit[point] = unit_id
    for i in 0...n
        if boolean[point][i]
            fill_unit(i, unit_id, unit, n, boolean)
        end
    end
end

unit = Array.new(n)
for i in 0...n
    fill_unit(i, i, unit, n, boolean)
end

unit.uniq!
if pointer == 0
    puts lang.length
else
    puts unit.length - 1
end

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
lang = []
for i in 0...n
    lang[i] = []
    lang[i] = gets.chomp.split.map{|j| Integer(j)}
end
pointer = 0
for i in 0...n
    if lang[i] != [0]
        pointer = 1
        break
    end
end
unit = []
for i in 0...n
    lang[i].shift
    unit.push(i)
end
boolean = []
match = []
for i in 0...n
    boolean[i] = []
    for j in 0...n
        match = (lang[i] & lang[j]).length
        if match == 0
            boolean[i].push(false)
        else
            boolean[i].push(true)
        end
    end
end

for i in 0...n
    for j in 0..i
        if boolean[i][j]
            m = unit[j]
            for k in 0...n
                if unit[k] == m
                    unit[k] = i
                end
            end
        end
    end
end
unit.uniq!
if pointer == 0
    puts lang.length
else
    puts unit.length - 1
end

n,m=gets.split.map(&:to_i)
a=[]
n.times do
    k=0
    gets.split.map(&:to_i)[1..-1].each do |v|
        k+=(1<<(v-1))
    end
    a<<k
end

changed=true
while changed do
    changed=false
    for i in 0..n-1 do
        if a[i]>=0 then
            for j in i+1..n-1 do
                if a[j]>=0 and (a[i]&a[j])>0 then
                    a[i]|=a[j]
                    a[j]=-1
                    changed=true
                end
            end
        end
    end
end

z=res=0
a.each do |v|
    res+=1 if v>=0
    z+=1 if v==0
end
puts z==n ? n : res-1

n, m = gets.split(' ').collect!{|i| i.to_i}

k = []
n.times do |j|
	k[j] = gets.split(' ').collect!{|i| i.to_i}
end

needtolearn = 0

@mygroups = []

def checkinclude(int)
	@mygroups.each_index do |i|
		return i if @mygroups[i].include?(int)
	end
	return false
end

k.each do |line|
	if line[0] == 0
		needtolearn += 1
	else
		found = false
		line[1..-1].each do |lang|
			if grn = checkinclude(lang)
				@mygroups[grn] = @mygroups[grn] | line[1..-1]
				found = true
				break
			end
		end
		if !found
			@mygroups << line[1..-1]
		end
	end
end

size = @mygroups.size
size.times do |i|
	size.times do |j|
		if i != j && !(@mygroups[i] & @mygroups[j]).empty?
			@mygroups[i] = @mygroups[i] | @mygroups[j]
			@mygroups[j] = []
		end
	end
end

@mygroups.delete_if {|x| x.empty? }

learning = (@mygroups.size - 1)
learning = 0 if learning < 0

p needtolearn + learning

class Node
	attr_accessor :id, :visited, :links, :exists
	def initialize id
	    @exists = true
		@id = id
		@visited = false
		@links = []
	end
	def visit nodes
		if !@visited
			@visited = true
			@links.each { |l|
				nodes[l-1].visit nodes
			}
		end
	end
end

n,m = gets.split(" ").collect! {|x| x.to_i}
nodes = [0]*n
graph = [0]*n
(1..n).each {|v|
	node = Node.new v
	nodes[v-1] = node
	graph[v-1] = node
}
zero_list = [0]*m
zeros = 0
arrs = []
(1..n).each { |k|
arr = gets.split(" ").collect! {|x| x.to_i}
arr = arr[1..-1]
if arr.size == 0
	zeros += 1
    nodes[k-1].exists = false
end
arrs.push arr
}

(1..n).each { |s|
 (s+1..n).each { |t|
	if (arrs[s-1]&arrs[t-1]).size>0
		nodes[s-1].links.push t
		nodes[t-1].links.push s
	end
 }
}

graph = graph.keep_if {|x| x.exists==true}
size = graph.size
groups = -1
p = 0
while p<size
	while p<size and graph[p].visited
		p += 1
	end
	if p<size
	 graph[p].visit nodes
	 groups += 1
	end
end

#puts "z_list: #{zero_list}"
#puts "z: #{zeros}"
if groups==-1
	puts zeros
else
	puts groups+zeros
end

n, m = gets.split.map(&:to_i)
$list = Array.new(n)
class Emp
  attr_reader :lang, :group, :edge
  def initialize(lang)
    @lang = lang
    @group = nil
	@edge = Array.new
  end #def init

  def edgeplus(n)
    @edge << n
  end #def edge

  def search(g)
    @group = g
    for i in @edge
	  if $list[i].group == nil
	    $list[i].search(g)
	  end
	end
  end #def search
end #class

for i in 0...n
  lang = gets.split.map(&:to_i).drop(1)
  $list[i] = Emp.new(lang)
  for j in 0...i
    if ($list[i].lang & $list[j].lang).count > 0
	  $list[j].edgeplus(i)
	  $list[i].edgeplus(j)
	end
  end
end

g = 0
for i in 0...n
  if $list[i].group == nil
    $list[i].search(g)
	g += 1
  end
end

puts g-1

#!/usr/bin/ruby1.9

n, m = STDIN.readline.split.collect {|s| s.to_i }
seen = (m+1).times.collect { false }
connected = (m+1).times.collect { {} }

mute_count = 0

n.times do
	as = STDIN.readline.split.collect {|s| s.to_i }
	count = as.shift
	if count == 0
		mute_count += 1
		next
	end
	as.length.times do |i|
		u = as[i]
		seen[u] = true
		(i..as.length-1).each do |j|
			v = as[j]
			connected[u][v] = true
			connected[v][u] = true
		end
	end
end

(1..m).each {|u| puts '%d: %s' % [u, connected[u].keys] }

marked = (m+1).times.collect { false }
component_count = 0

def explore(u, marked, connected)
	marked[u] = true
	connected[u].keys.each do |v|
		next if marked[v]
		explore(v, marked, connected)
	end
end

(1..m).each do |start|
	next if marked[start] or not seen[start]
	component_count += 1
	explore(start, marked, connected)
end

puts '%d components, %d mute' % [component_count, mute_count]
puts component_count-1 + mute_count

#!/usr/bin/ruby1.9

n, m = STDIN.readline.split.collect {|s| s.to_i }
seen = (m+1).times.collect { false }
connected = (m+1).times.collect { {} }

mute_count = 0

n.times do
	as = STDIN.readline.split.collect {|s| s.to_i }
	count = as.shift
	if count == 0
		mute_count += 1
		next
	end
	as.length.times do |i|
		u = as[i]
		seen[u] = true
		(i..as.length-1).each do |j|
			v = as[j]
			connected[u][v] = true
			connected[v][u] = true
		end
	end
end

marked = (m+1).times.collect { false }
component_count = 0

def explore(u, marked, connected)
	marked[u] = true
	connected[u].keys.each do |v|
		next if marked[v]
		explore(v, marked, connected)
	end
end

(1..m).each do |start|
	next if marked[start] or not seen[start]
	component_count += 1
	explore(start, marked, connected)
end

puts component_count-1 + mute_count

n,m=gets.split.map(&:to_i)
a=[]
n.times do
    k=0
    gets.split.map(&:to_i)[1..-1].each do |v|
        k+=(1<<(v-1))
    end
    a<<k
end

changed=true
while changed do
    changed=false
    for i in 0..n-1 do
        if a[i]>=0 then
            for j in i+1..n-1 do
                if a[j]>=0 and (a[i]&a[j])>0 then
                    a[i]|=a[j]
                    a[j]=-1
                    changed=true
                end
            end
        end
    end
end

res=0
a.each do |v|
    res+=1 if v>=0
end
puts res==n ? n : res-1

n,m=gets.split.map(&:to_i)
a=[]
b=[]
n.times do
    k=0
    gets.split.map(&:to_i)[1..-1].each do |v|
        k+=(1<<(v-1))
    end
    a<<k
    b<<1
end

changed=true
while changed do
    changed=false
    for i in 0..n-1 do
        if b[i]>0 then
            for j in i+1..n-1 do
                if b[j]>0 and (a[i]&a[j])>0 then
                    b[j]=0
                    a[i]|=a[j]
                    a[j]=a[i]
                    changed=true
                    break
                end
                break if changed
            end
        end
        break if changed
    end
end

res=0
b.each do |v|
    res+=v
end
puts res-1

n,m=gets.split.map(&:to_i)
a=[]
n.times do
    k=0
    gets.split.map(&:to_i)[1..-1].each do |v|
        k+=(1<<(v-1))
    end
    a<<k
end

changed=true
while changed do
    changed=false
    for i in 0..n-1 do
        if a[i]>=0 then
            for j in i+1..n-1 do
                if a[j]>=0 and (a[i]&a[j])>0 then
                    a[i]|=a[j]
                    a[j]=-1
                    changed=true
                end
            end
        end
    end
end

res=0
a.each do |v|
    res+=1 if v>=0
end
puts res-1

n, m = gets.split(' ').collect!{|i| i.to_i}

k = []
n.times do |j|
	k[j] = gets.split(' ').collect!{|i| i.to_i}
end

langs = Array.new(101, 0)
needtolearn = 0
knownlangs = []

k.each do |sot|
	if sot[0] > 0
		(1..sot[0]).each do |i|
			langs[sot[i]] += 1

			knownlangs << sot[i] if !knownlangs.include?(sot[i])
		end
	else
		needtolearn += 1
	end
end

lanmap = {}

langs.each_index do |lan|
	# needtolearn += 1 if lan == 1

	if langs[lan] > 0
		lanmap[lan] = {} if !lanmap[lan]
		knownlangs.each do |kn|
			lanmap[lan][kn] = false if kn != lan
		end
	end
end

knownlangs.each do |kn|
	mylangs = [kn]
	k.each do |sot|
		if sot[0] > 0
			(1..sot[0]).each do |i|
				mylangs << sot[i] if sot[1..-1].include?(kn) && !mylangs.include?(sot[i])

				# lanmap[kn][sot[i]] = true if sot[1..-1].include?(kn)
			end
		end
	end

	mylangs.each do |ln1|
		mylangs.each do |ln2|
			lanmap[ln1][ln2] = true
		end
	end
end

allok = false
learning = 0

while !allok
	knownlangs[1..-1].each do |lan2|
		if lanmap[knownlangs[0]][lan2] == false
			p lan2
			learning += 1
			lanmap[lan2].each_key do |lan3|
				lanmap[knownlangs[0]][lan3] = true if lanmap[lan2][lan3]
			end
		end
		allok = true if lan2 == knownlangs[-1]
	end
end

p lanmap

n, m = gets.split(' ').collect!{|i| i.to_i}

k = []
n.times do |j|
	k[j] = gets.split(' ').collect!{|i| i.to_i}
end

langs = Array.new(101, 0)
needtolearn = 0

@mygroups = []

def checkinclude(int)
	@mygroups.each_index do |i|
		return i if @mygroups[i].include?(int)
	end
	return false
end

k.each do |line|
	if line[0] == 0
		needtolearn += 1
	else
		found = false
		line[1..-1].each do |lang|
			if grn = checkinclude(lang)
				# p grn
				# p @mygroups[grn].to_s
				# p line[1..-1].to_s
				@mygroups[grn] = @mygroups[grn] | line[1..-1]
				found = true
				break
			end
		end
		if !found
			@mygroups << line[1..-1]
		end
	end
end

size = @mygroups.size
size.times do |i|
	size.times do |j|
		if i != j && !(@mygroups[i] & @mygroups[j]).empty?
			@mygroups[i] = @mygroups[i] | @mygroups[j]
			@mygroups[j] = []
		end
	end
end

@mygroups.delete_if {|x| x.empty? }

p needtolearn + (@mygroups.size - 1)
# p needtolearn
# p @mygroups.to_s

# Warning: This code sucks.

require "set"

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

aijs = n.times.map do
  gets.split.map(&:to_i).drop(1)
end

groups = []
aijs.each_with_index do |ai, i|
  groups[i] = Set.new ai
end

know_nothing = groups.select(&:empty?).count

loop do
  new = []
  0.upto(groups.size - 1) do |i|
    found = false
    0.upto(new.size - 1) do |j|
      if (groups[i] & new[j]).any?
        new[j] += groups[i]
        found = true
        break
      end
    end
    if !found
      new << groups[i]
    end
  end
  break if new == groups
  groups = new
end

ans = groups.count - 1
if know_nothing > 0
  ans += 1
end
p ans

# Warning: This code sucks.

require "set"

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

aijs = n.times.map do
  gets.split.map(&:to_i).drop(1)
end

groups = []
aijs.each_with_index do |ai, i|
  groups[i] = Set.new ai
end

know_nothing = groups.select(&:empty?).count

loop do
  new = []
  0.upto(groups.size - 1) do |i|
    found = false
    0.upto(new.size - 1) do |j|
      if (groups[i] & new[j]).any?
        new[j] += groups[i]
        found = true
        break
      end
    end
    if !found
      new << groups[i]
    end
  end
  break if new == groups
  groups = new
end

ans = groups.count - 1
if know_nothing > 1
  ans += 1
end
p ans

# Warning: This code sucks.

require "set"

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

aijs = n.times.map do
  gets.split.map(&:to_i).drop(1)
end

groups = []
aijs.each_with_index do |ai, i|
  groups[i] = Set.new ai
end

loop do
  new = []
  0.upto(groups.size - 1) do |i|
    found = false
    0.upto(new.size - 1) do |j|
      if (groups[i] & new[j]).any?
        new[j] += groups[i]
        found = true
        break
      end
    end
    if !found
      new << groups[i]
    end
  end
  break if new == groups
  groups = new
end

p groups.count - 1

class Node
	attr_accessor :id, :visited, :links
	def initialize id
		@id = id
		@visited = false
		@links = []
	end
	def visit nodes
		if !@visited
			@visited = true
			@links.each { |l|
				nodes[l-1].visit nodes
			}
		end
	end
end

n,m = gets.split(" ").collect! {|x| x.to_i}
nodes = [0]*n
(1..n).each {|v|
	nodes[v-1] = Node.new v
}
zero_list = [0]*m
zeros = 0
arrs = []
(1..n).each {
arr = gets.split(" ").collect! {|x| x.to_i}
arr = arr[1..-1]
if arr.size == 0
	zeros += 1
end
arrs.push arr
}

(1..n).each { |s|
 (s+1..n).each { |t|
	if (arrs[s-1]&arrs[t-1]).size>0
		nodes[s-1].links.push t
		nodes[t-1].links.push s
	end
 }
}

groups = -1
p = 0
while p<n
	while p<n and nodes[p].visited
		p += 1
	end
	if p<n
	 nodes[p].visit nodes
	 groups += 1
	end
end

#puts "z_list: #{zero_list}"
#puts "z: #{zeros}"
puts groups

class Node
	attr_accessor :id, :visited, :links
	def initialize id
		@id = id
		@visited = false
		@links = []
	end
	def visit nodes
		if !@visited
			@visited = true
			@links.each { |l|
				nodes[l-1].visit nodes
			}
		end
	end
end

n,m = gets.split(" ").collect! {|x| x.to_i}
nodes = [0]*m
(1..m).each {|v|
	nodes[v-1] = Node.new v
}
zero_list = [0]*m
zeros = 0
(1..n).each {
arr = gets.split(" ").collect! {|x| x.to_i}
arr = arr[1..-1]
if arr.size == 0
	zeros += 1
end
arr.each { |s|
	arr.each { |t|
		zero_list[s-1]=1
		zero_list[t-1]=1
		if s!=t

			if !nodes[s-1].links.include? t
				nodes[s-1].links.push t
				nodes[t-1].links.push s
			end
		end
	}
}
}

groups = 0
p = 0
while p<m
	while p<m and nodes[p].visited
		p += 1
	end
	if p<m
	 nodes[p].visit nodes
	 groups += 1
	end
end

sum = m
zero_list.each { |v|
	sum -= v
}

#puts "z_list: #{zero_list}"
#puts "z: #{zeros}"
puts zeros+groups-1-sum


n = gets.to_i
M = [0]*10000
F = [0]*10000
n.times do
  a = gets.split(" ")
  gen = a.shift
  q, w = a.map(&:to_i)
  ary = eval gen
  (q..w).each {|d| ary[d] += 1 }
end

p 2*(1..366).map{|i| [M[i], F[i]].min }.max

n = gets.chomp.to_i
males = Array.new(367) { 0 }
females = Array.new(367) { 0 }
n.times do
  g, a, b = gets.chomp.split.map { |e| e.match(/\d/) ? e.to_i : e }
  (a..b).each { |i| g == 'M' ? males[i] += 1 : females[i] += 1 }
end
ans = 0
(0..367).each do |i|
  ans = [ans, [males[i], females[i]].map(&:to_i).min * 2].max
end
puts ans

n = gets.to_i
md = Array.new(370, 0)
fd = Array.new(370, 0)

n.times do
  g, s, t = gets.split
  s = s.to_i
  t = t.to_i
  if g == 'M'
    for i in s..t
      md[i] += 1
    end
  else
    for i in s..t
      fd[i] += 1
    end
  end
end
ans = 0

370.times do |i|
  if [md[i], fd[i]].min > ans
    ans = [md[i], fd[i]].min
  end
end
puts ans*2

n = gets.chomp.to_i
gs = []
as = []
bs = []
n.times do
  g, a, b = gets.chomp.split.map { |x| x =~ /[MF]/ ? x : x.to_i }
  gs << g
  as << a
  bs << b
end

ans = 0
for d in 1..366
  m = 0
  f = 0
  for i in 0...n
    if gs[i] == 'M'
      m += 1 if as[i] <= d && d <= bs[i]
    else
      f += 1 if as[i] <= d && d <= bs[i]
    end
  end
  ans = [ans, [m, f].min * 2].max
end

puts ans

n = gets.to_i
ms = []
fs = []
for i in 0...n
  g, s, e = gets.split(' ')
  s = s.to_i
  e = e.to_i

  for j in s..e
    if g == 'M'
      ms[j] ||= 0
      ms[j] += 1
    else
      fs[j] ||= 0
      fs[j] += 1
    end
  end
end

maxatt = 0
maxi = 0
for i in 1..366
  att = [ms[i].to_i, fs[i].to_i].min
  if att > maxatt
    maxatt = att
    maxi = i
  end
end

puts maxatt*2

n = gets.to_i
a = Array.new(367)
n.times do
  g, b, e = gets.split
  (b.to_i..e.to_i).each do |i|
    a[i] ||= []
    a[i] << g
  end
end
b = a.map do |suba|
  if suba.nil?
    0
  else
    f, m = 0, 0
    suba.each do |x|
      if x == 'F'
        f += 1
      elsif x == 'M'
        m += 1
      end
    end
    [f, m].min
  end
end
puts b.max * 2


class MemManager

  EMPTY = 0

  def initialize(size)
    @size = size
    @mem = Array.new(size, EMPTY)
    @id = 1
    @used_ids = {}
  end

  def get_mem
    @mem.to_s
  end

  # returns block id or 0 if cannot alloc block
  def alloc(len)
    ind = find_empty_block(len)
    if ind >= 0
      fill_block(ind, len, @id)
      @used_ids[@id] = [ind, len]
      @id += 1
      return @id - 1
    end
    0
  end

  def erase(id)
    if @used_ids[id]
      ind, len = @used_ids[id]
      fill_block(ind, len, EMPTY)
      @used_ids.delete(id)
      #p @used_ids
      return true
    end
    false
  end

  def defrag
    # get keys in order they are in mem
    keys = @mem.uniq
    keys.delete(0)
    # move 0-s to the end of @mem
    @mem = @mem.delete_if {|x| x == 0}
    @mem << 0 while @mem.size < @size
    # relocate @used_ids
    cur_ind = 0
    keys.each do |id|
       ind, len = @used_ids[id]
       ind = cur_ind
       cur_ind += len
       @used_ids[id] = [ind, len]
    end
  end

private

  # returns -1 if cannot find empty block of length n
  def find_empty_block(n)
    start = 0
    while start <= @size - n
      if @mem[start] != EMPTY
        start += 1
        next
      end
      i = 0
      while @mem[start + i] == EMPTY
        i += 1
      end
      return start if i >= n
      start += i
    end
    -1
  end

  def fill_block(ind, len, value)
    @mem.fill(value, ind, len)
  end
end

if __FILE__ == $0
  t, m = gets.split.collect {|x| Integer(x)}
  mm = MemManager.new(m)
  while line = gets
    line.chomp!
    if line.start_with?('alloc')
      a = line.scan(/\d+/)
      n = Integer(a[0])
      id = mm.alloc(n)
      puts (id == 0) ? "NULL" : id
    elsif line.start_with?('erase')
      a = line.scan(/\d+/)
      n = Integer(a[0])
      puts "ILLEGAL_ERASE_ARGUMENT" unless mm.erase(n)
    elsif line.start_with?('defragment')
      mm.defrag
    end
  end
end

input = gets.chomp.split.map do |x|
    Integer(x)
end

n = input[0]
m = input[1]

memory = Array.new(m) { |i| 0 }
next_id = 1

def find_and_fill_block(memory, len, next_id)
    head = 0
    for i in 0...memory.length
        if head <= i - len
            break
        end

        if memory[i] != 0
            head = i + 1
        end
    end
    if head + len <= memory.length
        for i in 0...len
            memory[head + i] = next_id
        end
        return true
    end

    return false
end

def erase_block(memory, id)
    flag = false
    for i in 0...memory.length
        if memory[i] == id
            memory[i] = 0
            flag = true
        end
    end

    return flag
end

def defragment(memory)
    j = 0
    for i in 0...memory.length
        if memory[i] != 0
            memory[j] = memory[i]
            j += 1
        end
    end
    for i in j...memory.length
        memory[i] = 0
    end
end

for i in 0...n
    input = gets.chomp.split
    command = input[0]
    if command[0] == 'a'
        len = Integer(input[1])
        ret = find_and_fill_block(memory, len, next_id)
        if ret
            puts next_id
            next_id += 1
        else
            puts "NULL"
        end
    elsif command[0] == 'e'
        cur_id = Integer(input[1])
        ret = cur_id != 0 && erase_block(memory, cur_id)
        if !ret
            puts "ILLEGAL_ERASE_ARGUMENT"
        end
    else
        defragment(memory)
    end
end

data = gets.chomp.split
t = Integer(data[0])
w = Integer(data[1])
task = []
for i in 0...t
    task[i] = gets.chomp
end

memory = []
for i in 0...w
    memory[i] = 0
end

def getInt(string)
    string = string.split
    integer = Integer(string[1])
    return integer
end
def allocate(amount, array)
    success = false
    endIndex = 0
    count = 0
    if amount == 1
        for i in 0...array.length
            if array[i] == 0
                endIndex = i - 1
                success = true
                break
            end
        end
    else
        if array.length > 1
            for i in 0...array.length - 1
                if array[i] == array[i + 1] && array[i] == 0
                    count += 1
                else
                    if count + 1 >= amount
                        endIndex = i
                        success = true
                        break
                    else
                        count = 0
                    end
                end
                if count + 1 >= amount
                    endIndex = i
                    success = true
                    break
                end
            end
        else
            if amount == 1 && array.length == 1
                if array[0] == 0
                    array[0] = 1
                    success = true
                    endIndex = -1
                end
            end
        end
    end
    if success
        return endIndex
    else
        return "NULL"
    end
end

def erase(identifier, array)
    for i in 0...array.length
        if array[i] == identifier
            array[i] = 0
        end
    end
    return array
end

def defragment(array)
    count = 0
    i = 0
    while i <= array.length
        if array[i] == 0
            array.delete_at(i)
            count += 1
        else
            i += 1
        end
    end
    count.times{ array.push(0)}
    return array
end

cast = Hash.new
key = 0
for i in 0...t
    if task[i][0] == "a"
        int = getInt(task[i])
        endIndex = allocate(int, memory)
        if endIndex == "NULL"
            puts "NULL"
        else
            key += 1
            cast[key] = int
            for i in endIndex - int + 2..endIndex + 1
                memory[i] = key
            end
            puts key
        end
    elsif task[i][0] == "e"
        int = getInt(task[i])
        if int <= key && int > 0
            value = cast[int]
            if value != nil
                memory = erase(int, memory)
                cast.delete(int)
            else
                puts "ILLEGAL_ERASE_ARGUMENT"
            end
        else
            puts "ILLEGAL_ERASE_ARGUMENT"
        end
    else
        memory = defragment(memory)
    end
end

n, m = gets.split.map(&:to_i)
identifiers = {}
memory_blocks = {}
identifier = 1

n.times do
	command = gets.split(' ')
	case command[0]
		when "alloc"
			start_index = 0
			block_size = command[1].to_i
			fit = false
			while memory_blocks[start_index]
				start_index += memory_blocks[start_index]
			end
			while start_index + block_size <= m
				break if block_size == m && !memory_blocks.empty?
				check = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }
				if check.empty?
					memory_blocks[start_index] = block_size
					identifiers[identifier] = start_index
					puts identifier
					identifier += 1
					fit = true
					# puts "memory_blocks is #{memory_blocks}"
					# puts "identifiers is #{identifiers}"
					break
				else
					next_index = check.min
					start_index = next_index[0] + memory_blocks[next_index[0]]
				end
			end
			puts "NULL" unless fit
		when "erase"
			d_iden = command[1].to_i
			iden_index = identifiers[d_iden]
			if iden_index.nil?
				puts "ILLEGAL_ERASE_ARGUMENT"
			else
				identifiers.delete(d_iden)
				memory_blocks.delete(iden_index)
				# puts "memory_blocks is #{memory_blocks}"
				# puts "identifiers is #{identifiers}"
			end
		else # defragment
			start_index = 0
			copy = {}
			memory_blocks.size.times do
				lowest = memory_blocks.min
				copy[start_index] = lowest[1]
				identifiers[identifiers.key(lowest[0])] = start_index
				start_index += lowest[1]
				memory_blocks.delete(lowest[0])
			end
			memory_blocks = copy
			# puts "memory_blocks is #{memory_blocks}"
			# puts "identifiers is #{identifiers}"
		end
end

input = gets.chomp.split.map do |x|
    Integer(x)
end

n = input[0]
m = input[1]

memory = Array.new(m) { |i| 0 }
next_id = 1

def find_and_fill_block(memory, len, next_id)
    head = 0
    for i in 0...memory.length
        if head <= i - len
            break
        end

        if memory[i] != 0
            head = i + 1
        end
    end
    if head + len <= memory.length
        for i in 0...len
            memory[head + i] = next_id
        end
        return true
    end

    return false
end

def erase_block(memory, id)
    flag = false
    for i in 0...memory.length
        if memory[i] == id
            memory[i] = 0
            flag = true
        end
    end

    return flag
end

def defragment(memory)
    j = 0
    for i in 0...memory.length
        if memory[i] != 0
            memory[j] = memory[i]
            j += 1
        end
    end
    for i in j...memory.length
        memory[i] = 0
    end
end

for i in 0...n
    input = gets.chomp.split
    command = input[0]
    if command[0] == 'a'
        len = Integer(input[1])
        ret = find_and_fill_block(memory, len, next_id)
        if ret
            puts next_id
            next_id += 1
        else
            puts "NULL"
        end
    elsif command[0] == 'e'
        cur_id = Integer(input[1])
        ret = erase_block(memory, cur_id)
        if !ret
            puts "ILLEGAL_ERASE_ARGUMENT"
        end
    else
        defragment(memory)
    end
    puts memory
end

input = gets.chomp.split.map do |x|
    Integer(x)
end

n = input[0]
m = input[1]

memory = Array.new(m) { |i| 0 }
next_id = 1

def find_and_fill_block(memory, len, next_id)
    head = 0
    for i in 0...memory.length
        if head <= i - len
            break
        end

        if memory[i] != 0
            head = i + 1
        end
    end
    if head + len <= memory.length
        for i in 0...len
            memory[head + i] = next_id
        end
        return true
    end

    return false
end

def erase_block(memory, id)
    flag = false
    for i in 0...memory.length
        if memory[i] == id
            memory[i] = 0
            flag = true
        end
    end

    return flag
end

def defragment(memory)
    j = 0
    for i in 0...memory.length
        if memory[i] != 0
            memory[j] = memory[i]
            j += 1
        end
    end
    for i in j...memory.length
        memory[i] = 0
    end
end

for i in 0...n
    input = gets.chomp.split
    command = input[0]
    if command[0] == 'a'
        len = Integer(input[1])
        ret = find_and_fill_block(memory, len, next_id)
        if ret
            puts next_id
            next_id += 1
        else
            puts "NULL"
        end
    elsif command[0] == 'e'
        cur_id = Integer(input[1])
        ret = erase_block(memory, cur_id)
        if !ret
            puts "ILLEGAL_ERASE_ARGUMENT"
        end
    else
        defragment(memory)
    end
end

data = gets.chomp.split
t = Integer(data[0])
w = Integer(data[1])
task = []
for i in 0...t
    task[i] = []
    task[i] = gets.chomp
end

memory = []
for i in 0...w
    memory[i] = 0
end

def allocate(amount, array)
    success = false
    endIndex = 0
    count = 0
    for i in 0...array.length - 1
        if array[i] == array[i + 1] && array[i] == 0
            count += 1
        else
            if count + 1 >= amount
                endIndex = i
                success = true
                break
            else
                count = 0
            end
        end
        if count + 1 >= amount
            endIndex = i
            success = true
            break
        end
    end
    if success
        return endIndex
    else
        return "NULL"
    end
end

def erase(identifier, array)
    for i in 0...array.length
        if array[i] == identifier
            array[i] = 0
        end
    end
    return array
end

def defragment(array)
    count = 0
    i = 0
    while i <= array.length
        if array[i] == 0
            array.delete_at(i)
            count += 1
        else
            i += 1
        end
    end
    count.times{ array.push(0)}
    return array
end

cast = Hash.new
key = 0
for i in 0...t
    if task[i][0] == "a"
        amount = Integer(task[i][task[i].length - 1])
        endIndex = allocate(amount, memory)
        if endIndex == "NULL"
            puts "NULL"
        else
            key += 1
            cast[key] = amount
            for i in endIndex - amount + 2..endIndex + 1
                memory[i] = key
            end
            puts key
        end
    elsif task[i][0] == "e"
        identifier = Integer(task[i][task[i].length - 1])
        if identifier <= key
            memory = erase(identifier, memory)
            cast[identifier] = -1
        else
            puts "ILLEGAL_ERASE_ARGUMENT"
        end
    else
        memory = defragment(memory)
    end
end

data = gets.chomp.split
t = Integer(data[0])
w = Integer(data[1])
task = []
for i in 0...t
    task[i] = []
    task[i] = gets.chomp
end

memory = []
for i in 0...w
    memory[i] = 0
end

def allocate(amount, array)
    success = false
    endIndex = 0
    count = 0
    if array.length > 1
        for i in 0...array.length - 1
            if array[i] == array[i + 1] && array[i] == 0
                count += 1
            else
                if count + 1 >= amount
                    endIndex = i
                    success = true
                    break
                else
                    count = 0
                end
            end
            if count + 1 >= amount
                endIndex = i
                success = true
                break
            end
        end
    else
        if amount == 1 && array.length == 1
            if array[0] == 0
                array[0] = 1
                success = true
                endIndex = -1
            end
        end
    end
    if success
        return endIndex
    else
        return "NULL"
    end
end

def erase(identifier, array)
    for i in 0...array.length
        if array[i] == identifier
            array[i] = 0
        end
    end
    return array
end

def defragment(array)
    count = 0
    i = 0
    while i <= array.length
        if array[i] == 0
            array.delete_at(i)
            count += 1
        else
            i += 1
        end
    end
    count.times{ array.push(0)}
    return array
end

cast = Hash.new
key = 0
for i in 0...t
    if task[i][0] == "a"
        amount = Integer(task[i][task[i].length - 1])
        endIndex = allocate(amount, memory)
        if endIndex == "NULL"
            puts "NULL"
        else
            key += 1
            cast[key] = amount
            for i in endIndex - amount + 2..endIndex + 1
                memory[i] = key
            end
            puts key
        end
    elsif task[i][0] == "e"
        negetive = task[i][task[i].length - 2]
        if negetive == "-"
            identifier = 0 - Integer(task[i][task[i].length - 1])
        else
            identifier = Integer(task[i][task[i].length - 1])
        end
        if identifier <= key && identifier > 0
            value = cast[identifier]
            if value != nil
                memory = erase(identifier, memory)
                cast.delete(identifier)
            else
                puts "ILLEGAL_ERASE_ARGUMENT"
            end
        else
            puts "ILLEGAL_ERASE_ARGUMENT"
        end
    else
        memory = defragment(memory)
    end
end

n, m = gets.split.map(&:to_i)
identifiers = {}
memory_blocks = {}
start_index = 0
identifier = 1

n.times do
	command = gets.split(' ')
	case command[0]
		when "alloc"
			block_size = command[1].to_i
			fit = false
			while memory_blocks[start_index]
				start_index += memory_blocks[start_index]
			end
			while start_index + block_size <= m
				break if block_size == m && !memory_blocks.empty?
				check = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }
				if check.empty?
					memory_blocks[start_index] = block_size
					identifiers[identifier] = start_index
					puts identifier
					identifier += 1
					start_index += block_size
					fit = true
					break
				else
					next_index = check.min
					start_index = next_index[0] + memory_blocks[next_index[0]]
				end
			end
			puts "NULL" unless fit
		when "erase"
			d_iden = command[1].to_i
			iden_index = identifiers[d_iden]
			if iden_index.nil?
				puts "ILLEGAL_ERASE_ARGUMENT"
			else
				identifiers.delete(d_iden)
				memory_blocks.delete(iden_index)
				start_index = iden_index
			end
		else # defragment
			start_index = 0
			copy = {}
			memory_blocks.size.times do
				lowest = memory_blocks.min
				copy[start_index] = lowest[1]
				identifiers[identifiers.key(lowest[0])] = start_index
				start_index += lowest[1]
				memory_blocks.delete(lowest[0])
			end
			memory_blocks = copy
		end
end

n, m = gets.split.map(&:to_i)
identifiers = {}
memory_blocks = {}
start_index = 0
identifier = 1

n.times do
	command = gets.split(' ')
	case command[0]
		when "alloc"
			block_size = command[1].to_i
			fit = false
			while memory_blocks[start_index]
				start_index += memory_blocks[start_index]
			end
			while start_index + block_size <= m
				break if block_size == m && !memory_blocks.empty?
				check = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }
				if check.empty?
					memory_blocks[start_index] = block_size
					identifiers[identifier] = start_index
					puts identifier
					identifier += 1
					start_index += block_size
					fit = true
					puts "memory_blocks is #{memory_blocks}"
					puts "identifiers is #{identifiers}"
					break
				else
					next_index = check.min
					start_index = next_index[1] + memory_blocks[next_index[0]]
				end
			end
			puts "NULL" unless fit
		when "erase"
			d_iden = command[1].to_i
			iden_index = identifiers[d_iden]
			if iden_index.nil?
				puts "ILLEGAL_ERASE_ARGUMENT"
			else
				identifiers.delete(d_iden)
				memory_blocks.delete(iden_index)
				start_index = iden_index
				puts "start_index is #{start_index}"
				puts "memory_blocks is #{memory_blocks}"
				puts "identifiers is #{identifiers}"
			end
		else # defragment
			start_index = 0
			copy = {}
			memory_blocks.size.times do
				lowest = memory_blocks.min
				copy[start_index] = lowest[1]
				identifiers[identifiers.key(lowest[0])] = start_index
				start_index += lowest[1]
				memory_blocks.delete(lowest[0])
			end
			memory_blocks = copy
			puts "memory_blocks is #{memory_blocks}"
			puts "identifiers is #{identifiers}"
		end
end

n, m = gets.split.map(&:to_i)
identifiers = {}
memory_blocks = {}
start_index = 0
identifier = 1

n.times do
	command = gets.split(' ')
	case command[0]
		when "alloc"
			block_size = command[1].to_i
			fit = false
			while memory_blocks[start_index]
				start_index += memory_blocks[start_index]
			end
			while start_index + block_size <= m
				break if block_size == m && !memory_blocks.empty?
				check = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }
				if check.empty?
					memory_blocks[start_index] = block_size
					identifiers[identifier] = start_index
					puts identifier
					identifier += 1
					start_index += block_size
					fit = true
					break
				else
					next_index = check.min
					start_index = next_index[1] + memory_blocks[next_index[0]]
				end
			end
			puts "NULL" unless fit
		when "erase"
			d_iden = command[1].to_i
			iden_index = identifiers[d_iden]
			if iden_index.nil?
				puts "ILLEGAL_ERASE_ARGUMENT"
			else
				identifiers.delete(d_iden)
				memory_blocks.delete(iden_index)
				start_index = iden_index
			end
		else # defragment
			start_index = 0
			copy = {}
			memory_blocks.size.times do
				lowest = memory_blocks.min
				copy[start_index] = lowest[1]
				identifiers[identifiers.key(lowest[0])] = start_index
				start_index += lowest[1]
				memory_blocks.delete(lowest[0])
			end
			memory_blocks = copy
		end
end

n, m = gets.split.map(&:to_i)
identifiers = {}
memory_blocks = {}
start_index = 0
identifier = 1

n.times do
	command = gets.split(' ')
	case command[0]
		when "alloc"
			block_size = command[1].to_i
			fit = false
			while memory_blocks[start_index]
				start_index += memory_blocks[start_index]
			end
			while start_index + block_size <= m
				check = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }
				if check.empty?
					memory_blocks[start_index] = block_size
					identifiers[identifier] = start_index
					puts identifier
					identifier += 1
					start_index += block_size
					fit = true
					break
				else
					next_index = check.min
					start_index = next_index[1] + memory_blocks[next_index[0]]
				end
			end
			puts "NULL" unless fit
		when "erase"
			d_iden = command[1].to_i
			iden_index = identifiers[d_iden]
			if iden_index.nil?
				puts "ILLEGAL_ERASE_ARGUMENT"
			else
				identifiers.delete(d_iden)
				memory_blocks.delete(iden_index)
				start_index = iden_index
			end
		else # defragment
			start_index = 0
			copy = memory_blocks
			memory_blocks.size.times do
				lowest = copy.min
				memory_blocks[start_index] = lowest[1]
				identifiers[identifiers.key(lowest[0])] = start_index
				start_index += lowest[1]
				copy.delete(lowest[0])
				memory_blocks.delete(lowest[0])
			end
		end
end

n, m = gets.split.map(&:to_i)
identifiers = {}
memory_blocks = {}
identifier = 1

n.times do
	command = gets.split(' ')
	case command[0]
		when "alloc"
			start_index = 0
			block_size = command[1].to_i
			fit = false
			while memory_blocks[start_index]
				start_index += memory_blocks[start_index]
			end
			while start_index + block_size <= m
				break if block_size == m && !memory_blocks.empty?
				check = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }
				if check.empty?
					memory_blocks[start_index] = block_size
					identifiers[identifier] = start_index
					puts identifier
					identifier += 1
					fit = true
					puts "memory_blocks is #{memory_blocks}"
					puts "identifiers is #{identifiers}"
					break
				else
					next_index = check.min
					start_index = next_index[0] + memory_blocks[next_index[0]]
				end
			end
			puts "NULL" unless fit
		when "erase"
			d_iden = command[1].to_i
			iden_index = identifiers[d_iden]
			if iden_index.nil?
				puts "ILLEGAL_ERASE_ARGUMENT"
			else
				identifiers.delete(d_iden)
				memory_blocks.delete(iden_index)
				puts "memory_blocks is #{memory_blocks}"
				puts "identifiers is #{identifiers}"
			end
		else # defragment
			start_index = 0
			copy = {}
			memory_blocks.size.times do
				lowest = memory_blocks.min
				copy[start_index] = lowest[1]
				identifiers[identifiers.key(lowest[0])] = start_index
				start_index += lowest[1]
				memory_blocks.delete(lowest[0])
			end
			memory_blocks = copy
			puts "memory_blocks is #{memory_blocks}"
			puts "identifiers is #{identifiers}"
		end
end


t = gets.to_i
t.times { |u|
  n = gets.to_i
  arr = gets.chomp.split(" ").map(&:to_i)
  even = 0
  odd = 0
  arr.each { |x|
    if x % 2 == 0
      even += 1
    else
      odd += 1
    end
  }
  if even == odd
    puts "Yes"
  else
    puts "No"
  end
}

#!/usr/bin/env ruby

ncases = gets.chomp.to_i

ncases.times do
  n = gets.chomp.to_i
  numbers = gets.chomp.split(' ').map { |str| str.to_i }
  answer = numbers.map { |n| n % 2 == 0 }.filter { |b| b }.size == n
  puts answer ? 'Yes' : 'No'
end

def possible?(arr)
  arr.filter { |x| x % 2 == 1}.size == arr.size / 2
end

t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  inp = gets.chomp.split(' ').map(&:to_i)
  possible?(inp) ? puts("Yes\n") : puts("No\n")
end

gets
while gets
  a = gets.split.map(&:to_i)
  puts a.count(&:odd?) == a.size / 2 ? 'Yes' : 'No'
end

a = gets.to_i
a.times{
 n = gets.to_i
 arr = gets.split.map &:to_i
 b = arr.select{_1.odd?}
 c = arr - b
 if b.size == n && c.size == n then puts "Yes"
 else
     puts "No"
end
}

gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i)
  c=0
  a.each do |aa|
    if aa.odd?
      c+=1
    end
  end
  if c==n
    puts "Yes"
  else
    puts "No"
  end
end


def read_integers
  STDIN.gets.split.map(&:to_i)
end

def solve array, m, k, hash
  result = []
  array.each do |x|
    rem = x%m
    hash[rem] = hash[rem] || []
    hash[rem].push(x)
    result = hash[rem] if hash[rem].length > result.length
  end
  result
end

def solution
  n, k, m = read_integers
  array = read_integers
  hash = []
  result = solve array, m, k, hash
  if result.length  < k
    print "No\n"
  else
    print "Yes\n"
    result.each_with_index do |val, index|
        print  val, ' ' if index < k
    end
  end
end

solution

def solution(k, ans_index, hash)
  puts 'Yes'
  hash[ans_index].each_with_index do |val, index|
    print "#{val} " if index < k
  end
end

def get_hash_and_index(k, m, array)
  hash = []
  ans_index = -1
  array.each do |x|
    rem = x % m
    hash[rem] = [] if hash[rem].nil?
    hash[rem].push(x)
    ans_index = rem if hash[rem].length == k
  end
  ans_index == -1 ? (puts 'No') : solution(k, ans_index, hash)
end

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

get_hash_and_index(k, m, array)

#! /usr/bin/env ruby

n, k, m = gets.split.map &:to_i
a = gets.split.map &:to_i
a = a.group_by { |i| i % m }
yes = false
a.each_key do |key|
    if a[key].size >= k
        yes = true
        puts:Yes
        puts a[key][0...k] * ' '
        break
    end
end
if not yes
    puts:No
end

def read_integers
  STDIN.gets.split.map(&:to_i)
end

def solve array, m, k
  result = []
  hash = []
  array.each do |x|
    rem = x%m
    hash[rem] = hash[rem] || []
    hash[rem].push(x)
    result = hash[rem] if hash[rem].length > result.length
  end
  result
end

def solution
  n, k, m = read_integers
  array = read_integers
  result = solve array, m, k
  if result.length  < k
    print "No\n"
  else
    print "Yes\n"
    result.each_with_index do |val, index|
        print  val, ' ' if index < k
    end
  end
end

solution

def read_integers
  STDIN.gets.split.map(&:to_i)
end

def solve array, m, k, hash
  ans_index = -1;
  array.each do |x|
    rem = x%m
    hash[rem] = hash[rem] || []
    hash[rem].push(x)
    if hash[rem].length == k
      ans_index = rem
    end
  end
  ans_index
end

def solution
  n, k, m = read_integers
  array = read_integers
  hash = []
  ans_index = solve array, m, k, hash
  if ans_index  == -1
    print "No\n"
  else
    print "Yes\n"
    hash[ans_index ].each_with_index do
      |val, index|
      if index < k
        print  val
        print ' '
      end
    end
  end
end

solution

def solution
  n, k, m = STDIN.gets.split.map(&:to_i)
  array = STDIN.gets.split.map(&:to_i)

  hash = []
  ans_index = -1;
  array.each{ |x|
    rem = x%m

    if hash[rem] == nil then
      hash[rem] = []
    end

    hash[rem].push(x)

    if hash[rem].length == k then
      ans_index = rem
    end

  }

  if ans_index  == -1 then
    puts 'No'
  else
    puts 'Yes'

    hash[ans_index ].each_with_index { |val, index|
      if index < k then
       print  val
       print ' '
      end
    }

    #puts output;
  end
end

solution

def solution
  n, k, m = STDIN.gets.split.map(&:to_i)
  array = STDIN.gets.split.map(&:to_i)

  hash = []
  ans_index = -1;
  array.each{ |x|
    rem = x%m

    if hash[rem] == nil then
      hash[rem] = []
    end

    hash[rem].push(x)

    if hash[rem].length == k then
      ans_index = rem
    end

  }

  if ans_index  == -1 then
    puts 'No'
  else
    puts 'Yes'
   # output = ""

    hash[ans_index ].each_with_index { |val, index|
      if index < k then
    #    output += val.to_s + " "
       puts val
      end
    }

    #puts output;
  end
end

solution

#! /usr/bin/env ruby

n, k, m = gets.split.map &:to_i
a = gets.split.map &:to_i
a = a.group_by { |i| i % m }
yes = false
a.each_key do |key|
    if a[key].size >= k
        yes = true
        puts:Yes
        puts a[key][0...k] * ' '
    end
end
if not yes
    puts:No
end

def solution
  n, k, m = STDIN.gets.split.map(&:to_i)
  array = STDIN.gets.split.map(&:to_i)

  hash = {}
  best = -1;
  array.each{ |x|
    rem = x%m
    if hash[rem] == nil then
      hash[rem] = []
    end

    hash[rem].push(x)
    if hash[rem].length == k then
      best = x%m
    end
  }

  if best == -1 then
    puts 'No'
  else
    puts 'yes'
    output = "";
    hash[best].each_with_index { |val, index|
      if index < k then
        output += val.to_s + " "
      end
    }
    puts output;
  end
end

solution

def solution
  n, k, m = gets.split.map(&:to_i)
  array = gets.split.map(&:to_i)

  hash = {}
  best = -1;
  array.each{ |x|
    rem = x%m
    if hash[rem] == nil then
      hash[rem] = []
    end

    hash[rem].push(x)
    if hash[rem].length == k then
      best = x%m
    end
  }

  if best == -1 then
    puts 'No'
  else
    puts 'yes'
    output = "";
    hash[best].each_with_index { |val, index|
      if index < k then
        output = output + val.to_s + " "
      end
    }
    puts output;
  end
end

solution


# your code goes here

# a = gets.chomp.to_i

# b = gets.chomp.to_i

# puts (a+b)
t = gets.chomp.to_i
for tc in 1..t
	n = gets.chomp.to_i
	a = gets.split
	b = false
	for i in 1..(n-1)
		if a[i] != a[i-1]
			b = true;
		end
	end
	if b
		puts 1
	else
		puts n
	end
end

# cook your code here
tc = gets.chomp.to_i
tc.times {
    n = gets.chomp.to_i

    arr = gets.chomp.split(' ').map(&:to_i)
    unique = 1
    index = 1
    while(index < n) do
        if arr[index] != arr[0]
            unique += 1
            break
        end
        index += 1
    end
    if arr.size() == 1 || unique >= 2
      puts 1
    else
      puts arr.size()
    end

}


gets
bars, towers = gets.split.map(&:to_i), Hash.new(0)
bars.each {|bar| towers[bar] += 1}
print towers.values.max, " ", towers.length

n = gets.to_i
hs = Hash.new 0
gets.split(' ').map(&:to_i).each{|x| hs[x]+=1}
puts "#{hs.max_by(&:last).last} #{hs.length}"

n = gets.to_i

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

kol = br.uniq.size

h = []
br.size.times do |i|
    h << br.reject{|it| it != br[i]}.size
end

h = h.sort

puts h[-1].to_s + " " + kol.to_s

n = gets.chomp.to_i
a = gets.chomp.split(/ /).map!{|x|x.to_i}
h = Hash.new
for i in 0..n-1
	if h.key?(a[i])
		h[a[i]] += 1
	else
		h[a[i]] = 1
	end
end
ha = h.to_a.sort{|a, b|
  b[1] <=> a[1]
}
puts ha[0][1].to_s + " " + h.length.to_s

gets
v=gets.split
print v.map{|x|v.count x}.max," ",v.uniq.size

#!/usr/bin/ruby
gets;
a=gets.split.map(&:to_i).sort_by{|e|e}.chunk{|e|e}.to_a
puts "#{a.map{|k,v|v.size}.max} #{a.size}"

n = gets.chomp.to_i
a = Array.new(1001).map {|x| x=0}
gets.chomp.split(" ").map {|x| x.to_i}.each { |x|
  a[x]+=1
}
puts a.sort[a.length-1].to_s << " " << a.count{|x| x!=0}.to_s

gets
a=gets.split
print a.map{|b|a.count b}.max," ",a.uniq.size

n = gets.to_i
l = gets.split(" ").map(&:to_i).sort
cnt = 1
max = 0
col = 0
last = l[0]
l.each do |el|
  if last != el
    cnt += 1
    last = el
    max = col if max < col
    col = 1
  else
    col += 1
  end
end

max = col if max < col

puts "#{max} #{cnt}"

n=gets.chomp.strip.to_i
p=gets.chomp.split(' ').map(&:to_i)
a=Array.new(1001,0)
sum=0
max=0
p.each do |x|
	if a[x]==0
		sum+=1
	end
	a[x]+=1
	if a[x]>max
		max=a[x]
	end
end
puts max.to_s + ' ' + sum.to_s

gets
a = gets.split.map(&:to_i).to_a
puts [a.map { |x| a.find_all {|y| x==y}.size }.max, a.uniq.size]*' '

a = gets
a = gets.chomp.split(' ')

r = {}
max = 0
a.each do |x|
y = x.to_sym

r[y] = (r[y]||0) + 1
max = r[y] if max < r[y]
end

puts "#{max} #{r.size}"

n = gets.to_i
l = gets.split.map(&:to_i).sort
fox = Array.new()

counter = 1
for i in 1..n
  if l[i]==l[i-1]
    counter+=1
  else
    fox.concat([counter])
    counter = 1
  end
end

puts fox.max.to_s + " " + l.uniq.size.to_s

gets
arr = gets.split.map { |x| x.to_i }.sort
print arr.count(arr.max_by { |x| arr.count(x) }), " ",arr.uniq.size

num=gets.chomp.to_i
max=0
num2=num
b=gets.chomp.split(" ")
1.upto(1000) do |i|
temp=b.count("#{i}")
num2=num2-temp
if temp>max
max= temp
end
if temp>1
num=num-temp+1
end
end
printf "#{max} "
printf "#{num}"

require 'set'
gets
x=gets.split.map{|e| e.to_i}
d=Set.new(x).size
h=Hash.new(0)
ans=0
x.each{|e|
	h[e]+=1
	ans=h[e] if ans<h[e]
}
puts [ans,d]*" "

gets;a=gets.split;print a.map{|i|a.count i}.max," ",a.uniq.size

gets
data=gets.split.map(&:to_i).sort

hs=Hash.new(0)

max_height = 0
data.each{|val|
	hs[val]+=1

	max_height = [max_height, hs[val]].max
}

puts "#{max_height} #{hs.size}"

gets
data=gets.split.map(&:to_i).sort

hs={}
hs.default = 0

max_height = 0
data.each{|val|
	hs[val]+=1

	max_height = [max_height, hs[val]].max
}

puts "#{max_height} #{hs.size}"

gets
q = Hash.new
gets.split.map do |e|
  e.to_i
  if q[e].nil?
    q[e] = 1
  else
    q[e] += 1
  end
end

puts q.values.max

puts q.size

n=gets; m=Hash.new(0)
gets.chomp.split.each { |i| m[i] += 1 }
puts m.max_by{|k,v| v}[1]; print m.length; print ' '

n=gets.chomp.to_i
s=gets.chomp.to_s
s=s.split(" ")
u=s.uniq
max=1
for i in u
	if (s.count(i)>max)
		max=s.count(i)
	end
end
print("#{max} #{u.size}\n")

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
a = gets.split.map(&:to_i) + [0] # 0 for terminator
prev = 0
towers = 0
highest_tower = 0
height = 0
a.sort.each { |i|
    if i != prev
        if i != 0
            towers += 1
            height = 1
            prev = i
        end
    else
        height += 1
        if height > highest_tower
            highest_tower = height
        end
    end
}

puts "#{highest_tower} #{towers}"

n = gets.to_i
a = gets.split(' ')[0...n].map { |s| s.to_i }
d = a.inject(Hash.new(0)) { |h, x| h[x] += 1; h }
cnt = d.size
max = d.map { |k, v| v }.max
puts "#{max} #{cnt}"

gets
mas = gets.split(" ")
puts mas.map { |s| mas.count s }.max.to_s+" "+mas.uniq.size.to_s

# http://codeforces.com/contest/37/problem/A

number = gets.chomp.to_i

bars = gets.chomp.split(" ").each {|str| [str.to_i]}

towers = []

bars.each {|bar|
  if index = towers.index {|item| item[0] == bar}
    towers[index] << bar
  else
    towers << [bar]
  end
}

towers.sort! {|a, b| a.length <=> b.length}.reverse!

puts "#{towers[0].length} #{towers.length}"

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

res = {}
blocks.each do |b|
 res[b] ||= 0
 res[b] += 1
end

puts "#{res.values.max} #{blocks.uniq.size}"

n = gets.to_i
m = gets.split
m = m.uniq.collect{|i| [i, m.find_all{|j| j == i}.size]}
print(m.max{|x, y| x[1] <=> y[1]}[1].to_s + " " + m.size.to_s)

class Euler
  mas=Array.new(size=0, obj=nil);
  n=gets.to_i;
  st=gets.to_s.split(" ");
  st.sort!;
  mx=1;
  for i in 0..st.length-1
    k=1;
    for j in i+1..st.length-1
    if (st[j]==st[i])
      k+=1;
    end
  end
  if (k>mx)
    mx=k;
  end
    end
  a=st.uniq;
  print mx.to_s+" "+a.length.to_s+"\n";
end

barNum = gets.chomp.to_i
bar = gets.chomp.split(' ').map {|item| item.to_i}
bar.sort!
total = bar.uniq.length
largest = 0
bar.each do |item|
	largest = bar.count(item) if largest < bar.count(item)
end
puts "#{largest} #{total}"

n = gets.to_i
l = gets.split(" ").map(&:to_i).sort
cnt = 1
max = 0
col = 0
last = l[0]
l.each do |el|
  if last != el
    cnt += 1
    last = el
    max = col if max < col
    col = 1
  else
    col += 1
  end
end

puts "#{max} #{cnt}"

a = gets
a = gets.chomp.split(' ')

r = {}
max = 0
a.each do |x|
y = x.to_sym
r[y] = r[y]||0 + 1
max = r[y] if max < r[y]
end

puts "#{max} #{r.size}"

n = gets.to_i
l = gets.split.map(&:to_i).sort
fox = Array.new()

counter = 1
for i in 1..n-1
  if l[i]==l[i-1]
    counter+=1
  else
    fox.concat([counter])
    counter = 1
  end
end

puts fox.max.to_s + " " + l.uniq.size.to_s

n=gets; m=Hash.new(0)
gets.chomp.split.each { |i| m[i] += 1 }
print m.length; print ' '; puts m.max_by{|k,v| v}[1]

a=gets.chomp.to_i
b=gets.chomp.split(" ")
sum=0
0.upto(a-1) do |i|
   b[i]= b[i].to_i
sum=sum+b[i]
end
r=0
if sum%2==0
    r=1
end

sum=(sum/2.0).ceil

b.sort!
b.reverse!
p=0
0.upto(a-1) do |i|
p=p+b[i].to_i

if ( p>=sum && r==0)
p=i+1
break
end
if  (p>sum && r==1)
p=i+1
break
end
end

puts "#{p}"


gets
a=gets.split.map &:to_i
g=->x{a.count{|i|i%3==x}}
p g[0]/2+[g[1],g[2]].min

n=gets.to_i
a=gets.split.map(&:to_i)
def calc(x)
        a=0
        while x>0
                a+=x%10
                x/=10
        end
        a%=3
end
cnt=[0,0,0]
a.each{|i|
        cnt[calc(i)]+=1
}
p cnt[0]/2+[cnt[1],cnt[2]].min

gets
c = Hash.new(0).tap { |h| gets.split.map(&:to_i).each { |v| h[v % 3] += 1 } }
p c[0] / 2 + [c[1], c[2]].min

N = gets.chomp.to_i
c = Hash.new(0).tap { |h| gets.split.map(&:to_i).each { |v| h[v % 3] += 1 } }
p c[0] / 2 + [c[1], c[2]].min

def min(a, b)
  a < b ? a : b
end

def digits_sum(number)
  number.to_s.chars.map { |v| v.to_i }.inject { |r, v| r + v }
end

n = gets.to_i
parts = gets.split.map { |v| digits_sum(v) % 3 }

count = [0, 0, 0]

parts.each { |p| count[p] += 1 }

puts count[0] / 2 + min(count[1], count[2])


times = gets.to_i

def resolve(binary_s, w_size)
  w = ""
  (0..w_size-1).each do |i|
    if i == 0
      w = binary_s[i..w_size-1]
   else
      binary_s[i..w_size+(i-1)].each_char.with_index {|c,index| w[index]="1" if c!=w[index]}
    end
  end
  w
end

(1..times).each do |i|
  w_size = gets.to_i
  s = gets.chomp
  puts resolve(s, w_size)
end

# cook your code here
t = gets.chomp.to_i
t.times {
    n = gets.chomp.to_i
    s = gets.chomp.split('')
    ans = ''
    i  = 0
    j = 0
    while i < n do
        ans = ans + s[j].to_s
        j += 2
        i += 1
    end
    puts ans
}

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  s = gets.chomp
  len = s.length
  arr = []
  (len-n+1).times do |i|
    arr << s[i..i+n-1]
  end
  bit = Array.new(n,"-1")
  n.times do |i|
    flg = true
    s = arr[0][i]
    (len-n+1).times do |j|
      if arr[j][i] != s
        flg = false
      end
    end
    if flg
      bit[i] = s
    else
      bit[i] = "0"
    end
  end
  str = ""
  n.times do |i|
    str = str+bit[i]
  end
  ans << str
end
ans.each do |x|
  puts x
end

gets.to_i.times do
    gets.to_i
    s = gets.split('')
    a = []
    s.each_with_index { |v, i| a << v if i.even? }
    puts a.join
end

gets.to_i.times do
    gets.to_i
    s = gets.split('')
    a = []
    s.each_with_index { |v, i| a << v if i.even? }
    a.join
end


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

balls = []
for i in 0...n
	balls <<= gets.chomp.split.map { |e| e.to_f }
end

def calcu_v v0, m0, v1, m1
	nv0 = ((m0 - m1) * v0 + 2 * m1 * v1) / (m0 + m1)
	nv1 = ((m1 - m0) * v1 + 2 * m0 * v0) / (m0 + m1)
	return nv0, nv1
end

def calcu_p(x0, v0, x1, v1)
	(x1 * v0 - x0 * v1) / (v0 - v1)
end

flags = Array.new(n) { false }
now = 0
while now < t
	next_time = t - now
	next_a = -1
	next_b = -1
	for i in 0...n
		for j in 0...i
			if (balls[i][1] - balls[j][1]).abs > 0.00001
				col_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])
				if col_time > 0 && col_time < next_time
					next_time = col_time
					next_a = i
					next_b = j
				end
			end
		end
	end
	#if next_a != -1 and next_b != -1
		#pos = (balls[next_b][0] * balls[next_a][1] - balls[next_a][0] - balls[next_b][1]) / (balls[next_a][1] - balls[next_b][1])
	#	pos = calcu_p(balls[next_a][0], balls[next_a][1], balls[next_b][0], balls[next_b][1])
	#	balls[next_a][1], balls[next_b][1] = calcu_v(balls[next_a][1], balls[next_a][2], balls[next_b][1], balls[next_b][2])
	#	balls[next_a][0] = pos
	#	balls[next_b][0] = pos
	#end

	for i in 0...n
		flags[i] = false
	end
	for i in 0...n
		for j in 0...i
			if !flags[i] && !flags[j] && (balls[i][1] - balls[j][1]).abs > 0.00001
				col_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])
				if (col_time - next_time).abs < 0.00001
					pos = calcu_p(balls[i][0], balls[i][1], balls[j][0], balls[j][1])
					balls[i][1], balls[j][1] = calcu_v(balls[i][1], balls[i][2], balls[j][1], balls[j][2])
					balls[i][0] = balls[j][0] = pos
					flags[i] = true
					flags[j] = true
				end
			end
		end
	end

	for i in 0...n
		if !flags[i]
			balls[i][0] += balls[i][1] * next_time
		end
	end
	now += next_time

	# print balls.to_s, "\n"
end

for i in 0...n
	print balls[i][0], "\n"
end

input = gets.chomp.split
n = Integer(input[0])
t = Integer(input[1])
balls = []
for i in 0...n
    balls[i] = []
    input1 = gets.chomp.split
    for j in 0...3
        balls[i][j] = Integer(input1[j])
    end
end

for i in 0...n
    balls[i][3] = i
end
balls = balls.sort_by{|a| a[0]}

def balls_state(balls, first_collide_i, first_collide_time, n)
    for i in 0...n
        balls[i][0] = balls[i][1] * first_collide_time + balls[i][0]
    end
    for i in 0...first_collide_i.length
        j = first_collide_i[i]
        v_0 = ((balls[j][2] - balls[j+1][2]) * balls[j][1] + 2 * balls[j+1][2] * balls[j+1][1]) / ((balls[j][2] + balls[j+1][2]).to_f)
        v_1 = ((balls[j+1][2] - balls[j][2]) * balls[j+1][1] + 2 * balls[j][2] * balls[j][1]) / ((balls[j][2] + balls[j+1][2]).to_f)
        balls[j][1] = v_0
        balls[j+1][1] = v_1
    end
    return balls
end

def next_collide(balls, n)
    collide_time = []
    s = 0
    v = 0
    ct = 0
    for i in 0...n-1
        s = balls[i+1][0] - balls[i][0]
        v = balls[i][1] - balls[i+1][1]
        if balls[i][1] < 0 && balls[i+1][1] < 0
            if balls[i][1] > balls[i+1][1]
                ct = s.to_f / v
                collide_time.push([i, ct])
            end
        elsif balls[i][1] > 0
            if balls[i+1][1] > 0 && balls[i][1] > balls[i+1][1]
                ct = s.to_f / v
                collide_time.push([i, ct])

            elsif balls[i+1][1] < 0
                ct = s.to_f / v
                collide_time.push([i, ct])
            end
        end
    end
    collide_time = collide_time.sort_by{|a| a[1]}
    if collide_time != []
        first_collide_i = []
        first_collide_time = collide_time[0][1]
        for i in 0...collide_time.length
            if collide_time[i][1] == first_collide_time
                first_collide_i.push(collide_time[i][0])
            end
        end
    else
        first_collide_time = 1000
        first_collide_i = []
    end
    return first_collide_time, first_collide_i
end

stop = false
while !stop do
    first_collide_time, first_collide_i = next_collide(balls, n)
    if t <= first_collide_time
        for i in 0...n
            balls[i][0] += balls[i][1] * t
        end
        result = balls.sort_by{|a| a[3]}
        for i in 0...n
            puts result[i][0]
        end
        stop = true
    else
        t -= first_collide_time
        balls = balls_state(balls, first_collide_i, first_collide_time, n)
    end
end

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

balls = []
for i in 0...n
	balls <<= gets.chomp.split.map { |e| e.to_f }
end

def calcu_v v0, m0, v1, m1
	nv0 = ((m0 - m1) * v0 + 2 * m1 * v1) / (m0 + m1)
	nv1 = ((m1 - m0) * v1 + 2 * m0 * v0) / (m0 + m1)
	return nv0, nv1
end

def calcu_p(x0, v0, x1, v1)
	(x1 * v0 - x0 * v1) / (v0 - v1)
end

now = 0
while now < t
	next_time = t - now
	next_a = -1
	next_b = -1
	for i in 0...n
		for j in 0...i
			if (balls[i][1] - balls[j][1]).abs > 0.00001
				col_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])
				if col_time > 0 && col_time < next_time
					next_time = col_time
					next_a = i
					next_b = j
				end
			end
		end
	end
	if next_a != -1 and next_b != -1
		#pos = (balls[next_b][0] * balls[next_a][1] - balls[next_a][0] - balls[next_b][1]) / (balls[next_a][1] - balls[next_b][1])
		pos = calcu_p(balls[next_a][0], balls[next_a][1], balls[next_b][0], balls[next_b][1])
		balls[next_a][1], balls[next_b][1] = calcu_v(balls[next_a][1], balls[next_a][2], balls[next_b][1], balls[next_b][2])
		balls[next_a][0] = pos
		balls[next_b][0] = pos
	end

	now += next_time
	for i in 0...n
		if i != next_a && i != next_b
			balls[i][0] += balls[i][1] * next_time
		end
	end
	# print balls.to_s, "\n"
end

for i in 0...n
	print balls[i][0], "\n"
end

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

balls = []
for i in 0...n
	balls <<= gets.chomp.split.map { |e| e.to_f }
end

def calcu_v v0, m0, v1, m1
	nv0 = ((m0 - m1) * v0 + 2 * m1 * v1) / (m0 + m1)
	nv1 = ((m1 - m0) * v1 + 2 * m0 * v0) / (m0 + m1)
	return nv0, nv1
end

def calcu_p(x0, v0, x1, v1)
	(x1 * v0 - x0 * v1) / (v0 - v1)
end

flags = Array.new(n) { false }
now = 0
while now < t
	next_time = t - now
	next_a = -1
	next_b = -1
	for i in 0...n
		for j in 0...i
			if (balls[i][1] - balls[j][1]).abs > 0.00001
				col_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])
				if col_time > 0 && col_time < next_time
					next_time = col_time
					next_a = i
					next_b = j
				end
			end
		end
	end
	#if next_a != -1 and next_b != -1
		#pos = (balls[next_b][0] * balls[next_a][1] - balls[next_a][0] - balls[next_b][1]) / (balls[next_a][1] - balls[next_b][1])
	#	pos = calcu_p(balls[next_a][0], balls[next_a][1], balls[next_b][0], balls[next_b][1])
	#	balls[next_a][1], balls[next_b][1] = calcu_v(balls[next_a][1], balls[next_a][2], balls[next_b][1], balls[next_b][2])
	#	balls[next_a][0] = pos
	#	balls[next_b][0] = pos
	#end

	for i in 0...n
		flags[i] = false
	end
	for i in 0...n
		for j in 0...i
			if !flags[i] && !flags[j] && (balls[i][1] - balls[j][1]).abs > 0.00001
				col_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])
				if (col_time - next_time).abs < 0.00001
					pos = calcu_p(balls[i][0], balls[i][1], balls[j][0], balls[j][1])
					balls[i][1], balls[j][1] = calcu_v(balls[i][1], balls[i][2], balls[j][1], balls[j][2])
					balls[i][0] = balls[j][0] = pos
					flags[i] = true
					flags[j] = true
				end
			end
		end
	end

	for i in 0...n
		if !flags[i]
			balls[i][0] += balls[i][1] * next_time
		end
	end
	now += next_time

	print balls.to_s, "\n"
end

for i in 0...n
	print balls[i][0], "\n"
end


h,l = gets.chomp.split.map(&:to_i)

depth = 1.0 * (l*l - h*h) / (2*h)
printf("%.6f\n", depth)

h,l = gets.split.map(&:to_i)
puts (l*l-h*h)/(2.0*h)