def fromAdjRow(xs, n)
  a = []
  1.upto(n) do |i|
    a << i if xs[i-1] == 1
  end
  a
end

def toAdj(g, n)
  g.map do |xs|
    a = [0]*n
    unless xs[1] == 0
      xs[2..-1].each do |x|
        a[x-1] = 1
      end
    end
    a
  end
end

if __FILE__ == $0
  n = gets.to_i
  g = toAdj(n.times.map{ gets.split.map(&:to_i) }, n)

  q = [1]
  visited = Array.new(n, false)
  visited[0] = true
  ds = Array.new(n, -1)
  ds[0] = 0
  d  = 1
  until q.empty?
    u  = q.shift
    fromAdjRow(g[u-1], n).each do |v|
      unless visited[v-1]
        ds[v-1] = d
        visited[v-1] = true
        q.push(v)
      end
    end
    d += 1
  end

  ds.each.with_index{ |x, i| puts (i+1).to_s + " " + x.to_s }
end