Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#include <map>
#include <bitset>
#include <cstdio>
#include <set>
#include <stack>
#include <queue>
#include <cassert>
#include <numeric>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    REP(i,v.size()){if(i)os<<" ";os<<v[i];}return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
    REP(i,v.size()){if(i)os<<endl;os<<v[i];}return os;}
#define vi vector<int>
#define vii vector<vector<int>>
#define mii map<int,int>
#define Sort(v) sort(v.begin(),v.end())
#define Reverse(v) reverse(v.begin(),v.end())
#define ALL(a)  (a).begin(),(a).end()
#define pb push_back
#define mp make_pair
//#define int ll

const int md = 1000000007;
const int INF = 1<<30;



int main(){
    int n; cin >> n;
    vector<vector<int>> e(n);


    rep(i,n){
        int s; cin >> s;
        int tmp; cin >> tmp;
        rep(j,tmp){
            int to; cin >> to;
            e[i].push_back(to-1);
        }
    }

    vi ans(n,-1);
    rep(i,n){
        //if(i != 3) continue;
        queue<pair<int,int>> bfs;
        bfs.emplace(0,0);
        while(!bfs.empty()){
            auto tmp = bfs.front();
            if(tmp.first == i) {
                ans[i] = tmp.second;
                break;
            }
            bfs.pop();

            rep(j,e[tmp.first].size()){
                bfs.emplace(e[tmp.first][j],tmp.second+1);
            }
        }
        
    }

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

C++: #include <iostream>
#include <queue>
#include <vector>
#include <list>
#include <algorithm>


using namespace std;


using ll = long long;


const ll MAX = 100;


vector<list<ll>> graph;
ll d[MAX + 1] = {};
ll n;
bool visited[MAX + 1] = {};

void bfs()
{
    queue<pair<ll, ll>> q;
    q.push(make_pair(1, 0));
    visited[1] = true;

    while (!q.empty()) {
        auto tmp = q.front(); q.pop();
        ll now = tmp.first; ll dis = tmp.second;

        d[now] = dis;

        for (auto e : graph[now]) {
            if (!visited[e]) {
                visited[e] = true;
                q.push(make_pair(e, dis + 1));
            }
        }
    }
}


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

    bfs();

    for (ll i = 1; i <= n; i++) {
        if (visited[i]) {
            cout << i << " " << d[i] << endl;
        } else {
            cout << i << " " << -1 << endl;
        }
    }
}

C++: #include<iostream>
#include<vector>
#include<queue>
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define N 100
#define INF (1 << 21)
using namespace std;

int n;
int a[N+5][N+5];

vector<int> d;
vector<int> color;

void dfs(int s){
  queue<int> Q;
  
  for(int i = 0; i < n; i++) color[i] = WHITE;
  for(int i = 0; i < n; i++) d[i] = INF;

  color[s] = GRAY;
  d[s] = 0;

  Q.push(s);

  while(!Q.empty()){
    int u = Q.front();
    Q.pop();

    for(int i = 0; i < n; i++){
      if(a[u][i] && color[i] == WHITE){
	color[i] = GRAY;
	d[i] = d[u] + 1;
	Q.push(i);
      }
    }

    color[u] = BLACK;
    
  }
}

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

  d.resize(n+5);
  color.resize(n+5);

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

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

  dfs(0);

  for(int i = 0; i < n; i++){
    cout << i+1 << " " << ((d[i] == INF) ? (-1):d[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(void){
    int u,k,v;

    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]=1;
	} 
    }

    bfs(0);

    return 0;
}

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

int main(){
  int n;
  vector<int> mat[100];
  cin >> 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;
      mat[i].push_back(v);
    }
  }

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

  queue<int> nb;
  nb.push(0);
  d[0]=0;

  while(!nb.empty()){
    for(vector<int>::iterator it=mat[nb.front()].begin();it!=mat[nb.front()].end;it++){
      if(d[*it]==-1){
        d[*it]=d[nb.front()]+1;
        nb.push(*it);
      }
    }
    nb.pop();
  }

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