import scala.collection.immutable.IntMap
import scala.io.StdIn._

object Main extends App
{
	val (n, m) = readInts()
	
	val pathMap: IntMap[Set[Int]] = (1 to m).foldLeft[IntMap[Set[Int]]](IntMap.empty) { case (map, _) =>
		val (from, to) = readInts()
		val fromSet = map.getOrElse(from, Set.empty)
		val toSet = map.getOrElse(to, Set.empty)
		map.updated(from, fromSet + to).updated(to, toSet + from)
	}
	
	val depthMap = calc()
	if(depthMap.size != n){
		println("No")
	}
	else{
		(2 to n).foreach{room =>
			val depth = depthMap(room)
			println(pathMap(room).find(path => depthMap(path) == depth - 1).get)
		}
	}
	
	
	private def calc(fromSet: Set[Int] = Set(1), steps: Int = 1, distMap: Map[Int, Int] = Map(1 -> 0), visited: Set[Int] = Set(1)): Map[Int, Int] =
	{
		val toRooms: Set[Int] = fromSet.flatMap(pathMap(_)).filterNot(visited.contains)
		if (toRooms.isEmpty) distMap
		else {
			val newMap = toRooms.foldLeft(distMap) { case (map, toRoom) => map.updated(toRoom, steps) }
			calc(toRooms, steps + 1, newMap, visited ++ toRooms)
		}
		
	}
	
	
	private def readInts(): (Int, Int) =
	{
		val arr = readLine().split(" ", 2).map(_.toInt)
		(arr(0), arr(1))
	}
}