Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

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

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

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
		}
	}
}

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

C++: #include <iostream>
#include <queue>
using namespace std;
 
#define N 100
#define INFTY 1000000
 
int M[N][N],d[N];
int n,u,k,v;
 
//bfs
void bfs(int s){
 
  queue <int> Q;
  //十分大きな数字に初期化
  for(int i = 0; i < N; i++ ) d[i] = INFTY;
  
  //最初の訪問先
  d[s] = 0;
  Q.push(s);

  //Qが空でないとき
  while ( !Q.empty() ){

    //queueから出す
    u = Q.front(); Q.pop();

    for( int v = 0; v < n; v++ ){

      //つながってないとき
      if( M[u][v] == 0 ) continue;

      //既に訪問ずみのとき
      if( d[v] != INFTY ) continue;

      //時間を入力
      d[v] = d[u] + 1;

      //queueに追加
      Q.push(v);
    }
  }
 
  //出力
  for( int i = 0; i < n; i++ ){
    cout << i+1 << " ";
    if ( d[i] == INFTY ) cout << -1;
    else cout << d[i];
    cout << endl;
  }
 
}
 
 
int main(){

  //初期化
  for( int i = 0; i < n; i++ ){
    for( int j = 0; j < n; j++ ) M[u][v] = 0;
  }
  
  //入力
  cin >> n;
  for( int i = 0; i < n; i++ ){
    cin >> u;
    u--;
    cin >> k;
    for( int j = 0; j < k; j++ ){
      cin >> v;
      v--;
      //つながっているとき
      M[u][v] = 1;
    }
  }
  
  //bfs
  bfs(0);
   
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

using namespace std;

vector<int> d(101, -1);
vector<vector<int> > c(101, vector<int>(101, 0));

int n, u, k, v;
typedef pair<int, int> P; 
int bfs(void) {
    queue<P> que;
    que.push(P(0, 0));
    while(que.size()>0) {
        P p = que.front(); que.pop();
        if(d[p.first]==-1) d[p.first] = p.second;
        for(int i=0; i<n; ++i) {
            if(c[p.first][i]==1 && d[i]==-1) que.push(P(i, d[p.first]+1));
        }
    }
}

int main() {
    cin >> n;
    for(int i=0; i<n; ++i) {
        cin >> u >> k;
        for(int j=0; j<k; ++j) {
            cin >> v;
            c[i][v-1] = 1;
        }
    }
    bfs();
    for(int i=0; i<n; ++i) {
        cout << i+1 << " " << d[i] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define r(i,n) for(auto i=0;i<n;i++)
#define s(c) static_cast<int>((c).size())

int main(){
	int n;
	cin >> n;

	set<int> A[n+1];
	int v1, v2, deg;
	r(i, n){
		cin >> v1 >> deg;

		r(j, deg){
			cin >> v2;
			A[v1].insert(v2);
		}
	}

	queue<int> s;
	s.push(1);
	int dist[n+1]{0, 1};

	while(!s.empty()){
		int v1 = s.front(); s.pop();
		for(int v2 : A[v1]){
			if(!dist[v2]){
				s.push(v2);
				dist[v2] = dist[v1]+1;
			}
		}
	}

	for(int i = 1; i <= n; i++)
		printf("%d %d\n", i, dist[i]-1);

	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;

class Vtx {
public:
	int id;
	int dist = -1;
	vector<int> vtxs;
};

void dfs(int n, Vtx vtxs[]){
	queue<pair<int,int> > q;
	q.push(make_pair(vtxs[0].id, 0));
	while(!q.empty()){
		pair<int,int> p=q.front();
		q.pop();
		int id=p.first-1;
		int d=p.second;
		Vtx v = vtxs[id];
		if(v.dist==-1){
			vtxs[id].dist=d;
		}
		for(int i=0;i<v.vtxs.size();i++){
			int nId=v.vtxs[i];
			if(vtxs[nId-1].dist==-1){
				q.push(make_pair(nId,d+1));
			}
		}
	}
}

int main(){
	int n;
	cin >> n;
	Vtx vtxs[n];
	for(int i=0;i<n;i++){
		int id, k;
		cin >> id >> k;
		vtxs[i].id = id;
		for(int j=0;j<k;j++){
			int v;
			cin >> v;
			vtxs[i].vtxs.push_back(v);
		}
	}
	
	dfs(n, vtxs);
	
	for(int i=0;i<n;i++){
		Vtx v = vtxs[i];
		cout << v.id << " ";
		cout << v.dist << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>

using namespace std;
static const int N=100;
static const int INFTY=(1<<21);

int n,M[N][N];
int d[N];

void bfs(int s){
  queue<int>q;
  q.push(s);
  for(int i=0;i<n;i++)d[i]=INFTY;
  d[s]=0;
  int u;
  while(!q.empty()){
    u=q.front();q.pop();
    for(int v=0;v<n;v++){
      if(M[u][v]==0)continue;
      if(d[v]!=INFTY)continue;
      d[v]=d[u]+1;
      q.push(v);
    }
  }
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<( (d[i]==INFTY) ? (-1):d[i] ) << endl;
  }
}
int main(){
  int u,k,v;
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++)M[i][j]=0;
  }
  for(int i=0;i<n;i++){
    cin>>u>>k;
    u--;
    for(int j=0;j<k;j++){
      cin>>v;
      v--;
      M[u][v]=1;
    }
  }
  bfs(0);
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;

void calc(const vector<vector<int> >& G, vector<int>& D) {
    queue<int> Q1;
    D[1] = 0;
    int dim = 1;
    Q1.push(1);
    while (!Q1.empty()) {
        queue<int> Q2;
        while (!Q1.empty()) {
            int i = Q1.front();
            for (int j = 1; j < G[i].size(); j++) {
                if (G[i][j] && D[j] == -1) {
                    Q2.push(j);
                    D[j] = dim;
                }
            }
            Q1.pop();
        }
        dim++;
        Q1 = Q2;
    }

}

int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n + 1, vector<int>(n + 1, 0));
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            G[u][v] = 1;
        }
    }
    vector<int> D(n + 1, -1);
    calc(G, D);

    for (int i = 1; i <= n; i++) {
        cout << i << " " << D[i] << endl;
    }

    return 0;
}