def breathSerch(P,N):
    n=len(P)-1
    m=1
    QQ=[N[1]]
    while(QQ != []):
        R=[]
        for Q in QQ:
            for i in range(1,n+1):
                if Q[i]==1 and P[i]==-1:
                    P[i]=m
                    R.append(N[i])
        QQ = R
        m=m+1
    
n = int(input())
A = [[0 for j in range(n+1)] for i in range(n+1)]


for i in range(n):
    vec = input().split()
    u = int(vec[0])
    k = int(vec[1])
    nodes = vec[2:]
    for i in range(int(k)):
        v = int(nodes[i])
        A[u][v] = 1

P=[-1 for i in range(n+1)]
P[1]=0
breathSerch(P,A)
for i in range(1,n+1):
    print(i,P[i]) def breathSerch(P,N):
    n=len(P)-1
    m=1
    QQ=[N[1]]
    while(QQ != []):
        R=[]
        for Q in QQ:
            for i in range(1,n+1):
                if Q[i]==1 and P[i]==-1:
                    P[i]=m
                    R.append(N[i])
        QQ = R
        m=m+1
     breathSerch n=len(P)-1 n len(P)-1 len(P) len P 1 m=1 m 1 QQ=[N[1]] QQ [N[1]] N[1] N 1 while(QQ != []):
        R=[]
        for Q in QQ:
            for i in range(1,n+1):
                if Q[i]==1 and P[i]==-1:
                    P[i]=m
                    R.append(N[i])
        QQ = R
        m=m+1
     QQ != [] QQ [] R=[] R [] for Q in QQ:
            for i in range(1,n+1):
                if Q[i]==1 and P[i]==-1:
                    P[i]=m
                    R.append(N[i])
         Q QQ for i in range(1,n+1):
                if Q[i]==1 and P[i]==-1:
                    P[i]=m
                    R.append(N[i])
         i range(1,n+1) range 1 n+1 n 1 if Q[i]==1 and P[i]==-1:
                    P[i]=m
                    R.append(N[i])
         Q[i]==1 and P[i]==-1 Q[i]==1 Q[i] Q i 1 P[i]==-1 P[i] P i -1 P[i]=m P[i] P i m R.append(N[i]) R.append R append N[i] N i QQ = R QQ R m=m+1 m m+1 m 1 P P N N n = int(input()) n int(input()) int input() input [0 for j in range(n+1)] for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) [0 for j in range(n+1)] A = [[0 for j in range(n+1)] for i in range(n+1)] A [[0 for j in range(n+1)] for i in range(n+1)] for i in range(n):
    vec = input().split()
    u = int(vec[0])
    k = int(vec[1])
    nodes = vec[2:]
    for i in range(int(k)):
        v = int(nodes[i])
        A[u][v] = 1 i range(n) range n vec = input().split() vec input().split() ().split () input split u = int(vec[0]) u int(vec[0]) int vec[0] vec 0 k = int(vec[1]) k int(vec[1]) int vec[1] vec 1 nodes = vec[2:] nodes vec[2:] vec 2: 2 for i in range(int(k)):
        v = int(nodes[i])
        A[u][v] = 1 i range(int(k)) range int(k) int k v = int(nodes[i]) v int(nodes[i]) int nodes[i] nodes i A[u][v] = 1 A[u][v] [u] A u v 1 -1 for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) -1 P=[-1 for i in range(n+1)] P [-1 for i in range(n+1)] P[1]=0 P[1] P 1 0 breathSerch(P,A) breathSerch P A for i in range(1,n+1):
    print(i,P[i]) i range(1,n+1) range 1 n+1 n 1 print(i,P[i]) print i P[i] P i n = int(input()) int(input()) n P=[-1 for i in range(n+1)] [-1 for i in range(n+1)] P A = [[0 for j in range(n+1)] for i in range(n+1)] [[0 for j in range(n+1)] for i in range(n+1)] A vec = input().split() input().split() vec nodes = vec[2:] vec[2:] nodes k = int(vec[1]) int(vec[1]) k v = int(nodes[i]) int(nodes[i]) v def breathSerch(P,N):
    n=len(P)-1
    m=1
    QQ=[N[1]]
    while(QQ != []):
        R=[]
        for Q in QQ:
            for i in range(1,n+1):
                if Q[i]==1 and P[i]==-1:
                    P[i]=m
                    R.append(N[i])
        QQ = R
        m=m+1
     def breathSerch(P,N):
    n=len(P)-1
    m=1
    QQ=[N[1]]
    while(QQ != []):
        R=[]
        for Q in QQ:
            for i in range(1,n+1):
                if Q[i]==1 and P[i]==-1:
                    P[i]=m
                    R.append(N[i])
        QQ = R
        m=m+1
     breathSerch u = int(vec[0]) int(vec[0]) u
def breathSerch(P,N):
    n=len(P)-1
    m=1
    QQ=[N[1]]
    while(QQ != []):
        R=[]
        for Q in QQ:
            for i in range(1,n+1):
                if Q[i]==1 and P[i]==-1:
                    P[i]=m
                    R.append(N[i])
        QQ = R
        m=m+1
    
n = int(input())
A = [[0 for j in range(n+1)] for i in range(n+1)]


for i in range(n):
    vec = input().split()
    u = int(vec[0])
    k = int(vec[1])
    nodes = vec[2:]
    for i in range(int(k)):
        v = int(nodes[i])
        A[u][v] = 1

P=[-1 for i in range(n+1)]
P[1]=0
breathSerch(P,A)
for i in range(1,n+1):
    print(i,P[i])