import Queue
class Node:
    def __init__(self,v):
        self.v = v
        self.c = []

def bfs(n):
    d = {n.v:0}
    q = Queue.Queue()
    q.put(n)
    visited = set([n.v])
    while q.qsize() > 0:
        x = q.get()
        for c in x.c:
            if not c.v in visited:
                q.put(c)
                d[c.v] = d[x.v]+1
                visited.add(c.v)
    return d



n = int(raw_input())
nodes = {}
for _ in range(n):
    entry = map(int,raw_input().split(' '))
    nid = entry[0]
    if not nid in nodes: nodes[nid] = Node(nid)
    k = entry[1]
    if k > 0:
        children = entry[2:]
        for c in children:
            if not c in nodes: nodes[c] = Node(c)
            nodes[nid].c.append(nodes[c])

d = bfs(nodes[1])
for nid in nodes:
    if not nid in d:
        d[nid] = -1
for k in d:
    print k,d[k] import Queue Queue class Node:
    def __init__(self,v):
        self.v = v
        self.c = [] Node def __init__(self,v):
        self.v = v
        self.c = [] __init__ self.v = v self.v self v v self.c = [] self.c self c [] self self v v def bfs(n):
    d = {n.v:0}
    q = Queue.Queue()
    q.put(n)
    visited = set([n.v])
    while q.qsize() > 0:
        x = q.get()
        for c in x.c:
            if not c.v in visited:
                q.put(c)
                d[c.v] = d[x.v]+1
                visited.add(c.v)
    return d bfs d = {n.v:0} d {n.v:0} n.v n.v n v 0 q = Queue.Queue() q Queue.Queue() Queue.Queue Queue Queue q.put(n) q.put q put n visited = set([n.v]) visited set([n.v]) set [n.v] n.v n v while q.qsize() > 0:
        x = q.get()
        for c in x.c:
            if not c.v in visited:
                q.put(c)
                d[c.v] = d[x.v]+1
                visited.add(c.v)
     q.qsize() > 0 q.qsize() q.qsize q qsize 0 x = q.get() x q.get() q.get q get for c in x.c:
            if not c.v in visited:
                q.put(c)
                d[c.v] = d[x.v]+1
                visited.add(c.v)
     c x.c x c if not c.v in visited:
                q.put(c)
                d[c.v] = d[x.v]+1
                visited.add(c.v)
     not c.v in visited c.v in visited c.v c v visited q.put(c) q.put q put c d[c.v] = d[x.v]+1 d[c.v] d c.v c v d[x.v]+1 d[x.v] d x.v x v 1 visited.add(c.v) visited.add visited add c.v c v return d d n n n = int(raw_input()) n int(raw_input()) int raw_input() raw_input nodes = {} nodes {} for _ in range(n):
    entry = map(int,raw_input().split(' '))
    nid = entry[0]
    if not nid in nodes: nodes[nid] = Node(nid)
    k = entry[1]
    if k > 0:
        children = entry[2:]
        for c in children:
            if not c in nodes: nodes[c] = Node(c)
            nodes[nid].c.append(nodes[c]) _ range(n) range n entry = map(int,raw_input().split(' ')) entry map(int,raw_input().split(' ')) map int raw_input().split(' ') ().split () raw_input split ' ' nid = entry[0] nid entry[0] entry 0 if not nid in nodes: nodes[nid] = Node(nid)
     not nid in nodes nid in nodes nid nodes k = entry[1] k entry[1] entry 1 if k > 0:
        children = entry[2:]
        for c in children:
            if not c in nodes: nodes[c] = Node(c)
            nodes[nid].c.append(nodes[c]) k > 0 k 0 children = entry[2:] children entry[2:] entry 2: 2 for c in children:
            if not c in nodes: nodes[c] = Node(c)
            nodes[nid].c.append(nodes[c]) c children if not c in nodes: nodes[c] = Node(c)
             not c in nodes c in nodes c nodes nodes[nid].c.append(nodes[c]) [nid].c.append [nid].c [nid] nodes nid c append nodes[c] nodes c d = bfs(nodes[1]) d bfs(nodes[1]) bfs nodes[1] nodes 1 for nid in nodes:
    if not nid in d:
        d[nid] = -1 nid nodes if not nid in d:
        d[nid] = -1 not nid in d nid in d nid d d[nid] = -1 d[nid] d nid -1 for k in d:
    print k,d[k] k d print k d[k] d k nodes = {} {} nodes def bfs(n):
    d = {n.v:0}
    q = Queue.Queue()
    q.put(n)
    visited = set([n.v])
    while q.qsize() > 0:
        x = q.get()
        for c in x.c:
            if not c.v in visited:
                q.put(c)
                d[c.v] = d[x.v]+1
                visited.add(c.v)
    return d def bfs(n):
    d = {n.v:0}
    q = Queue.Queue()
    q.put(n)
    visited = set([n.v])
    while q.qsize() > 0:
        x = q.get()
        for c in x.c:
            if not c.v in visited:
                q.put(c)
                d[c.v] = d[x.v]+1
                visited.add(c.v)
    return d bfs n = int(raw_input()) int(raw_input()) n k = entry[1] entry[1] k class Node:
    def __init__(self,v):
        self.v = v
        self.c = [] class Node:
    def __init__(self,v):
        self.v = v
        self.c = [] Node nid = entry[0] entry[0] nid children = entry[2:] entry[2:] children d = bfs(nodes[1]) bfs(nodes[1]) d entry = map(int,raw_input().split(' ')) map(int,raw_input().split(' ')) entry
import Queue
class Node:
    def __init__(self,v):
        self.v = v
        self.c = []

def bfs(n):
    d = {n.v:0}
    q = Queue.Queue()
    q.put(n)
    visited = set([n.v])
    while q.qsize() > 0:
        x = q.get()
        for c in x.c:
            if not c.v in visited:
                q.put(c)
                d[c.v] = d[x.v]+1
                visited.add(c.v)
    return d



n = int(raw_input())
nodes = {}
for _ in range(n):
    entry = map(int,raw_input().split(' '))
    nid = entry[0]
    if not nid in nodes: nodes[nid] = Node(nid)
    k = entry[1]
    if k > 0:
        children = entry[2:]
        for c in children:
            if not c in nodes: nodes[c] = Node(c)
            nodes[nid].c.append(nodes[c])

d = bfs(nodes[1])
for nid in nodes:
    if not nid in d:
        d[nid] = -1
for k in d:
    print k,d[k]