Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

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

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

#define debug(x) cout <<"DEBUG"<<" "<< #x << ":" << x << '\n'

// n回繰り返し
#define rep(i,n) for(int i = 0; i < ((int)(n)); i++)   // 0-indexed昇順
#define rep1(i,n) for(int i = 1; i <= ((int)(n)); i++) // 1-indexed昇順
#define rrep(i,n) for(int i = ((int)(n)-1); i >= 0; i--)  // 0-indexed降順
#define rrep1(i,n) for(int i = ((int)(n)); i >= 1; i--)   // 1-indexed降順
#define all(x) (x).begin(),(x).end()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii;  // 2次元配列
typedef vector<ll> vll;
typedef vector<double> vd;
typedef pair<int, int> PII;

template<typename T>
using vec = vector<T>;
template<class T>
using maxheap = std::priority_queue<T>;
template<class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template<class T,class U> inline bool chmax(T &a, const U &b){if(a<b){a=b;return 1;}return 0;}
template<class T,class U> inline bool chmin(T &a, const U &b){if(a>b){a=b;return 1;}return 0;}

const ld Pi = std::acos(-1.0L);
const ll infll = (1LL<<62)-1;
const int inf = (1<<30)-1;
const int mod = 1000000007;
// const int mod = 998244353;


/*FUNCs=================================================*/


/*MAIN==================================================*/
signed main(){
    int _StartTime_ = clock();
    cin.tie(nullptr);
    ios::sync_with_stdio(false);   //cin cout 高速化
    // cout << fixed << setprecision(15);

    int n; cin >> n;
    vii g(n);
    rep(i,n) {
        int u,v,k; cin >> u >> k;
        --u;
        rep(j,k) {
            cin >> v;
            --v;
            g[u].push_back(v);
        }
    }

    vi dist(n, -1);
    queue<int> que;
    que.emplace(0);
    dist[0] = 0;

    while(que.size()) {
        int u = que.front(); que.pop();
        for(auto v : g[u]) {
            if(dist[v] != -1) continue;
            dist[v] = dist[u] + 1;
            que.emplace(v);
        }
    }

    for(int i = 0; i < n; ++i) {
        cout << i+1 << ' ' << dist[i] << '\n';
    }

    // printf("ExcutionTime: %d /ms\n",1000*(int)((clock()-_StartTime_)/CLOCKS_PER_SEC));
}

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

int main()
{
  int n;
  scanf("%d", &n);

  std::vector<std::vector<int>> graph;
  graph.resize(n);
  for (int i = 0; i < n; ++i)
  {
    int u, k, v;
    scanf("%d%d", &u, &k);
    for (int j = 0; j < k; ++j)
    {
      scanf("%d", &v);
      graph[u - 1].push_back(v - 1);
    }
  }

  std::queue<int> vertex;
  vertex.push(0);
  int current_distance = 0;

  int distances[n];
  for (int i = 0; i < n; ++i)
    distances[i] = -1;

  while (vertex.size() > 0)
  {
    int s = vertex.size();

    // Loop for current depth
    for (int i = 0; i < s; ++i)
    {
      int id = vertex.front();
      vertex.pop();

      distances[id] = current_distance;

      std::vector<int>::const_iterator itr;
      for (itr = graph[id].begin(); itr != graph[id].end(); ++itr)
      {
        if (distances[*itr] == -1)
          vertex.push(*itr);
      }
    }

    ++current_distance;
  }

  for (int i = 0; i < n; ++i)
    printf("%d %d\n", i + 1, distances[i]);

  return 0;
}

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

int main()
{
  int n;
  scanf("%d", &n);

  std::vector<std::vector<int>> graph;
  graph.resize(n);
  for (int i = 0; i < n; ++i)
  {
    int u, k, v;
    scanf("%d%d", &u, &k);
    for (int j = 0; j < k; ++j)
    {
      scanf("%d", &v);
      graph[u - 1].push_back(v - 1);
    }
  }

  std::queue<int> vertex;
  vertex.push(0);
  int current_distance = 0;

  int distances[100];
  for (int i = 0; i < 100; ++i)
    distances[i] = -1;

  while (vertex.size() > 0)
  {
    int s = vertex.size();

    // Loop for current depth
    for (int i = 0; i < s; ++i)
    {
      int id = vertex.front();
      vertex.pop();

      if (distances[id] == -1)
        distances[id] = current_distance;

      std::vector<int>::const_iterator itr;
      for (itr = graph[id].begin(); itr != graph[id].end(); ++itr)
      {
        if (distances[*itr] == -1)
          vertex.push(*itr);
      }
    }

    ++current_distance;
  }

  for (int i = 0; i < n; ++i)
    printf("%d %d\n", i + 1, distances[i]);

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

struct Node {
	int k, d, f;
	vector<int> v;
};

#define MAX_N 100

Node node[MAX_N + 1];

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		int u;
		cin >> u;
		cin >> node[u].k;
		for (int j = 0; j < node[u].k; ++j) {
			int v;
			cin >> v;
			node[u].v.push_back(v);
		}
		node[u].d = -1;
	}
	queue<int> q;
	node[1].d = 0;
	q.push(1);
	while(!q.empty()) {
		int u = q.front(); q.pop();
		for (int i = 0; i < node[u].k; ++i) {
			int v = node[u].v[i];
			if (node[v].d == -1) {
				node[v].d = node[u].d + 1;
				q.push(v);
			}
		}
	}
	for (int i = 1; i <= n; ++i)
		cout << i << " " << node[i].d << endl;
	return 0;
}

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

const int nIMPOS = -1;
struct StDist
{
  int m_nDist;
  int m_nMeTo;

  StDist(int nDist) : m_nDist(nDist), m_nMeTo(0)
  { }
};

void fnInput(vector<vector<int>>& rvvnAdjcy, vector<StDist>& rvoDist)
{
  int nxSiz;
  cin >> nxSiz;

  rvvnAdjcy.resize(nxSiz + 1, vector<int>(nxSiz + 1, 0));
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  StDist oDist(nIMPOS);
  rvoDist.resize(nxSiz + 1, oDist);
}

void fnBreadthFirstSearch(const vector<vector<int>>& cnrvvnAdjcy, 
                          queue<int>& rqnBFSrch, vector<StDist>& rvoDist)
{
  while (rqnBFSrch.size())
  {
    int nx = rqnBFSrch.front(); rqnBFSrch.pop();
    if (rvoDist[nx].m_nMeTo)  continue; 

    rvoDist[nx].m_nMeTo = 1;
    for (int ny = 1; ny < cnrvvnAdjcy[nx].size(); ++ny)
      if (cnrvvnAdjcy[nx][ny])
      {
        int nDist = rvoDist[nx].m_nDist + 1;
        if (rvoDist[ny].m_nDist == nIMPOS)
          rvoDist[ny].m_nDist = nDist;

        rqnBFSrch.push(ny);
      }
  }
}

void fnResult(const vector<StDist>& cnrvoDist)
{
  for (int i = 1; i < cnrvoDist.size(); ++i)
    cout << i <<  " " << cnrvoDist[i].m_nDist << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  queue<int>  qnBFSrch;
  vector<StDist> voDist;

  fnInput(vvnAdjcy, voDist);

  voDist[1].m_nDist = 0;
  qnBFSrch.push(1);
  fnBreadthFirstSearch(vvnAdjcy, qnBFSrch, voDist);
  fnResult(voDist);

  return 0;
}

C++: #include <stdio.h>

int main (){

}