from collections import deque
n = int(input())
graph = [[] for _ in range(n)]
global Q, S, T
global visited, dis
Q = deque([0])
S = [0] #訪れた順
T = []
visited = [0 for _ in range(n)]
dis = [-1 for _ in range(n)] #初めからの距離
visited[0] = 1
dis[0] = 0
def input_graph(n):
  for _ in range(n):
    node_id, num, *v = list(map(int, input().split()))
    for i in range(num):
      graph[node_id-1].append(v[i]-1)

def bfs(node_id):
  global Q, S
  while Q:
    node_id = Q.popleft()

    for ad_node in graph[node_id]:
      if visited[ad_node] == 1:
        continue
      visited[ad_node] = 1
      dis[ad_node] = dis[node_id] + 1
      S.append(ad_node)
      Q.append(ad_node)

if __name__ == "__main__":
  input_graph(n)
  bfs(0)
  for i, d in enumerate(dis):
    print(i+1, d)

 from collections import deque n = int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] graph = [[] for _ in range(n)] graph [[] for _ in range(n)] global Q, S, T global visited, dis Q = deque([0]) Q deque([0]) deque [0] 0 S = [0] S [0] 0 T = [] T [] 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 visited = [0 for _ in range(n)] visited [0 for _ in range(n)] -1 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) -1 dis = [-1 for _ in range(n)] dis [-1 for _ in range(n)] visited[0] = 1 visited[0] visited 0 1 dis[0] = 0 dis[0] dis 0 0 def input_graph(n):
  for _ in range(n):
    node_id, num, *v = list(map(int, input().split()))
    for i in range(num):
      graph[node_id-1].append(v[i]-1) input_graph for _ in range(n):
    node_id, num, *v = list(map(int, input().split()))
    for i in range(num):
      graph[node_id-1].append(v[i]-1) _ range(n) range n node_id, num, *v = list(map(int, input().split())) node_id list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split num *v v for i in range(num):
      graph[node_id-1].append(v[i]-1) i range(num) range num graph[node_id-1].append(v[i]-1) [node_id-1].append [node_id-1] graph node_id-1 node_id 1 append v[i]-1 v[i] v i 1 n n def bfs(node_id):
  global Q, S
  while Q:
    node_id = Q.popleft()

    for ad_node in graph[node_id]:
      if visited[ad_node] == 1:
        continue
      visited[ad_node] = 1
      dis[ad_node] = dis[node_id] + 1
      S.append(ad_node)
      Q.append(ad_node) bfs global Q, S while Q:
    node_id = Q.popleft()

    for ad_node in graph[node_id]:
      if visited[ad_node] == 1:
        continue
      visited[ad_node] = 1
      dis[ad_node] = dis[node_id] + 1
      S.append(ad_node)
      Q.append(ad_node) Q node_id = Q.popleft() node_id Q.popleft() Q.popleft Q popleft for ad_node in graph[node_id]:
      if visited[ad_node] == 1:
        continue
      visited[ad_node] = 1
      dis[ad_node] = dis[node_id] + 1
      S.append(ad_node)
      Q.append(ad_node) ad_node graph[node_id] graph node_id if visited[ad_node] == 1:
        continue
       visited[ad_node] == 1 visited[ad_node] visited ad_node 1 continue visited[ad_node] = 1 visited[ad_node] visited ad_node 1 dis[ad_node] = dis[node_id] + 1 dis[ad_node] dis ad_node dis[node_id] + 1 dis[node_id] dis node_id 1 S.append(ad_node) S.append S append ad_node Q.append(ad_node) Q.append Q append ad_node node_id node_id if __name__ == "__main__":
  input_graph(n)
  bfs(0)
  for i, d in enumerate(dis):
    print(i+1, d) __name__ == "__main__" __name__ "__main__" input_graph(n) input_graph n bfs(0) bfs 0 for i, d in enumerate(dis):
    print(i+1, d) i d enumerate(dis) enumerate dis print(i+1, d) print i+1 i 1 d global Q, S, T T = [] [] T S = [0] [0] S def input_graph(n):
  for _ in range(n):
    node_id, num, *v = list(map(int, input().split()))
    for i in range(num):
      graph[node_id-1].append(v[i]-1) def input_graph(n):
  for _ in range(n):
    node_id, num, *v = list(map(int, input().split()))
    for i in range(num):
      graph[node_id-1].append(v[i]-1) input_graph n = int(input()) int(input()) n visited = [0 for _ in range(n)] [0 for _ in range(n)] visited global visited, dis Q = deque([0]) deque([0]) Q def bfs(node_id):
  global Q, S
  while Q:
    node_id = Q.popleft()

    for ad_node in graph[node_id]:
      if visited[ad_node] == 1:
        continue
      visited[ad_node] = 1
      dis[ad_node] = dis[node_id] + 1
      S.append(ad_node)
      Q.append(ad_node) def bfs(node_id):
  global Q, S
  while Q:
    node_id = Q.popleft()

    for ad_node in graph[node_id]:
      if visited[ad_node] == 1:
        continue
      visited[ad_node] = 1
      dis[ad_node] = dis[node_id] + 1
      S.append(ad_node)
      Q.append(ad_node) bfs graph = [[] for _ in range(n)] [[] for _ in range(n)] graph dis = [-1 for _ in range(n)] [-1 for _ in range(n)] dis
from collections import deque
n = int(input())
graph = [[] for _ in range(n)]
global Q, S, T
global visited, dis
Q = deque([0])
S = [0] #訪れた順
T = []
visited = [0 for _ in range(n)]
dis = [-1 for _ in range(n)] #初めからの距離
visited[0] = 1
dis[0] = 0
def input_graph(n):
  for _ in range(n):
    node_id, num, *v = list(map(int, input().split()))
    for i in range(num):
      graph[node_id-1].append(v[i]-1)

def bfs(node_id):
  global Q, S
  while Q:
    node_id = Q.popleft()

    for ad_node in graph[node_id]:
      if visited[ad_node] == 1:
        continue
      visited[ad_node] = 1
      dis[ad_node] = dis[node_id] + 1
      S.append(ad_node)
      Q.append(ad_node)

if __name__ == "__main__":
  input_graph(n)
  bfs(0)
  for i, d in enumerate(dis):
    print(i+1, d)


