Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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