Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

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

func (q *Queue) pop() Pair {
	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([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

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

	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 <iostream>
#include <queue>
#define MAX 101
#define NIL -1

using namespace std;


int matrix[MAX][MAX] = {0};
int dis[MAX];
//提交时须对time改名 
int n;


void bfs_visit(int u);
int next(int u);

int main()
{
	cin >> n;
	
	for (int i = 0; i < n; i++)
	{
		int u, k, v;
		cin >> u >> k;
		for (int j = 0; j < k; j++)
		{
			cin >> v;
			matrix[u][v] = 1;			
		}
	}
	
//显示邻接矩阵 
//	for (int i = 0; i < n; i++)
//	{
//		for (int j = 0; j < n; j++)
//		{
//			if(j) cout << ' ';
//			cout << matrix[i+1][j+1];		
//		}
//		cout << endl;
//	}

	bfs_visit(1);

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



void bfs_visit(int u)
{
	
	//dis[]初始化，取值NIL时表示未访问 
	for (int i = 1; i <= n; i++)
		dis[i] = NIL;	
	dis[u] = 0;	
	
	queue<int> Q;
	Q.push(u);
	
//	int d = 1;
//	while (!Q.empty())
//	{
//		u = next(Q.front());
//
//		if (u)
//		{
//			dis[u] = d; 
//			Q.push(u);
//		}
//		else
//		{
//			Q.pop();			
//		}
//		
//		//判断是否进入下一层 
//		if (dis[Q.front()] == d) d++;
//
//	}

//另一种实现 
	while (!Q.empty())
	{
		u = Q.front();
		Q.pop();
		for (int v = 1; v <= n; v++)
		{
			if (matrix[u][v] != 0 && dis[v] == NIL)
			{
				dis[v] = dis[u] + 1;
				Q.push(v);
			}
		}
	} 
}


int next(int u)
{
	for (int i = 1; i <= n; i++)
	{
		if (matrix[u][i] != 0 && dis[i] == NIL)
			return i;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N=100,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,v,k,i,j;
  cin>>n;
  for (i=0; i<n; i++) {
    for (j=0; j<n; j++) {
      M[i][j]=0;
    }
  }
  for (i=0; i<n; i++) {
    cin>>u>>k;
    u--;
    for (j=0; j<k; j++) {
      cin>>v;
      v--;
      M[u][v]=1;
    }
  }
  bfs(0);
  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <numeric>
#include <queue>
#include <stack>
#include <map> 
#include <set>
#include <string>
#include <functional>
#include <list>
#include <random>
#include <time.h>
#include <iomanip>
#include <assert.h>
#include <numeric>
#include <new>
#include <sstream>
#include <complex>
#define BIT(nr) (1ULL << (nr))
#define int long long
#define ll long long
#define double long double
#define mod 1000000007
#define MAXN (int)1e+5 * 2+1
#define LL_MAX 9223372036854775807	
#define LL_HALFMAX 9223372036854775807 / 2	
#define MIN -(9223372036854775807 / 2)
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define mp make_pair
template<typename T1, typename T2> inline void chmin(T1 & a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }

using namespace std;

using Weight = int;
using Flow = int;
struct Edge {
	int src, dst;

	// libalgo のものに追加、メンバを追加するだけなので互換性は崩さないはず、逆辺のG[e.dstの]インデックスを保持
	int rev;
	Weight weight;
	Flow cap;
	Edge() : src(0), dst(0), weight(0) {}
	Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
	bool operator<(const Edge& right) const {
		return dst < right.dst;
	}
};

using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph& g, int a, int b, Weight w = 1) {
	g[a].emplace_back(a, b, w);
	g[b].emplace_back(b, a, w);
}
void add_arc(Graph& g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); }

int dx[4] = { 0, 1, 0, -1 }; // x軸方向への変位
int dy[4] = { 1, 0, -1, 0 }; // y軸方向への変位

signed main() {
	int n;
	cin >> n;
	vector<int> dist(n + 1, -1);

	Graph G(n + 1);
	rep(i, n) {

		int u, k;
		cin >> u >> k;
		rep(j, k) {
			int v;
			cin >> v;
			add_arc(G, u, v);
		}
	}

	queue<int> q;
	q.push(1);
	dist[1] = 0;
	while (!q.empty())
	{
		int node = q.front();
		q.pop();
		for (Edge e : G[node]) {
			if (dist[e.dst] == -1) {
				dist[e.dst] = dist[e.src] + 1;
				q.push(e.dst);
			}
		}
	}

	REPS(i, n) {
		cout << i << " " << dist[i] << "\n";
	}

	return 0;
}

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

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

	std::vector<std::vector<int>> edges(n);
	for (auto&& v : edges) {
		int k;
		std::cin >> k >> k;

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

			v.push_back(u);
		}
	}

	std::vector<int> depth(n, -1);
	depth[0] = 0;

	std::queue<int> que;
	que.push(0);
	while (!que.empty()) {
		int cur = que.front();
		que.pop();

		for (auto&& dist : edges[cur]) {
			if (depth[dist] == -1) {
				depth[dist] = depth[cur] + 1;
				que.push(dist);
			}
		}
	}

	for (int i = 0; i < n; ++i)
		std::cout << i + 1 << " " << depth[i] << std::endl;
}

C++: #include <iostream>
#include <stack>
#include <queue>

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    stack<int> *q = new stack<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(!q->empty())
    {
        int u = q->top();
        q->pop();

        for (int v = 0; v < N; v++)
        {
            if (M[u][v] && C[v] == WHITE)
            {
                C[v] = GRAY;
                D[v] = D[u] + 1;
                q->push(v);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

        for (int j = 0; j < N; j++)
        {
            M[i][j] = 0;
        }
    }

    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--;
            M[u][v] = 1;
        }
    }

/*
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }
*/
    bfs(0);

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

    return 0;
}