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;
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<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++: 
/*
Problem

Category
BFS

URL:
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=ja

考察:


Date: 2020/04/14
*/

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long

/* ==== declarations ====== */
// functions
void solve();
void getInput();

// global variables
int n, k;
vector<vector<int>> AL;
vector<int> D;


/* ======================== */

signed main(){
    getInput();
    solve();
    return 0;
}

void getInput() {
    cin>>n;
    D = vector<int>(n, 1e9);
    for(int i=0; i<n; ++i) {
        int g, k;
        cin>>g>>k;
        vector<int> al;
        for(int j=0; j<k; ++j) {
            int a;
            cin>>a;
            al.push_back(a-1);
        }
        AL.push_back(al);
    }
    
}


void bfs(int gid, int d) {
    if(d >= D[gid]) return;
    D[gid] = d;
    for(int child: AL[gid]) {
        bfs(child, d + 1);
    }
}

void solve() {
    // print(AL);
    bfs(0, 0);
    for(int i=0; i<n; ++i) {
        if (D[i] == 1e9) D[i] = -1;
        cout<<i+1<<" "<<D[i]<<endl;
    }
}

C++: #include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#define ll long long
#define ALL(x) (x).begin(),(x).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define pint_int 2000000003
using namespace std;

class BFS {
public:
    vector<vector<int>> Graph_as_list;

    void emplace_back(vector<int> list) {
        Graph_as_list.push_back(list);
    }
    vector<int> Search(int u) {// search from node u
        vector<int> dist_from_u(Graph_as_list.size(), pint_int);
        queue<int> que;

        que.push(u);
        dist_from_u[u] = 0;
        while (!que.empty()) {
            int u = que.front(); que.pop();
            for (int next_node : Graph_as_list[u]) {
                if (dist_from_u[u] + 1 < dist_from_u[next_node]) {
                    dist_from_u[next_node] = dist_from_u[u] + 1;
                    que.push(next_node);
                }
            }
        }
        for (int& elem : dist_from_u) {
            if (elem == pint_int) elem = -1;
        }
        return dist_from_u;
    }
};

int main() {
    int n; cin >> n;
    BFS bfs;
    for (int i = 0; i < n; i++) {
        int u, k; cin >> u >> k; u--;
        vi temp;
        for (int j = 0; j < k; j++) {
            int v; cin >> v; v--;
            temp.push_back(v);
        }
        bfs.emplace_back(temp);
    }
    vi ans = bfs.Search(0);
    for (int i = 0; i < ans.size(); i++) {
        cout << i + 1 << " " << ans[i] << endl;
    }

}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1LL << 60;
const int MAX = 1000000010;
const ll MOD = 1000000007;
const double PI = acos(-1);

int main() {
    int N; cin >> N;
    vector<vector<int>> G(N);
    for (int i = 0; i < N; i++) {
        int u, k; cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            int v; cin >> v;
            v--;
            G[u].push_back(v);
        }
    }

    queue<int> que;
    vector<int> dist(N, -1);
    dist[0] = 0;
    que.push(0);

    while (!que.empty()) {
        int u = que.front(); que.pop();
        
        for (auto nv : G[u]) {
            if (dist[nv] != -1) continue;

            dist[nv] = dist[u] + 1;
            que.push(nv);
        }
    }

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

    return 0;
}