Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1) // 1-indexed
	d := make([]int, n+1)   // 1-indexed
	for i := 1; i <= n; i++ {
		d[i] = -1
		var u, k, v int
		fmt.Scan(&u, &k)
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			g[u] = append(g[u], v)
		}
	}

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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