Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

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