Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

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

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)
	paths[1] = 1

	for len(queue) > 0 {
		u := queue.pop()
		d := paths[u] + 1
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<bits/stdc++.h>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)

using namespace std;

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


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

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

int main() {
  int u, k, v;

  cin >> n;
  REP(i, n) {
    REP(j, n) M[i][j] = 0;
  }

  REP(i, n) {
    cin >> u >> k;
    u--;
    REP(j, k) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  bfs(0);
  return 0;
}

C++: # include <iostream>
# include <vector>
# include <cstdio>
# include <queue>
using namespace std;
struct info
{
	int d = -1;
	bool vis = false;
};
vector<int> a[105];
info *ans;
queue<int> solve;
void bfs()
{
	solve.push(1); 
	ans[1].vis = true; ans[1].d = 0;
	while(!solve.empty())
	{
		int u = solve.front(); solve.pop();
		for(int j = 0;j < a[u].size();j++)
		{
			if(!ans[a[u][j]].vis)
			{
				ans[a[u][j]].vis = true;
				ans[a[u][j]].d = ans[u].d + 1;
				solve.push(a[u][j]);
			}
		}
	}
	
}
int main(void)
{
	int n,cnt,k,u;
	scanf("%d",&n);
	ans = new info [n+1];
	for(int w = 0;w < n;w++)
	{
		scanf("%d %d",&u,&cnt);
		for(int i = 0;i < cnt;i++)
		{
			scanf("%d",&k);
			a[u].push_back(k);
		}
	}
	bfs();
	for(int i = 1;i <= n;i++) printf("%d %d\n",i,ans[i].d);
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#define MAX 101
using namespace std;

int n;
int a[MAX][MAX]={};
int d[MAX];

void track(int v,int depth){
  if(d[v]>depth||d[v]==-1) d[v]=depth;
  for(int i=1;i<=n;i++){
    if(a[v][i]==1){
      track(i,depth+1);
    }
  }
}

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

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

  track(1,0);

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

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN = 100 + 4;
const int INF = 1000000;
int M[MAXN][MAXN];
int d[MAXN];

int main()
{
    int N, u, k, m;
    memset ( M, 0, sizeof(M) );
    d[1] = 0;
    cin>>N;
    for ( int i = 0; i < N; i++ ) {
        cin>>u>>k;
        for ( int j = 0; j < k; j++ ){
            cin>>m;
            M[u][m] = 1;
        }
    }


    queue<int> q;
    q.push(1);
    for ( int i = 0; i <= N; i++ ) d[i] = INF;
    d[1] = 0;
    while ( !q.empty() ) {
        int u = q.front(); q.pop();
        for ( int v = 1; v <= N; v++ ) {
            if ( M[u][v] == 0 ) continue;
            if ( d[v] != INF ) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }

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

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

int n, u, k, v;
int a[100][100] = {{0}};
int b[100];
queue<int> q;

int main() {
	for ( int i = 0; i < 100; i++ )
		b[i] = - 1;
	cin >> n;
	for ( int i = 0; i < n; i++ ) {
		cin >> u >> k;
		for ( int j = 0; j < k; j++ ) {
			cin >> v;
			a[u - 1][v - 1] = 1;
		}
	}
	b[0] = 0;
	q.push( 0 );
	while ( ! q.empty() ) {
		u = q.front();
		for ( int i = 0; i < n; i++ ) {
			if ( a[u][i] == 1 && b[i] == -1 ) {
				b[i] = b[u] + 1;
				q.push( i );
			}
		}
		q.pop();
	}
	for ( int i = 0; i < n; i++ )
		cout << ( i + 1 ) << " " << b[i] << endl;
}

C++: #include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
using namespace std;

int d[100];
int f[100];
int map[100][100];
int dis;
int n;
queue<int> q;
void visit(int);
void searchQ(int);

int main(){
	dis=0;
	int i,j,k,u,v;
	for(i=0;i<100;i++) d[i]=-1; //?????¢??¨?????¢??\
	cin >> n;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			map[i][j]=0;
		}
	}
	for(i=0;i<n;i++){
		cin>>u>>k;
		for(j=0;j<k;j++){
			cin>>v;
			map[u-1][v-1]=1; //?????????????????????????????????????????????
		}
	}
	visit(0);
	for(i=0;i<n;i++) cout<< i+1 <<" "<<d[i]<<endl;
	return 0;
}

void visit(int a){
	q.push(a);
	d[a]=0;

	while(!q.empty()){
		int i=q.front();
		q.pop();
		searchQ(i);
		
	}
}

void searchQ(int a){//?????\????????¶?????????
	for(int i=0;i<n;i++){
		if(map[a][i]==1 && d[i]==-1){
			q.push(i);
			d[i]=d[a]+1;
		}
	}
}

C++: #include "taichitary.h"
#define V_MAX 100

int m[V_MAX][V_MAX];
int cnt[V_MAX];
int count=0;
int n;

void bfs(int u){

    queue<int> Q;
    Q.push(u);
    cnt[u] = 0;
    int v = Q.front();

    while(!Q.empty()){

        for(int i=0; i<n; i++){
            if(m[v][i] == 1 && cnt[i] == -1){
                Q.push(i);
                cnt[i] = cnt[v]+1;
            }
        }

        Q.pop();
        v = Q.front();
    }
}


int main(){

    cin >> n;

    int from, deg;

    for(int i=0; i<n; i++){
        cin >> from >> deg;
        from--;
        for(int j=0; j<deg; j++){
            int tmp;
            cin >> tmp;
            tmp--;
            m[from][tmp] = 1;
        }
    }

    for(int k=0; k<n; k++) cnt[k] = -1; //?????????

    bfs(0);

    for(int j=0; j<n; j++){
        cout << j+1 << " " << cnt[j] << endl;
    }


}

C++: from collections import deque
n = int(input())
A = [0]*(n+1)
D = [-1]*(n+1)
DP = [0]*(n+1)
d = deque()
for i in range(n):
    A[i+1] = [int(i) for i in input().split()]
d.append(1)
D[1] = 0
DP[1] = 1
while(len(d)>0):
    tmp = d.pop()
    for i in A[tmp][2:]:
        if not DP[i]:
            D[i] = D[tmp]+1
            DP[i] = 1
            d.appendleft(i)
for i in range(1,n+1):
    tmp = "{} {}".format(i,D[i])
    print(tmp)