Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

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

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

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

const long N = 100;
const long INFTY = (1<<21);

long n, M[N + 1][N + 1];
long d[N + 1];

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

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

	bfs(1);

	return 0;
}

C++: #include "bits/stdc++.h"
 
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef priority_queue<int> PQ_DESC;
typedef priority_queue<int, vector<int>, greater<int>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<VVLL> VVVLL;

#define SORT_ASC(c) sort((c).begin(), (c).end())
#define SORT_DESC(c) sort((c).begin(), (c).end(), greater<typeof((c).begin())>())
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define FORL(i,a,b) for(LL i=(a);i<(b);++i)
#define REPL(i,n)  FORL(i,0,n)
#define ALL(a) (a).begin(),(a).end()
 
const double EPS = 1e-10;
const double PI  = acos(-1.0);

 //debug func
template<typename T>
void vprint(vector<T> v) {
    for(auto x : v) {
        cerr << x << " ";
    }
    cerr << endl;
}
 
template<typename T>
void vvprint(vector<vector<T>> vv) {
    REP(i, vv.size()) {
        REP(j, vv[i].size()) {
            cerr << vv[i][j] << " ";
        }
        cerr << endl;
    }
}

template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k, const Iterator last)
{
   /* Credits: Thomas Draper */
   if ((first == last) || (first == k) || (last == k))
      return false;
   Iterator itr1 = first;
   Iterator itr2 = last;
   ++itr1;
   if (last == itr1)
      return false;
   itr1 = last;
   --itr1;
   itr1 = k;
   --itr2;
   while (first != itr1)
   {
      if (*--itr1 < *itr2)
      {
         Iterator j = k;
         while (!(*itr1 < *j)) ++j;
         iter_swap(itr1,j);
         ++itr1;
         ++j;
         itr2 = k;
         rotate(itr1,j,last);
         while (last != j)
         {
            ++j;
            ++itr2;
         }
         rotate(k,itr2,last);
         return true;
      }
   }
   rotate(first,k,last);
   return false;
}

inline double get_time_sec(void){
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count())/1000000000;
}

template<typename T>
T lcm(T a, T b) {
    return (a / __gcd(a, b)) * b;
}

template<typename T>
map<T, T> prime_list(T n) {
    map<T, T> ret;
    for(T i = 2; i*i <= n; i++) {
        if(n % i == 0) {
            ret[i] = 0;
            while(n % i == 0) {
                n /= i;
                ret[i]++;
            }
        }
    }
    if(n != 1) ret[n]++;
    return ret;
}

#define MOD 1000000007

LL mypow(LL a, LL n) {
    if(n == 0) return 1;
    if(n == 1) return a % MOD;
    if(n % 2 == 1) return (a * mypow(a, n-1)) % MOD;
    LL t = mypow(a, n/2);
    return (t * t) % MOD; 
}

LL mycomb(LL n, LL k) {
    if(n == 0 || n == k) return 1;
    LL x = 1, y = 1;
    REP(i, k) {
        x = x * (n-i) % MOD;
        y = y * (i+1) % MOD;
    }
    return x * mypow(y, MOD - 2) % MOD;
}

#define INF 1e8

int n;
VVI g;

int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> n;
    g = VVI(n, VI(n, 0));
    REP(i, n) {
        int u, k;
        cin >> u >> k;
        u--;
        REP(j, k) {
            int v;
            cin >> v;
            v--;
            g[u][v]++;
        }
    }
    VI d(n, -1);
    d[0] = 0;
    queue<int> q;
    q.push(0);
    while(!q.empty()) {
        int f = q.front();
        q.pop();
        REP(i, n) {
            if(g[f][i] && d[i] == -1) {
                d[i] = d[f] + 1;
                q.push(i);
            }
        }
    }
    // vprint(d);
    REP(i, n) {
        cout << i+1 << " " << d[i] << endl;
    }
}

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

class Vertex {
public:
    int num;
    bool flag;                  // 以前訪れたかどうか
    vector<int> adj;            // 隣接リスト
};

int dist[101];
void bfs(map<int, Vertex>& graph, Vertex s) {
    queue<Vertex> que;

    dist[s.num] = 0;
    que.push(s);
    while (!que.empty()) {
        Vertex u = que.front(); que.pop();
        for (auto i = 0; i < u.adj.size(); i++) {
            Vertex v = graph[u.adj[i]];
            if (!v.flag) {
                graph[u.adj[i]].flag = true;
                dist[v.num] = dist[u.num]+1;
                que.push(v);
            }
        }
    }
}

int main(void) {
    int n;
    cin >> n;
    map<int, Vertex> graph; // 頂点の番号->頂点
    for (int i = 0; i < n; i++) {
        Vertex v;
        int num, k;
        cin >> num >> k;
        v.flag = false;
        v.num = num;
        for (int j = 0; j < k; j++) {
            int tmp;
            cin >> tmp;
            v.adj.push_back(tmp);
        }
        graph[num] = v;
    }

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

    return 0;
}