Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

C++: //Breadth First Search
#if 0
?????¨?????????30??????
??????????????¨??????????????¢?´¢????????¬????????¨??¢?????´????????????????£?
#endif
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n;
int D[100];
int matrix[100][100];
void bfs(int src)
{
    //cerr<<"bfs root = "<<src<<endl;
    queue<int> q;
    q.push(src);
    D[src]=0;
    while(!q.empty())
    {
        int cur=q.front();
        q.pop();
        //cerr<<"visiting "<<cur<<' '<<D[cur]<<endl;
        for(int dst=0;dst<n;dst++)
        {
            if(matrix[cur][dst]==1&&D[dst]==-1)
            {
                D[dst]=D[cur]+1;
                q.push(dst);
            }
        }
    }
};

int main()
{
    vector<int> l[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            int tmp;
            cin>>tmp;
            l[u].push_back(tmp);
        }
    }
    
    for(int i=0;i<100;i++)
    {
        D[i]=-1;
    }
    
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            matrix[i][j]=0;
        }
    }
    
    for(int i=1;i<n+1;i++)
    {
        for(int j=0;j<l[i].size();j++)
        {
            matrix[i-1][l[i][j]-1]=1;
        }
    }
    
    bfs(0);
    
    for(int i=0;i<n;i++)
    {
        cout<<(i+1)<<' '<<D[i]<<endl;
    }
}

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

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

int n,M[N][N];
int d[N]; // ?????¢??§?¨??????¶???(color)???????????????

void bfs(int s){
	queue<int> q; // ?¨??????????????????????queue????????¨
	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(); // ?????\???????????????u???????????????
		for(int v=0;v<n;v++){
			if(M[u][v] == 0) continue; // ??????u????????????v??????????????????????????°??????
			if(d[v] != INFTY) continue; // ??????v????????§????¨???????????????¨???????????°??????
			d[v] = d[u] + 1; // ??????v????????????u?????°?????????+1?????°??????????????\?????????
			q.push(v); // ?????\????????????v?????\??????
		}
	}
	for(int i=0;i<n;i++){
		cout << i+1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl; // ??????i?????°????????????INFTY??????-1???????????\???????????°????????????????????????
	}
}

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 <queue>
using namespace std;

int n;
int d[101];
bool a[101][101];
const int INF = (1<<21);

void bfs(int s)
{
    queue<int> q;
    q.push(s);
    for(int i=0; i<n; i++) d[i] = INF;
    d[s] = 0;
    
    int u;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        for(int v=0; v<n; v++)
        {
            if(a[u][v] && d[v] == INF)
            {
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }
}


int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            a[i][j] = false;
        }
    }

    for(int i=0; i<n; i++)
    {
        int u, k;
        cin >> u >> k;
        for(int j=0; j<k; j++)
        {
            int v;
            cin >> v;
            a[u-1][v-1] = true;
        }
    }
    
    bfs(0);

    for(int i=0; i<n; i++)
    {
        cout << i+1 << " " << ((d[i] == INF) ? (-1) : d[i]) << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <string>
#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;i<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 <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll

static const int N = 100;
static const int no = -1;

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll graph[N][N] = {{}}, d[N] = {}, n;

void bfs(int v) {
  queue<int> q;
  q.push(v);
  ll u;
  for(int i = 1; i <= n; i++) d[i] = no;
  d[v] = 0;
  while(!q.empty()) {
    u = q.front(); q.pop();
    for(int i = 1; i <= n; i++) {
      if(graph[u][i] == 0) continue;
      if(d[i] != no) continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
}

signed main() {
	ll u, v, k;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      graph[u][v] = 1;
    }
  }

  bfs(1);

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

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int N=100,INFTY=(1<<21);
int n,M[N][N],d[N];
void bfs(int s){
    std::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(void){
    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);
}