Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 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;
}