def bfs(adj, s):
    n = len(adj)
    d = [-1 for _ in range(n)]
    c = [0 for _ in range(n)]

    d[s] = 0
    c[s] = 1

    queue = [s]

    while len(queue) > 0:
        u = queue.pop(0)
        for v in adj[u]:
            if c[v] == 0:
                c[v] = 1
                d[v] = d[u] + 1
                queue.append(v)

    return d


n = int(input())
adj = [[] for _ in range(n)]
for _ in range(n):
    s = [int(x) for x in input().split()]
    u = s[0]
    for v in s[2:]:
        adj[u - 1].append(v - 1)

d = bfs(adj, 0)

for i in range(n):
    print(str(i + 1) + " " + str(d[i])) def bfs(adj, s):
    n = len(adj)
    d = [-1 for _ in range(n)]
    c = [0 for _ in range(n)]

    d[s] = 0
    c[s] = 1

    queue = [s]

    while len(queue) > 0:
        u = queue.pop(0)
        for v in adj[u]:
            if c[v] == 0:
                c[v] = 1
                d[v] = d[u] + 1
                queue.append(v)

    return d bfs n = len(adj) n len(adj) len adj -1 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) -1 d = [-1 for _ in range(n)] d [-1 for _ in range(n)] 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 c = [0 for _ in range(n)] c [0 for _ in range(n)] d[s] = 0 d[s] d s 0 c[s] = 1 c[s] c s 1 queue = [s] queue [s] s while len(queue) > 0:
        u = queue.pop(0)
        for v in adj[u]:
            if c[v] == 0:
                c[v] = 1
                d[v] = d[u] + 1
                queue.append(v)

     len(queue) > 0 len(queue) len queue 0 u = queue.pop(0) u queue.pop(0) queue.pop queue pop 0 for v in adj[u]:
            if c[v] == 0:
                c[v] = 1
                d[v] = d[u] + 1
                queue.append(v)

     v adj[u] adj u if c[v] == 0:
                c[v] = 1
                d[v] = d[u] + 1
                queue.append(v)

     c[v] == 0 c[v] c v 0 c[v] = 1 c[v] c v 1 d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 queue.append(v) queue.append queue append v return d d adj adj s s 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) [] adj = [[] for _ in range(n)] adj [[] for _ in range(n)] for _ in range(n):
    s = [int(x) for x in input().split()]
    u = s[0]
    for v in s[2:]:
        adj[u - 1].append(v - 1) _ range(n) range n int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x s = [int(x) for x in input().split()] s [int(x) for x in input().split()] u = s[0] u s[0] s 0 for v in s[2:]:
        adj[u - 1].append(v - 1) v s[2:] s 2: 2 adj[u - 1].append(v - 1) [u - 1].append [u - 1] adj u - 1 u 1 append v - 1 v 1 d = bfs(adj, 0) d bfs(adj, 0) bfs adj 0 for i in range(n):
    print(str(i + 1) + " " + str(d[i])) i range(n) range n print(str(i + 1) + " " + str(d[i])) print str(i + 1) + " " + str(d[i]) str(i + 1) + " " str(i + 1) str i + 1 i 1 " " str(d[i]) str d[i] d i d = bfs(adj, 0) bfs(adj, 0) d def bfs(adj, s):
    n = len(adj)
    d = [-1 for _ in range(n)]
    c = [0 for _ in range(n)]

    d[s] = 0
    c[s] = 1

    queue = [s]

    while len(queue) > 0:
        u = queue.pop(0)
        for v in adj[u]:
            if c[v] == 0:
                c[v] = 1
                d[v] = d[u] + 1
                queue.append(v)

    return d def bfs(adj, s):
    n = len(adj)
    d = [-1 for _ in range(n)]
    c = [0 for _ in range(n)]

    d[s] = 0
    c[s] = 1

    queue = [s]

    while len(queue) > 0:
        u = queue.pop(0)
        for v in adj[u]:
            if c[v] == 0:
                c[v] = 1
                d[v] = d[u] + 1
                queue.append(v)

    return d bfs s = [int(x) for x in input().split()] [int(x) for x in input().split()] s adj = [[] for _ in range(n)] [[] for _ in range(n)] adj u = s[0] s[0] u n = int(input()) int(input()) n
def bfs(adj, s):
    n = len(adj)
    d = [-1 for _ in range(n)]
    c = [0 for _ in range(n)]

    d[s] = 0
    c[s] = 1

    queue = [s]

    while len(queue) > 0:
        u = queue.pop(0)
        for v in adj[u]:
            if c[v] == 0:
                c[v] = 1
                d[v] = d[u] + 1
                queue.append(v)

    return d


n = int(input())
adj = [[] for _ in range(n)]
for _ in range(n):
    s = [int(x) for x in input().split()]
    u = s[0]
    for v in s[2:]:
        adj[u - 1].append(v - 1)

d = bfs(adj, 0)

for i in range(n):
    print(str(i + 1) + " " + str(d[i]))
