Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

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

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;
    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;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 <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;
            q.push(i);
            if (d[i] == -1) {
                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<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; 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<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define N 1000
bool M[N][N];

int main(){
    int n,u,k,v;
    queue<int> Q;
 
    cin >> n;
    for(int i=0;i<n;i++){
	cin >> u >> k;
	u--;
	for(int j=0;j<k;j++){
	    cin >> v;
	    v--;
	    M[u][v]=true;
	}
    }
   
    int d[N]={0};
    Q.push(0);
    while(!Q.empty()){
	u=Q.front();
	Q.pop();
	for(int i=0;i<n;i++){
	    if(M[u][i] && d[i]==0){
		d[i]=d[u]+1;
		Q.push(i);
	    }
	}
    }

    for(int i=0;i<n;i++){
	if(i!=0 && d[i]==0) d[i]=-1;
	printf("%d %d\n",i+1,d[i]);
    }

    return 0;
}

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

using namespace std;

void read_data(int n, vector<vector<int>> & Es){
    int u = 0, k = 0, v = 0;
    while (n--){
        cin >> u;
        cin >> k;
        while (k--){
            cin >> v;
            Es[u - 1].push_back(v - 1);
        }
    }
}

void wfs(int node, vector<int> & dist, vector<vector<int>> & Es){
    int d = 0;
    vector<int> frontiers;
    frontiers.push_back(node);
    while (!frontiers.empty()){
        ++d;
        vector<int> new_frontiers;
        for (auto u : frontiers){
            for (auto v : Es[u]){
                if (dist[v] == -1){
                    dist[v] = d;
                    new_frontiers.push_back(v);
                }
            }
        }
        frontiers = move(new_frontiers);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    int n = 0;
    cin >> n;
    vector< vector<int> > Es(n);
    vector<int> dist(n, -1);
    read_data(n, Es);
    wfs(0, dist, Es);
    for (int i = 0; i != n; ++i) cout << i + 1 << ' ' << dist[i] << endl;
    
    return 0;
}

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]; // ?????¢??§?¨??????¶???(color)???????????????

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; 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 <cstdio>
#include <queue>
#include <vector>

using namespace std;
#define MAX_N (105)
typedef pair<int, int> P;

int n;
vector<int> G[MAX_N];
int dist[MAX_N];
int main()
{
    scanf("%d", &n);
    for ( int i = 0; i < n; i++ ) {
	int u, k;
	scanf("%d %d", &u, &k);
	u--;

	for ( int j = 0; j < k; j++ ) {
	    int v;
	    scanf("%d", &v);
	    v--;
	    G[u].push_back( v );
	}
    }

    for ( int i = 0; i < n; i++ ) dist[i] = -1;
    dist[0] = 0;

    queue<int> Q;
    Q.push( 0 );

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

    for ( int i = 0; i < n; i++ ) {
	printf("%d %d\n", i+1, dist[i]);
    }

    return 0;
}