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;

int main(){
  queue<int> q;
  int n,k,u,max=0;
  int g[101]={},c[101];
  int d[101][101]={};
  int di;
  cin>>n;
  for(int i=0;i<101;i++) c[i]=-1;
  c[1]=0;
  g[1]=1;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    if(max<k) max=k;
    for(int j=0;j<k;j++){
      cin>>di;
	d[u][j]=di;
    }
  }

  q.push(1);

  int a;
  while(!q.empty()){
 a=q.front();
    for(int i=0;i<=max;i++){

      if( d[a][i]>1 && g[d[a][i]]==0){
	g[d[a][i]]=1;
	q.push(d[a][i]);
	 c[d[a][i]]=c[a]+1;
      }
    }
      q.pop();
  }
  for(int i=1;i<=n;i++) cout<<i<<" "<<c[i]<<endl;
  return 0;
}

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

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(),(v).end()

using namespace std;

using ll=long long;
using pii=pair<int, int>;
using vi=vector<int>;

const ll INF=1LL<<60;
const int MOD=1e9+7;

int main(){
    int n; cin >> n;
    vector<vi> G(n, vi(n));
    for(int i=0; i<n; i++){
        int u, k; cin >> u >> k;
        for(int j=0; j<k; j++){
            int v; cin >> v;
            G[u-1].push_back(v-1);
        }
    }
    queue<int> que;
    que.push(0);
    vi dist(n, -1);
    dist[0]=0;
    while(!que.empty()){
        int v=que.front();
        que.pop();
        for(auto &nv: G[v]){
            if(dist[nv] != -1) continue;
            dist[nv]=dist[v]+1;
            que.push(nv);
        }
    }
    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];

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 <bits/stdc++.h>
using namespace std;

int main()
{

	int n;
	cin >> n;
	vector<vector<int>> g(n+1);

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

	vector<int> dist(n+1);
	for (int i=1;i<=n;++i)
	{
		dist[i]=-1;
	}
	queue<int> q;

	q.push(1);
	dist[1]=0;
	while (q.empty()==false)
	{
		int node=q.front();
		q.pop();

		for (int i=0;i<g[node].size();++i)
		{
			int nn=g[node][i];
			if (dist[nn]<0)
			{
				dist[nn]=dist[node]+1;
				q.push(nn);
			}
		}
	}

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

C++: #include <stdio.h>
#include <stdlib.h>

struct node_t {
    int key;
    struct node_t *next;
};
typedef struct node_t Node;

Node *enqueue(Node *node, int key) {
  Node *p, *q;

  if (node == NULL) {
    node = malloc(sizeof(Node));
    node->key = key;
    node->next = NULL;
    return node;
  }

  for (p = node; p != NULL; p = p->next) {
    q = p;
  }
  q->next = malloc(sizeof(Node));
  q->next->key = key;
  q->next->next = NULL;
  return node;
}

Node *dequeue(Node *node, int *key) {
  Node *p;

  if (node != NULL) {
    *key = node->key;
    p = node->next;
    free(node);
        return p;
  }
  return NULL;
}

int main(void) {
  int num, i, j, k, u;
  int way[101][101] = {{0}};
  int d[101];
  Node *q = NULL;

  scanf("%d", &num);
  for (i = 0; i < num; i++) {
    scanf("%d %d", &u, &k);
    for (j = 0; j < k; j++) {
      scanf("%d", &way[u-1][j]);
    }
  }

  d[0] = 0;
  for (i = 1; i < num; i++) {
    d[i] = -1;
  }
  q = enqueue(q, 0);

  while (q != NULL) {
    q = dequeue(q, &u);
    for (i = 0; i < num; i++) {
      k = way[u][i] - 1;
      if (d[k] < 0) {
    d[k] = d[u] + 1;
    q = enqueue(q, k);
      }
    }
  }

  for (i = 0; i < num; i++) {
    printf("%d %d\n", i+1, d[i]);
  }
  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];
intd  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 <queue>
#include <vector>

using namespace std;

const int N = 100;


int main() {
    int A[N][N] = {{0}};

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int u;
        cin >> u;

        int k;
        cin >> k;
        for (int j = 0; j < k; ++j) {
            int x;
            cin >> x;
            A[u-1][x-1] = 1;
        }
    }

    vector<int> d(n, -1);
    using int_pair = pair<int, int>;
    queue<int_pair> Q;
    Q.push(int_pair(0, 0));
    while (!Q.empty()) {
        auto p = Q.front();
        Q.pop();

        int k = p.first;
        int t = p.second;
        if (d[k] > t)
            continue;

        if (d[k] == -1) {
            d[k] = t;
        } else {
            d[k] = min(d[k], t);
        }
        for (int i = 0; i < n; ++i) {
            if (A[k][i] != 0)
                Q.push(int_pair(i, t + 1));
        }
    }

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

    return 0;
}