Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

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;
}