import sys

def bfs():
    global V, D

    D[1] = 0
    children = V[1]
    d = 1
    while len(children) != 0 :
        new = []
        for child in children:
            if D[child] == -1:
                D[child] = d
                new += V[child]
        children = new
        d += 1

lines = sys.stdin.readlines()
n = int(lines[0])
V = [[] for _ in range(n + 1)]

for i in range(1, n + 1):
    v = map(int, lines[i].split())
    V[v[0]] = v[2:]
D = [-1] * (n + 1)
bfs()

for i in range(1, n + 1):
    print i, D[i] import sys sys def bfs():
    global V, D

    D[1] = 0
    children = V[1]
    d = 1
    while len(children) != 0 :
        new = []
        for child in children:
            if D[child] == -1:
                D[child] = d
                new += V[child]
        children = new
        d += 1 bfs global V, D D[1] = 0 D[1] D 1 0 children = V[1] children V[1] V 1 d = 1 d 1 while len(children) != 0 :
        new = []
        for child in children:
            if D[child] == -1:
                D[child] = d
                new += V[child]
        children = new
        d += 1 len(children) != 0 len(children) len children 0 new = [] new [] for child in children:
            if D[child] == -1:
                D[child] = d
                new += V[child]
         child children if D[child] == -1:
                D[child] = d
                new += V[child]
         D[child] == -1 D[child] D child -1 D[child] = d D[child] D child d new += V[child] new V[child] V child children = new children new d += 1 d 1 lines = sys.stdin.readlines() lines sys.stdin.readlines() sys.stdin.readlines sys.stdin sys stdin readlines n = int(lines[0]) n int(lines[0]) int lines[0] lines 0 [] for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) [] V = [[] for _ in range(n + 1)] V [[] for _ in range(n + 1)] for i in range(1, n + 1):
    v = map(int, lines[i].split())
    V[v[0]] = v[2:] i range(1, n + 1) range 1 n + 1 n 1 v = map(int, lines[i].split()) v map(int, lines[i].split()) map int lines[i].split() [i].split [i] lines i split V[v[0]] = v[2:] V[v[0]] V v[0] v 0 v[2:] v 2: 2 D = [-1] * (n + 1) D [-1] * (n + 1) [-1] -1 n + 1 n 1 bfs() bfs for i in range(1, n + 1):
    print i, D[i] i range(1, n + 1) range 1 n + 1 n 1 print i D[i] D i D = [-1] * (n + 1) [-1] * (n + 1) D n = int(lines[0]) int(lines[0]) n V = [[] for _ in range(n + 1)] [[] for _ in range(n + 1)] V v = map(int, lines[i].split()) map(int, lines[i].split()) v lines = sys.stdin.readlines() sys.stdin.readlines() lines def bfs():
    global V, D

    D[1] = 0
    children = V[1]
    d = 1
    while len(children) != 0 :
        new = []
        for child in children:
            if D[child] == -1:
                D[child] = d
                new += V[child]
        children = new
        d += 1 def bfs():
    global V, D

    D[1] = 0
    children = V[1]
    d = 1
    while len(children) != 0 :
        new = []
        for child in children:
            if D[child] == -1:
                D[child] = d
                new += V[child]
        children = new
        d += 1 bfs
import sys

def bfs():
    global V, D

    D[1] = 0
    children = V[1]
    d = 1
    while len(children) != 0 :
        new = []
        for child in children:
            if D[child] == -1:
                D[child] = d
                new += V[child]
        children = new
        d += 1

lines = sys.stdin.readlines()
n = int(lines[0])
V = [[] for _ in range(n + 1)]

for i in range(1, n + 1):
    v = map(int, lines[i].split())
    V[v[0]] = v[2:]
D = [-1] * (n + 1)
bfs()

for i in range(1, n + 1):
    print i, D[i]