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 <queue>

using namespace std;

const int N = 100;
const int Inf = 1e9;

int m[N][N];
int d[N];
int n;

void bfs(int);

int main(int argc, char const *argv[]) {
  cin >> n;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      m[i][j] = 0;
    }
    d[i] = -1;
  }

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

  return 0;
}

void bfs(int s) {
  queue<int> q;
  q.push(s);
  d[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < n; ++i) {
      if (m[u][i] == 0 || d[i] != -1) {
        continue;
      }
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << i + 1 << " " << d[i] << '\n';
  }
}

C++: //By Vlgd
#include<bits/stdc++.h>
using namespace std;
int G[101][101],n,dis[101],vis[101]={0},cur=0;
queue<int>q;
void bfs(int u){
	q.push(u);
	vis[u]=1;
	dis[u]=0;
	while(!q.empty()){
		int w=q.front();q.pop();
		for (int v=1;v<=n;v++) 
			if (G[w][v]&&!vis[v]){ 
				dis[v]=dis[w]+1;
				q.push(v);
				vis[v]=1;
			}
	}
}
main(){
	int u,v,deg;
	scanf("%d",&n);
	for (int i=1;i<=n;i++) dis[i]=-1;
	for (int i=0;i<n;i++){
		scanf("%d%d",&u,&deg);
		for (int j=0;j<deg;j++){
			scanf("%d",&v);
			G[u][v]=1;
		}
	}
	bfs(1);
	for (int i=1;i<=n;i++) printf("%d %d\n",i,dis[i]);
}

C++: #include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAX 100+5
int num;
int m[MAX][MAX],a[MAX],d[MAX],color[MAX];//color[i]中0--白色；1--灰色；2--黑色； 
void bfs(int v){
	queue<int> q;
	q.push(v);d[v]=0;
	color[v]=1;
	int shou;
	while(!q.empty()){
	shou=q.front();q.pop();
	for(int i=1;i<=num;i++){
		if(m[shou][i]==1&&color[i]==0){
			q.push(i);d[i]=d[shou]+1;color[i]=1;
		}
		color[shou]=2;
	}
	}
	return ;
}
int main(){
	memset(m,0,sizeof(m));
	memset(color,0,sizeof(color));
	memset(d,-1,sizeof(d)); 
	cin>>num;
	for(int i=1;i<=num;i++){
		int cnt;
		cin>>a[i]>>cnt;
		for(int j=0;j<cnt;j++){
			int c;
			cin>>c;
			m[a[i]][c]=1; 
		}
	}
	bfs(1);
	for(int i=1;i<=num;i++){
		cout<<a[i]<<" "<<d[a[i]]<<endl;
	}
	
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
using namespace std;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef long long ll;
#define range(i,a,b) for(int i=a;i<b;++i)
#define rep(i,n) range(i,0,n)
#define INF 10000000000 //10^10
#define PI 3.141592
#define pb push_back

int main(){
    //内部処理は0indexで行う

    //入力
    int n;
    cin >> n;
    vector<vint> adj(n);
    rep(i,n){
        int u,k;
        cin >> u >> k;
        rep(j,k){
            int v;
            cin >> v;
            adj[u-1].pb(v-1);
        }
    }

    //計算
    vint d(n,-1);
    d[0]=0;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()){
        int cur=Q.front(); Q.pop();
        rep(i,adj[cur].size()){
            int dst=adj[cur][i];
            if(d[dst]==-1){
                d[dst]=d[cur]+1;
                Q.push(dst);
            }
        }
    }

    //出力
    rep(i,n){
        cout << i+1 << ' ' << d[i] << endl;
    }
}

C++: #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < (n); ++i)
#define FOR(i, m, n) for(ll i = m; i < (n); i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vii = vector<vi>;
using pii = pair<int, int>; 
using vl = vector<ll>;
using vll = vector<vl>;
using pll = pair<ll, ll>;

int main() {
    ll n;
    cin >> n;
    vll G(n);
    rep(i, n){
        ll u, k;
        cin >> u >> k;
        u--;
        rep(j, k){
            ll a;
            cin >> a;
            a--;
            if(a < 0) continue;
            G[u].push_back(a);
        }
    }
    
    vl dist(n, -1);
    queue<ll> que;
    dist[0] = 0;
    que.push(0);
    while(!que.empty()){
        ll v = que.front();
        que.pop();

        for(auto nv : G[v]){
            if(dist[nv] != -1) continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    rep(i, n){
        cout  << i+1 << " " <<  dist[i] << endl;
    }
}

C++: #include <stdio.h>
#include <queue>
#include <algorithm>

#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;

int n;
int M[N][N]; // ??£??\??????
int d[N]; // ???????????¢


void bfs()
{
    int i;
    for (i = 0; i < n; i++) {
        d[i] = -1;
    }
    
    int u;
    u = 0;
    d[u] = 0;
    queue<int> q;
    q.push(u);
    
    while (!q.empty()) {
        u = q.front();
        q.pop();
        for (i = 0; i < n; i++) {
            if (M[u][i] == 0) continue;
            if (d[i] != -1) continue;
            q.push(i);
            d[i] = d[u] + 1;
        }
    }
    
    for (i = 0; i < n; i++) {
        printf("%d %d\n", i+1, d[i]);
    }
}


int main()
{
    scanf("%d", &n);
    int i, j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            M[i][j] = 0;
        }
    }
    
    // input graph
    int u, k, v;
    for (i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        for (j = 0; j < k; j++) {
            scanf("%d", &v);
            M[u-1][v-1] = 1;
        }
    }
    
    bfs();
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

vector<vector<int>> g;
vector<int> d;

int main()
{
	int n;
	cin >> n;
	g = vector<vector<int>>( n );
	d = vector<int>( n );
	for( int i = 0; i < n; i++ ) {
		int u, k;
		cin >> u >> k;
		u--;
		for( int j = 0; j < k; j++ ) {
			int v;
			cin >> v;
			v--;
			g[u].push_back( v );
		}
	}

	queue<pair<int, int>> que;
	que.push( make_pair( 0, 1 ) );
	while( !que.empty() ) {
		int v, l;
		tie( v, l ) = que.front();
		que.pop();
		if( d[v] == 0 ) d[v] = l;
		for( int u : g[v] ) {
			if( d[u] ) continue;
			que.push( make_pair( u,  l + 1 ) );
		}
	}

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