from collections import deque

n = int(input())
adj = [list(map(int, input().split(' '))) for i in range(n)]

class Node:
  def __init__(self, id, ck):
    self.id = id
    self.ck = ck
    self.depth = -1

  def __str__(self):
    return '{} {}'.format(self.id, self.depth)

class Graph:
  def __init__(self):
    self.map = {}

  def get(self, id):
    return self.map[id]

  def set(self, id, ck):
    self.map[id] = Node(id, ck)

  def bfs(self):
    root = self.get(1)
    root.depth = 0
    queue = deque()
    queue.append(root)
    while(len(queue) > 0):
      node = queue.popleft()
      for id in node.ck:
        target = self.get(id)
        if target.depth != -1: continue
        target.depth = node.depth + 1
        queue.append(target)

  def __str__(self):
    return '\n'.join(map(str, [self.get(i + 1) for i in range(len(self.map))]))

graph = Graph()
for data in adj:
  id = data[0]
  ck = data[2:]
  graph.set(id, ck)

graph.bfs()
print(graph)
 from collections import deque n = int(input()) n int(input()) int input() input list(map(int, input().split(' '))) for i in range(n) for i in range(n) i range(n) range n for i in range(n) list(map(int, input().split(' '))) list map(int, input().split(' ')) map int input().split(' ') ().split () input split ' ' adj = [list(map(int, input().split(' '))) for i in range(n)] adj [list(map(int, input().split(' '))) for i in range(n)] class Node:
  def __init__(self, id, ck):
    self.id = id
    self.ck = ck
    self.depth = -1

  def __str__(self):
    return '{} {}'.format(self.id, self.depth) Node def __init__(self, id, ck):
    self.id = id
    self.ck = ck
    self.depth = -1

   __init__ self.id = id self.id self id id self.ck = ck self.ck self ck ck self.depth = -1 self.depth self depth -1 self self id id ck ck def __str__(self):
    return '{} {}'.format(self.id, self.depth) __str__ return '{} {}'.format(self.id, self.depth) '{} {}'.format(self.id, self.depth) '{} {}'.format '{} {}' format self.id self id self.depth self depth self self class Graph:
  def __init__(self):
    self.map = {}

  def get(self, id):
    return self.map[id]

  def set(self, id, ck):
    self.map[id] = Node(id, ck)

  def bfs(self):
    root = self.get(1)
    root.depth = 0
    queue = deque()
    queue.append(root)
    while(len(queue) > 0):
      node = queue.popleft()
      for id in node.ck:
        target = self.get(id)
        if target.depth != -1: continue
        target.depth = node.depth + 1
        queue.append(target)

  def __str__(self):
    return '\n'.join(map(str, [self.get(i + 1) for i in range(len(self.map))])) Graph def __init__(self):
    self.map = {}

   __init__ self.map = {} self.map self map {} self self def get(self, id):
    return self.map[id]

   get return self.map[id] self.map[id] self.map self map id self self id id def set(self, id, ck):
    self.map[id] = Node(id, ck)

   set self.map[id] = Node(id, ck) self.map[id] self.map self map id Node(id, ck) Node id ck self self id id ck ck def bfs(self):
    root = self.get(1)
    root.depth = 0
    queue = deque()
    queue.append(root)
    while(len(queue) > 0):
      node = queue.popleft()
      for id in node.ck:
        target = self.get(id)
        if target.depth != -1: continue
        target.depth = node.depth + 1
        queue.append(target)

   bfs root = self.get(1) root self.get(1) self.get self get 1 root.depth = 0 root.depth root depth 0 queue = deque() queue deque() deque queue.append(root) queue.append queue append root while(len(queue) > 0):
      node = queue.popleft()
      for id in node.ck:
        target = self.get(id)
        if target.depth != -1: continue
        target.depth = node.depth + 1
        queue.append(target)

   len(queue) > 0 len(queue) len queue 0 node = queue.popleft() node queue.popleft() queue.popleft queue popleft for id in node.ck:
        target = self.get(id)
        if target.depth != -1: continue
        target.depth = node.depth + 1
        queue.append(target)

   id node.ck node ck target = self.get(id) target self.get(id) self.get self get id if target.depth != -1: continue
         target.depth != -1 target.depth target depth -1 target.depth = node.depth + 1 target.depth target depth node.depth + 1 node.depth node depth 1 queue.append(target) queue.append queue append target self self def __str__(self):
    return '\n'.join(map(str, [self.get(i + 1) for i in range(len(self.map))])) __str__ self.get(i + 1) for i in range(len(self.map)) for i in range(len(self.map)) i range(len(self.map)) range len(self.map) len self.map self map for i in range(len(self.map)) self.get(i + 1) self.get self get i + 1 i 1 return '\n'.join(map(str, [self.get(i + 1) for i in range(len(self.map))])) '\n'.join(map(str, [self.get(i + 1) for i in range(len(self.map))])) '\n'.join '\n' join map(str, [self.get(i + 1) for i in range(len(self.map))]) map str [self.get(i + 1) for i in range(len(self.map))] self self graph = Graph() graph Graph() Graph for data in adj:
  id = data[0]
  ck = data[2:]
  graph.set(id, ck) data adj id = data[0] id data[0] data 0 ck = data[2:] ck data[2:] data 2: 2 graph.set(id, ck) graph.set graph set id ck graph.bfs() graph.bfs graph bfs print(graph) print graph graph = Graph() Graph() graph class Node:
  def __init__(self, id, ck):
    self.id = id
    self.ck = ck
    self.depth = -1

  def __str__(self):
    return '{} {}'.format(self.id, self.depth) class Node:
  def __init__(self, id, ck):
    self.id = id
    self.ck = ck
    self.depth = -1

  def __str__(self):
    return '{} {}'.format(self.id, self.depth) Node adj = [list(map(int, input().split(' '))) for i in range(n)] [list(map(int, input().split(' '))) for i in range(n)] adj class Graph:
  def __init__(self):
    self.map = {}

  def get(self, id):
    return self.map[id]

  def set(self, id, ck):
    self.map[id] = Node(id, ck)

  def bfs(self):
    root = self.get(1)
    root.depth = 0
    queue = deque()
    queue.append(root)
    while(len(queue) > 0):
      node = queue.popleft()
      for id in node.ck:
        target = self.get(id)
        if target.depth != -1: continue
        target.depth = node.depth + 1
        queue.append(target)

  def __str__(self):
    return '\n'.join(map(str, [self.get(i + 1) for i in range(len(self.map))])) class Graph:
  def __init__(self):
    self.map = {}

  def get(self, id):
    return self.map[id]

  def set(self, id, ck):
    self.map[id] = Node(id, ck)

  def bfs(self):
    root = self.get(1)
    root.depth = 0
    queue = deque()
    queue.append(root)
    while(len(queue) > 0):
      node = queue.popleft()
      for id in node.ck:
        target = self.get(id)
        if target.depth != -1: continue
        target.depth = node.depth + 1
        queue.append(target)

  def __str__(self):
    return '\n'.join(map(str, [self.get(i + 1) for i in range(len(self.map))])) Graph id = data[0] data[0] id n = int(input()) int(input()) n ck = data[2:] data[2:] ck
from collections import deque

n = int(input())
adj = [list(map(int, input().split(' '))) for i in range(n)]

class Node:
  def __init__(self, id, ck):
    self.id = id
    self.ck = ck
    self.depth = -1

  def __str__(self):
    return '{} {}'.format(self.id, self.depth)

class Graph:
  def __init__(self):
    self.map = {}

  def get(self, id):
    return self.map[id]

  def set(self, id, ck):
    self.map[id] = Node(id, ck)

  def bfs(self):
    root = self.get(1)
    root.depth = 0
    queue = deque()
    queue.append(root)
    while(len(queue) > 0):
      node = queue.popleft()
      for id in node.ck:
        target = self.get(id)
        if target.depth != -1: continue
        target.depth = node.depth + 1
        queue.append(target)

  def __str__(self):
    return '\n'.join(map(str, [self.get(i + 1) for i in range(len(self.map))]))

graph = Graph()
for data in adj:
  id = data[0]
  ck = data[2:]
  graph.set(id, ck)

graph.bfs()
print(graph)

