import queue

N = int(input())
L = [list(map(int,input().split())) for _ in range(N)]
V = [(i+1) for i in range(N)]
E = []
for i in range(N):
    for j in range(len(L[i])-2):
        E.append((i+1,L[i][j+2]))

class Graph:
    def __init__(self,v,e):
        self.v = v
        self.e = e
        self.v_visited = [False for _ in range(N)]
    def visited(self,v):
        self.v_visited[v-1] = True

q = queue.Queue()
g = Graph(V,E)
TS = [-1 for _ in range(N)]

def bfs(u):
    q.put(u)
    g.visited(u)
    TS[u-1] = 0
    while not q.empty():
        u = q.get()
        for v in g.v:
            if (u,v) in g.e and g.v_visited[v-1] == False:
                q.put(v)
                g.visited(v)
                TS[v-1] = TS[u-1]+1
bfs(1)

for i in range(N):
    print(i+1,TS[i])
 import queue queue N = int(input()) N int(input()) int input() input list(map(int,input().split())) for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split L = [list(map(int,input().split())) for _ in range(N)] L [list(map(int,input().split())) for _ in range(N)] (i+1) for i in range(N) for i in range(N) i range(N) range N for i in range(N) i+1 i 1 V = [(i+1) for i in range(N)] V [(i+1) for i in range(N)] E = [] E [] for i in range(N):
    for j in range(len(L[i])-2):
        E.append((i+1,L[i][j+2])) i range(N) range N for j in range(len(L[i])-2):
        E.append((i+1,L[i][j+2])) j range(len(L[i])-2) range len(L[i])-2 len(L[i]) len L[i] L i 2 E.append((i+1,L[i][j+2])) E.append E append (i+1,L[i][j+2]) i+1 i 1 L[i][j+2] [i] L i j+2 j 2 class Graph:
    def __init__(self,v,e):
        self.v = v
        self.e = e
        self.v_visited = [False for _ in range(N)]
    def visited(self,v):
        self.v_visited[v-1] = True Graph def __init__(self,v,e):
        self.v = v
        self.e = e
        self.v_visited = [False for _ in range(N)]
     __init__ self.v = v self.v self v v self.e = e self.e self e e False for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) False self.v_visited = [False for _ in range(N)] self.v_visited self v_visited [False for _ in range(N)] self self v v e e def visited(self,v):
        self.v_visited[v-1] = True visited self.v_visited[v-1] = True self.v_visited[v-1] self.v_visited self v_visited v-1 v 1 True self self v v q = queue.Queue() q queue.Queue() queue.Queue queue Queue g = Graph(V,E) g Graph(V,E) Graph V E -1 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) -1 TS = [-1 for _ in range(N)] TS [-1 for _ in range(N)] def bfs(u):
    q.put(u)
    g.visited(u)
    TS[u-1] = 0
    while not q.empty():
        u = q.get()
        for v in g.v:
            if (u,v) in g.e and g.v_visited[v-1] == False:
                q.put(v)
                g.visited(v)
                TS[v-1] = TS[u-1]+1 bfs q.put(u) q.put q put u g.visited(u) g.visited g visited u TS[u-1] = 0 TS[u-1] TS u-1 u 1 0 while not q.empty():
        u = q.get()
        for v in g.v:
            if (u,v) in g.e and g.v_visited[v-1] == False:
                q.put(v)
                g.visited(v)
                TS[v-1] = TS[u-1]+1 not q.empty() q.empty() q.empty q empty u = q.get() u q.get() q.get q get for v in g.v:
            if (u,v) in g.e and g.v_visited[v-1] == False:
                q.put(v)
                g.visited(v)
                TS[v-1] = TS[u-1]+1 v g.v g v if (u,v) in g.e and g.v_visited[v-1] == False:
                q.put(v)
                g.visited(v)
                TS[v-1] = TS[u-1]+1 (u,v) in g.e and g.v_visited[v-1] == False (u,v) in g.e (u,v) u v g.e g e g.v_visited[v-1] == False g.v_visited[v-1] g.v_visited g v_visited v-1 v 1 False q.put(v) q.put q put v g.visited(v) g.visited g visited v TS[v-1] = TS[u-1]+1 TS[v-1] TS v-1 v 1 TS[u-1]+1 TS[u-1] TS u-1 u 1 1 u u bfs(1) bfs 1 for i in range(N):
    print(i+1,TS[i]) i range(N) range N print(i+1,TS[i]) print i+1 i 1 TS[i] TS i L = [list(map(int,input().split())) for _ in range(N)] [list(map(int,input().split())) for _ in range(N)] L class Graph:
    def __init__(self,v,e):
        self.v = v
        self.e = e
        self.v_visited = [False for _ in range(N)]
    def visited(self,v):
        self.v_visited[v-1] = True class Graph:
    def __init__(self,v,e):
        self.v = v
        self.e = e
        self.v_visited = [False for _ in range(N)]
    def visited(self,v):
        self.v_visited[v-1] = True Graph N = int(input()) int(input()) N E = [] [] E q = queue.Queue() queue.Queue() q def bfs(u):
    q.put(u)
    g.visited(u)
    TS[u-1] = 0
    while not q.empty():
        u = q.get()
        for v in g.v:
            if (u,v) in g.e and g.v_visited[v-1] == False:
                q.put(v)
                g.visited(v)
                TS[v-1] = TS[u-1]+1 def bfs(u):
    q.put(u)
    g.visited(u)
    TS[u-1] = 0
    while not q.empty():
        u = q.get()
        for v in g.v:
            if (u,v) in g.e and g.v_visited[v-1] == False:
                q.put(v)
                g.visited(v)
                TS[v-1] = TS[u-1]+1 bfs V = [(i+1) for i in range(N)] [(i+1) for i in range(N)] V g = Graph(V,E) Graph(V,E) g TS = [-1 for _ in range(N)] [-1 for _ in range(N)] TS
import queue

N = int(input())
L = [list(map(int,input().split())) for _ in range(N)]
V = [(i+1) for i in range(N)]
E = []
for i in range(N):
    for j in range(len(L[i])-2):
        E.append((i+1,L[i][j+2]))

class Graph:
    def __init__(self,v,e):
        self.v = v
        self.e = e
        self.v_visited = [False for _ in range(N)]
    def visited(self,v):
        self.v_visited[v-1] = True

q = queue.Queue()
g = Graph(V,E)
TS = [-1 for _ in range(N)]

def bfs(u):
    q.put(u)
    g.visited(u)
    TS[u-1] = 0
    while not q.empty():
        u = q.get()
        for v in g.v:
            if (u,v) in g.e and g.v_visited[v-1] == False:
                q.put(v)
                g.visited(v)
                TS[v-1] = TS[u-1]+1
bfs(1)

for i in range(N):
    print(i+1,TS[i])

