class Queue:
  def __init__(self,c):
    self.values = [None]*(c+1)
    self.s = 0
    self.t = 0

  def push(self,x):
    if self._next(self.s) == self.t:
      raise Exception("Overflow")
    self.values[self.s] = x
    self.s = self._next(self.s)

  def pop(self):
    if self.s == self.t:
      raise Exception("Underflow")
    ret = self.values[self.t]
    self.t = self._next(self.t)
    return ret

  def size(self):
    if self.s >= self.t:
      return self.s-self.t
    else:
      return len(self.values) - (self.t-self.s)


  def _next(self,p):
    p += 1
    if p >= len(self.values):
      p = 0
    return p


def solve(G):
  d = {}
  n = len(G)
  for u in G:
    d[u] = -1
  d[1] = 0
  queue = Queue(n+1)
  queue.push(1)
  while queue.size() > 0:
    u = queue.pop()
    for v in G[u]:
      if d[v] == -1:
        d[v] = d[u] + 1
        queue.push(v)
  return d







n = int(input())
G = {}
for i in range(n):
  entry = list(map(int, input().split(' ')))
  u = entry[0]
  k = entry[1]
  G[u] = set()
  for v in entry[2:]:
    G[u].add(v)


d = solve(G)
for u in G:
  print(u,d[u]) class Queue:
  def __init__(self,c):
    self.values = [None]*(c+1)
    self.s = 0
    self.t = 0

  def push(self,x):
    if self._next(self.s) == self.t:
      raise Exception("Overflow")
    self.values[self.s] = x
    self.s = self._next(self.s)

  def pop(self):
    if self.s == self.t:
      raise Exception("Underflow")
    ret = self.values[self.t]
    self.t = self._next(self.t)
    return ret

  def size(self):
    if self.s >= self.t:
      return self.s-self.t
    else:
      return len(self.values) - (self.t-self.s)


  def _next(self,p):
    p += 1
    if p >= len(self.values):
      p = 0
    return p Queue def __init__(self,c):
    self.values = [None]*(c+1)
    self.s = 0
    self.t = 0

   __init__ self.values = [None]*(c+1) self.values self values [None]*(c+1) [None] None c+1 c 1 self.s = 0 self.s self s 0 self.t = 0 self.t self t 0 self self c c def push(self,x):
    if self._next(self.s) == self.t:
      raise Exception("Overflow")
    self.values[self.s] = x
    self.s = self._next(self.s)

   push if self._next(self.s) == self.t:
      raise Exception("Overflow")
     self._next(self.s) == self.t self._next(self.s) self._next self _next self.s self s self.t self t raise Exception("Overflow") Exception("Overflow") Exception "Overflow" self.values[self.s] = x self.values[self.s] self.values self values self.s self s x self.s = self._next(self.s) self.s self s self._next(self.s) self._next self _next self.s self s self self x x def pop(self):
    if self.s == self.t:
      raise Exception("Underflow")
    ret = self.values[self.t]
    self.t = self._next(self.t)
    return ret

   pop if self.s == self.t:
      raise Exception("Underflow")
     self.s == self.t self.s self s self.t self t raise Exception("Underflow") Exception("Underflow") Exception "Underflow" ret = self.values[self.t] ret self.values[self.t] self.values self values self.t self t self.t = self._next(self.t) self.t self t self._next(self.t) self._next self _next self.t self t return ret ret self self def size(self):
    if self.s >= self.t:
      return self.s-self.t
    else:
      return len(self.values) - (self.t-self.s)


   size if self.s >= self.t:
      return self.s-self.t
    else:
      return len(self.values) - (self.t-self.s)


   self.s >= self.t self.s self s self.t self t return self.s-self.t self.s-self.t self.s self s self.t self t return len(self.values) - (self.t-self.s) len(self.values) - (self.t-self.s) len(self.values) len self.values self values self.t-self.s self.t self t self.s self s self self def _next(self,p):
    p += 1
    if p >= len(self.values):
      p = 0
    return p _next p += 1 p 1 if p >= len(self.values):
      p = 0
     p >= len(self.values) p len(self.values) len self.values self values p = 0 p 0 return p p self self p p def solve(G):
  d = {}
  n = len(G)
  for u in G:
    d[u] = -1
  d[1] = 0
  queue = Queue(n+1)
  queue.push(1)
  while queue.size() > 0:
    u = queue.pop()
    for v in G[u]:
      if d[v] == -1:
        d[v] = d[u] + 1
        queue.push(v)
  return d solve d = {} d {} n = len(G) n len(G) len G for u in G:
    d[u] = -1
   u G d[u] = -1 d[u] d u -1 d[1] = 0 d[1] d 1 0 queue = Queue(n+1) queue Queue(n+1) Queue n+1 n 1 queue.push(1) queue.push queue push 1 while queue.size() > 0:
    u = queue.pop()
    for v in G[u]:
      if d[v] == -1:
        d[v] = d[u] + 1
        queue.push(v)
   queue.size() > 0 queue.size() queue.size queue size 0 u = queue.pop() u queue.pop() queue.pop queue pop for v in G[u]:
      if d[v] == -1:
        d[v] = d[u] + 1
        queue.push(v)
   v G[u] G u if d[v] == -1:
        d[v] = d[u] + 1
        queue.push(v)
   d[v] == -1 d[v] d v -1 d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 queue.push(v) queue.push queue push v return d d G G n = int(input()) n int(input()) int input() input G = {} G {} for i in range(n):
  entry = list(map(int, input().split(' ')))
  u = entry[0]
  k = entry[1]
  G[u] = set()
  for v in entry[2:]:
    G[u].add(v) i range(n) range n entry = list(map(int, input().split(' '))) entry list(map(int, input().split(' '))) list map(int, input().split(' ')) map int input().split(' ') ().split () input split ' ' u = entry[0] u entry[0] entry 0 k = entry[1] k entry[1] entry 1 G[u] = set() G[u] G u set() set for v in entry[2:]:
    G[u].add(v) v entry[2:] entry 2: 2 G[u].add(v) [u].add [u] G u add v d = solve(G) d solve(G) solve G for u in G:
  print(u,d[u]) u G print(u,d[u]) print u d[u] d u n = int(input()) int(input()) n def solve(G):
  d = {}
  n = len(G)
  for u in G:
    d[u] = -1
  d[1] = 0
  queue = Queue(n+1)
  queue.push(1)
  while queue.size() > 0:
    u = queue.pop()
    for v in G[u]:
      if d[v] == -1:
        d[v] = d[u] + 1
        queue.push(v)
  return d def solve(G):
  d = {}
  n = len(G)
  for u in G:
    d[u] = -1
  d[1] = 0
  queue = Queue(n+1)
  queue.push(1)
  while queue.size() > 0:
    u = queue.pop()
    for v in G[u]:
      if d[v] == -1:
        d[v] = d[u] + 1
        queue.push(v)
  return d solve entry = list(map(int, input().split(' '))) list(map(int, input().split(' '))) entry G = {} {} G u = entry[0] entry[0] u d = solve(G) solve(G) d k = entry[1] entry[1] k class Queue:
  def __init__(self,c):
    self.values = [None]*(c+1)
    self.s = 0
    self.t = 0

  def push(self,x):
    if self._next(self.s) == self.t:
      raise Exception("Overflow")
    self.values[self.s] = x
    self.s = self._next(self.s)

  def pop(self):
    if self.s == self.t:
      raise Exception("Underflow")
    ret = self.values[self.t]
    self.t = self._next(self.t)
    return ret

  def size(self):
    if self.s >= self.t:
      return self.s-self.t
    else:
      return len(self.values) - (self.t-self.s)


  def _next(self,p):
    p += 1
    if p >= len(self.values):
      p = 0
    return p class Queue:
  def __init__(self,c):
    self.values = [None]*(c+1)
    self.s = 0
    self.t = 0

  def push(self,x):
    if self._next(self.s) == self.t:
      raise Exception("Overflow")
    self.values[self.s] = x
    self.s = self._next(self.s)

  def pop(self):
    if self.s == self.t:
      raise Exception("Underflow")
    ret = self.values[self.t]
    self.t = self._next(self.t)
    return ret

  def size(self):
    if self.s >= self.t:
      return self.s-self.t
    else:
      return len(self.values) - (self.t-self.s)


  def _next(self,p):
    p += 1
    if p >= len(self.values):
      p = 0
    return p Queue
class Queue:
  def __init__(self,c):
    self.values = [None]*(c+1)
    self.s = 0
    self.t = 0

  def push(self,x):
    if self._next(self.s) == self.t:
      raise Exception("Overflow")
    self.values[self.s] = x
    self.s = self._next(self.s)

  def pop(self):
    if self.s == self.t:
      raise Exception("Underflow")
    ret = self.values[self.t]
    self.t = self._next(self.t)
    return ret

  def size(self):
    if self.s >= self.t:
      return self.s-self.t
    else:
      return len(self.values) - (self.t-self.s)


  def _next(self,p):
    p += 1
    if p >= len(self.values):
      p = 0
    return p


def solve(G):
  d = {}
  n = len(G)
  for u in G:
    d[u] = -1
  d[1] = 0
  queue = Queue(n+1)
  queue.push(1)
  while queue.size() > 0:
    u = queue.pop()
    for v in G[u]:
      if d[v] == -1:
        d[v] = d[u] + 1
        queue.push(v)
  return d







n = int(input())
G = {}
for i in range(n):
  entry = list(map(int, input().split(' ')))
  u = entry[0]
  k = entry[1]
  G[u] = set()
  for v in entry[2:]:
    G[u].add(v)


d = solve(G)
for u in G:
  print(u,d[u])