Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <unordered_map> //???????????\??¢??°
#include <fstream> //ifstream, ofstream

//#define NDEBUG //NDEBUG???#include <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert


using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???                           ????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#define din cin
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout dummy.str(""); dummy.clear(stringstream::goodbit); dummy //dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢????????????
//#define OUTPUTFILE "output.txt" //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif

//?¨??????\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢????????????
//#define INPUTFILE "input.txt" //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif


#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << setw(3) << (A) << " " // << setw(3) ??????????????\????????????
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;


const int INF = 1e9-1;

int N;

const int N_MAX = 101;
//all array 1-based index
bool adjacencyMatrix[N_MAX][N_MAX] = {0};
int adjacencyList[N_MAX][N_MAX] = {0};

int currentTime = 1;
int discoveredTime[N_MAX] = {0};
int finishedTime[N_MAX] = {0};

int minDistanceFrom1[N_MAX];
int from[N_MAX]; //??????????????¨

int q[N_MAX]; //?????§100????????§????´????????????§???????????\??? ???????????¨?????????101?????????????????\??????????£????????????±??????1??????????????????????????????????????????
int qHead = 0;
int qTail = 0;


bool qIsEmpty() {
    return (qHead==qTail);
}

bool qIsFull() {
    return qHead == (qTail+1)%N_MAX;
}

void enq(int i) {
    assert(!qIsFull());
    
    q[qTail++] = i;
    
    qTail %= N_MAX;
}

int deq() {
    assert(!qIsEmpty());
    
    int ret = q[qHead++];
    
    qHead %= N_MAX;
    
    return ret;
}

void dispq() {
    disp(qHead);
    disp(qTail);
    dispAll(q, 15);
}



void bfs(int i) {
    

}


void visit(int i) {
//    disp(src); disp(i);
//    dispAll(discoveredTime, n+3);
//    dispAll(finishedTime, n+3);
    
    if(discoveredTime[i]!=0) return;
    
    discoveredTime[i]=currentTime++;
    
    int j = 1;
    while(adjacencyList[i][j] != 0) {
//        disp(j);
//        disp(adjacencyList[i][j]);
        
        assert(j<=N);
        //??????????????¨??¨??§N?????§??????????????¨??????????????¨??????????????????list????????§??§???N-1??????????????°???????????§???0???N-1?????§???????????°??????adjacencyList[i][N]????????¶???0???????????????
        
        visit(adjacencyList[i][j]);
        j++;
    }
    
    finishedTime[i] = currentTime++;
    
    return;
}



int main() {
    din>>N;
    
    int s, g, numOfAdjacent;
    
    
    //input
    rep(i,1,N+1) {
        din>>s>>numOfAdjacent;
        
        rep(j,1,numOfAdjacent+1) {
            din>>g;
            adjacencyMatrix[s][g] = 1;
            adjacencyList[s][j] = g;
        }
    }
    
//    //test display
//    dout<<"   dist = ";
//    rep(j,1,N+1) dout<<j<<" ";
//    dout<<endl;
//    dout<<"          ";
//    rep(j,1,N+1) dout<<"-"<<" ";
//    dout<<endl;
//    
//    rep(i,1,N+1) {
//        dout<<"src = "<<i<<" | ";
//        rep(j,1,N+1) {
//            dout<<(j==1?"":" ")<<adjacencyMatrix[i][j];
//        }
//        dout<<endl;
//    }
//    
//    dout<<endl;
//    dout<<"                       ";
//    rep(j,0,N+1) disP(j);
//    dout<<endl;
//    dout<<"                       ";
//    rep(j,0,N+1) dout<<"  - ";
//    dout<<endl;
//    rep(i,1,N+1) {
//        disP(i); dispAll(adjacencyList[i], N+1);
//    }
    
    
    
//    //DFS (Depth First Search)
//    rep(i,1,N+1) {
//        if(discoveredTime[i]==0) visit(i);
//    }

//    dispAll(discoveredTime, N+1);
//    dispAll(finishedTime, N+1);
    
    
//    //output
//    rep(i,1,N+1) {
//        dout<<i<<" "<<discoveredTime[i]<<" "<<finishedTime[i]<<endl;
//    }
    
    
    
    //BFS (Breadth First Search) ????????????1????????????????????????????????¢????±???????
    
    //initialize
    minDistanceFrom1[1] = 0;
    rep(i,2,N+1) minDistanceFrom1[i] = INF;
    
    enq(1);
    int currentNode;
    
    while( not qIsEmpty() ) {
//        dispq();
        
        currentNode = deq();
    
//        disp(currentNode);
        
        int j = 1;
        int nextNode = adjacencyList[currentNode][j];
        while( nextNode != 0 ) {
//            disp(j); disp(nextNode);
            
            if( minDistanceFrom1[ nextNode ] == INF ) { //INF??§???????????°????????§????¨????????????§????????????????¨?????????????????????????1??????????????????????????§???????????\???????¨??????????????????????
                enq(nextNode);
                
                assert(minDistanceFrom1[ currentNode ] + 1 < minDistanceFrom1[ nextNode ]);
                if( minDistanceFrom1[ currentNode ] + 1 < minDistanceFrom1[ nextNode ] ) { //??¶??£???????????????????¨???????????????¨????????????????°?????????´??°????????????INF??????1???????????´??°????????????????????\????????????????¨????????????????????????????
                    from[ nextNode ] = currentNode;
                    minDistanceFrom1[ nextNode ] = minDistanceFrom1[ currentNode ] + 1;
                }
            }
            
            j++;
            nextNode = adjacencyList[currentNode][j];
        }
    }
    
    
    
//    dispAll(minDistanceFrom1, N+1);
    
    
    rep(i,1,N+1) {
        dout<<i<<" "<<(minDistanceFrom1[i]==INF?-1:minDistanceFrom1[i])<<endl;
    }
    
    
    
    
#ifdef INPUTFILE
    inputfile.close();
#endif
    
#ifdef OUTPUTFILE
    outputfile.close();
    cout << "\"" << OutputFilePath << "\"" << endl;
#endif
    
    return 0;
}