Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

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

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)
	paths[1] = 1

	for len(queue) > 0 {
		u := queue.pop()
		d := paths[u] + 1
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <bits/stdc++.h>
using namespace std;

#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
//constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

struct Edge {
    ll to, rev;
    long double cap;
    Edge(ll _to, long double _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag,
              long double revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve() {
    ll n;
    cin >> n;
    vvec<int> g(n);
    REP(i, n) {
        int u, k;
        cin >> u >> k;
        u--;
        REP(i, k) {
            int v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    vec<ll> d(n, INF);
    queue<ll> que;
    que.push(0);
    d[0] = 0;
    while(que.size()) {
        int v = que.front();
        que.pop();
        for(auto to : g[v]) {
            if(d[to] != INF)
                continue;
            d[to] = d[v] + 1;
            que.push(to);
        }
    }

    REP(i, n) {
        if(d[i] == INF)
            cout << i + 1 << " " << -1 << en;
        else
            cout << i + 1 << " " << d[i] << en;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    solve();

    /*
    ll t;
    cin >> t;
    REP(i, t) { solve();}
    */

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, to) for (int i = 0; i < (to); i++)
#define rrep(i, to) for (int i = (to) - 1; i >= 0; i--)
#define repf(i, from, to) for (int i = (from); i < (to); i++)
#define all(v) v.begin(), v.end()
#define unless(cond) if (!(cond))
using namespace std;
using ll = long long;
template <typename T>
using V = vector<T>;
template <typename T, typename U>
using P = pair<T, U>;

V<int> dist;
V<V<int>> graph;

int main() {
  int n;
  cin >> n;

  graph.resize(n);
  dist.resize(n, -1);

  rep(i, n) {
    int u, k;
    cin >> u >> k;
    u--;

    rep(j, k) {
      int adj;
      cin >> adj;
      adj--;
      graph[u].push_back(adj);
    }

    dist[i] = -1;
  }


  queue<int> will;
  dist[0] = 0;
  will.push(0);

  while(!will.empty()) {
    int current_i = will.front();
    will.pop();

    for(auto next_i : graph[current_i]) {
      if(dist[next_i] != -1) {
        continue;
      }
      dist[next_i] = dist[current_i] + 1;
      will.push(next_i);
    }
  }


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

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

int main()
{
    int n;
    cin >> n;
    int Adj[n][n] = {};
    int d[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;
            Adj[u - 1][v - 1] = 1;
        }
        d[i] = -1;
    }

    int src = 0;
    queue<int> q;
    d[src] = 0;
    q.push(src);
    while (!q.empty())
    {
        int cur = q.front();
        q.pop();
        for (int dst = 0; dst < n; dst++)
        {
            if (Adj[cur][dst] && d[dst] == -1)
            {
                d[dst] = d[cur] + 1;
                q.push(dst);
            }
        }
    }

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

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>
#include <iomanip>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL<<60;
const double PI=3.14159265358979323846;
const int NMAX=100005;
using namespace std;

vector<int> to[101];
vector<int> d(101,-1);

int main(){
    int n;
    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;
            to[u].push_back(v);
        }
    }

    queue<pair<int, int> > q;
    q.push(make_pair(1,0));

    while(q.size() > 0){
        pair<int, int> p = q.front();
        q.pop();
        int i = p.first;
        int t = p.second;

        if(d[i] != -1) continue;
        d[i] = t;
        for(int j = 0; j < to[i].size(); j++){
            q.push(make_pair(to[i][j], t+1));
        }
    }

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

    return 0;
}

C++: #include <iostream>
#include<bits/stdc++.h>
using namespace std;
int n,m,k,a,b,ch[100];
vector<int>v[100];
queue<int>que;
int main(void){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>m>>k;
        for(int i=0;i<k;i++){
            cin>>a;
            v[m-1].push_back(a-1);
        }
    }
    que.push(0);
    ch[0]=1;
    while(que.size()){
        int q=que.front();
        que.pop();
        for(auto x:v[q]){
            if(ch[x]==0){
                ch[x]=ch[q]+1;
                que.push(x);
            }
        }
    }
    for(int i=0;i<n;i++){
        cout<<i+1<<" "<<ch[i]-1<<endl;
    }
}

C++: #include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAXN = (int)1e2 + 10;

int n;
vector<int> g[MAXN];
int vis[MAXN];
int dep[MAXN];

void BFS(){
    int d=0;
    queue<int> q;
    for(int i=1;i<=n;i++) dep[i]=-1;
    dep[1]=0;
    q.push(1);
    vis[1]=1;
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int i=0;i<g[u].size();i++){
            int v=g[u][i];
            if(vis[v]) continue;
            dep[v]=dep[u]+1;
            vis[v]=1;
            q.push(v);
        }
    }
}

void work(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int u,k; scanf("%d%d",&u,&k);
        while(k--){
            int v; scanf("%d",&v);
            g[u].push_back(v);
        }
        sort(g[u].begin(),g[u].end());
    }
    BFS();
    for(int i=1;i<=n;i++) printf("%d %d\n",i,dep[i]);
}

int main(){
    work();
    return 0;
}