// Problem idea by Albert Lai // Problem written by Albert Lai // This solution written by Albert Lai import java.io.*; import java.util.*; public class Dance { static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static int getint() { try { in.nextToken(); return (int)in.nval; } catch (Exception e) { throw new InternalError(); } } static int[] giovanni(List[] adjlist) { Set q = new HashSet(); q.add(new Integer(0)); int P = adjlist.length; int[] d = new int[P]; d[0] = 0; for (int i = 1; i < P; ++i) { d[i] = -1; // -1 stands for oo } int n = 0; // n == d[anyone in q] while (!q.isEmpty()) { Set q_ = new HashSet(); for (Iterator qi = q.iterator(); qi.hasNext(); ) { int v = ((Integer) qi.next()).intValue(); for (Iterator vi = adjlist[v].iterator(); vi.hasNext(); ) { Integer w = (Integer) vi.next(); int w_ = w.intValue(); if (d[w_] == -1) { q_.add(w); d[w_] = n+1; } } } q = q_; n++; } return d; } public static void main(String[] args) { int P = getint(); int D = getint(); List[] adjlist = new List[P]; for (int i = 0; i < P; ++i) { adjlist[i] = new LinkedList(); } for (int i = 0; i < D; ++i) { int u = getint(); int v = getint(); adjlist[u].add(new Integer(v)); adjlist[v].add(new Integer(u)); } int[] d = giovanni(adjlist); for (int i = 1; i < P; ++i) { System.out.println(d[i]); } } }