class Solve def initialize h, w, n = read_line.split.map(&.to_i) @xy = Array(Array(Int32)).new(n) { read_line.split.map(&.to_i.pred) } @graph = Array(Array({Int32, Int32})).new(h + w) { [] of {Int32, Int32} } @xy.each_with_index do |(x, y), i| @graph[x] << {h + y, i} @graph[h + y] << {x, i} end @cycle = [] of Int32 @index = Array(Int32?).new(h + w, nil) end def dfs(v, p) if i = @index[v] ans = @cycle[i..] ans.rotate! if @xy[ans[0]][1] != @xy[ans[1]][1] puts ans.size, ans.join(' ', &.succ) exit end @index[v] = @cycle.size @graph[v].each do |u, i| next if u == p @cycle.push i dfs(u, v) @cycle.pop end end def solve @graph.size.times do |i| dfs(i, p) if @index[i].nil? end puts -1 end end Solve.new.solve