Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

C++: #include <bits/stdc++.h>

#define FOR(i, a, b) for(long long int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LLINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;

int n;
int u, k, v;
int hour = 0;
int pos;
int d[101];
int gra[101][101] = {};
int color[101] = {};
queue<int>que;



int main(void) {
	cin >> n;
	
	FOR(i, 1, n) {
		cin >> u >> k;
		FOR(t, 1, k) {
			cin >> v;
			gra[u][v] = 1;
		}
	}

	FOR(i, 1, n) {
		d[i] = -1;
	}

	que.push(1);
	d[1] = hour;
	
	while (que.empty() != 1) {
		pos = que.front();
		que.pop();
		FOR(i, 1, n) {
			if (gra[pos][i] == 1 && color[i] == 0) {
				que.push(i);
				d[i] = d[pos] + 1;
				color[i] = 1;
			}
		}
	}

	FOR(i, 1, n) {
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
void debug1();
int n, gragh[110][110] = {}, d[110] = {};
queue<int> q;

int main() {
	cin >> n;

	//inicialize
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			gragh[i][j] = -1;
		}
	}
	//input
	for (int i = 0; i < n; i++) {
		int u,k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int t;
			cin >> t;
			gragh[u][t] = 1;
		}
	}
	//debug1();
	//search
	d[1] = 0;
	q.push(1);
	while (!q.empty()) {
		int cur = q.front();
		q.pop();
		//cerr << "visiting " << cur << " Distance: " << d[cur] << endl;
		for (int i = 1; i <= n; i++) {
			if (d[i] == 0 && gragh[cur][i] == 1) {
				d[i] = d[cur] + 1;
				q.push(i);
			}
		}
	}
	//output
	for (int i = 1; i <= n; i++) {
		cout << i << " " << d[i] << endl;
	}
	return 0;
}

void debug1() {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cerr << gragh[i][j] << " " ;
		}
		cerr << endl;
	}
}

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;
  whie(!q.empty){
    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;
    }
  }
  for(int =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<k;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++: #define _LIBCPP_DEBUG 0
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1e9 + 7;

vector<int> G[100+1];
vector<int> dist(100+1,-1);

void bfs(int x){
    dist[x] = 0;
    queue<int> PQ;
    PQ.push(x);

    while (!PQ.empty()){
        int tmp; tmp = PQ.front(); PQ.pop();
        for (int i = 0; i < G[tmp].size(); ++i) {
            if(dist[G[tmp][i]]==-1){
                dist[G[tmp][i]] = dist[tmp] + 1;
                PQ.push(G[tmp][i]);
            }
        }
    }

}


int main(void) {

    int n; cin>>n;

    for (int i = 1; i <= n; ++i) {
        int u,k; cin>>u>>k;
        for (int j = 1; j <= k; ++j) {
            int v; cin>>v;
            G[u].push_back(v);
        }
    }

    bfs(1);

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

    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <sstream>
#include <string>
#include <utility>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>

using namespace std;

class BFSearch
{
public:
  int solve();
};

int BFSearch::solve()
{
  int n;
  cin >> n;

  vector<vector<int>> adj( n + 1, vector<int>( n + 1, -1));

  int u, k, v;

  for ( auto j = 0; j < n; ++j )
    {
      cin >> u >> k;

      for ( auto i = 0; i < k; ++i )
        {
          cin >> v;
          adj[u][v] = 1;
        }
    }
  
  queue<int> q;
  vector<int> d(n + 1, -1);

  u = 1;
  d[u] = 0;
  q.push(u);

  while ( !q.empty() )
    {
      u = q.front();
      
      for ( auto i = 1; i <= n; ++i )
        {
          if ( adj[u][i] == 1 && d[i] == -1 )
            {
              d[i] = d[u] + 1;
              q.push( i );
            }
        }
      
      q.pop();
    }
  
  for ( auto i = 1; i <= n; ++i )
    cout << i << " " << d[i] << endl;
  
  return 0;
}

int main()
{
  BFSearch b;

  b.solve();

  return 0;
}

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

#define MAX_NODE 10000
#define INF 1000000000

using namespace std;

int d[MAX_NODE];
vector<int> adj[MAX_NODE];

int bfs(int n, int start){
	queue<int> que;
	for(int i=0;i<n;i++){
		d[i]=(i==start)?0:-1;
	}
	que.push(start);
	while(que.empty()==0){
		int from=que.front();
		que.pop();
		for(int i=0;i<adj[from].size();i++){
			int to=adj[from][i];
			if(d[to]==-1){
				d[to]=d[from]+1;
				que.push(to);
			}
		}
	}
}	

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

C++: #include<iostream>
#include<queue>
using namespace std;
int N;
int D[100];
void bfs(int src){
    int u[100],k[100],a[100],d[100][100];
    bool visit[100];
    for(int i=1;i<=N;++i){
        cin>>u[i]>>k[i];
        for(int j=0;j<k[i];++j){
            cin>>a[j];
            d[u[i]][a[j]]=1;
        }
        visit[i]=true;
    }
    queue<int>Q;
    Q.push(src);
    D[src]=0;
    visit[src]=false;
    while(!Q.empty()){
        int cur=Q.front();
        Q.pop();
        for(int i=1;i<=N;++i){
            if(d[cur][i]==1&&visit[i]){
                visit[cur]=false;
                visit[i]=false;
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
    for(int i=1;i<=N;++i){
        if(i!=1&&D[i]==0){
            D[i]=-1;
        }
        cout<<i<<" "<<D[i]<<endl;
    }
}
int main(){
    cin>>N;
    int start=1;
    bfs(start);
}