Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 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;
}

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

int G[110][110];
int N,num,lines,tar;
int D[1000];
int ret[1000];

void bfs(int src);

int main() {
	for (int i=0;i<110;i++) {
		for(int j=0;j<110;j++) {
			G[i][j] = 0;
		}
	}
	for(int i=0;i<1000;i++) {
		D[i] = 100;
	}
	cin >> N;
	for(int i=0;i<N;i++) {
		cin >> num >> lines;
		for(int j=0;j<lines;j++) {
			cin >> tar;
			G[num][tar] = 1;
		}
	}
	/*
	for(int i=0;i<num+1;i++) {
		for(int j=0;j<num+1;j++) {
			cout << G[i][j] << " ";
		}
		cout << endl;
	}
	*/
	bfs(1);
	for(int i=1;i<num+1;i++) {
		if((ret[i] == 0) && (i>1)) ret[i] = -1;
		cout << i << " " << ret[i] << endl;
	}

}
void bfs(int src) {
	int cur,dst;
	//cerr << "bfs root = " << src << endl;
	queue<int> Q; // ??´??°????????????????????\????????????
	Q.push(src);
	D[src] = 0; // ?????????
	while (! Q.empty()) {
		int cur = Q.front(); // ??????????´????????????????
		Q.pop();
// ???????¢??????¨??¨???
		ret[cur] = D[cur];
		//cerr << cur << " " << D[cur] << endl;
		for (int dst=1;dst<N+1;dst++) { // ???????????? dst ????????????
			if ((G[cur][dst] == 1) && (D[dst] == 100)) { // cur ?????? dst ??????????????????dst ???????¨??????????
				D[dst] = D[cur]+1; //
				Q.push(dst); // dst ????¨???????????????????
			}
		}
	}
}