Problem:
Score : 600 points

Problem Statement

Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number.

The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i.

You can change trains at a station where multiple lines are available.

The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again.

Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare.

Constraints

2 \leq N \leq 10^5

0 \leq M \leq 2×10^5

1 \leq p_i \leq N (1 \leq i \leq M)

1 \leq q_i \leq N (1 \leq i \leq M)

1 \leq c_i \leq 10^6 (1 \leq i \leq M)

p_i \neq q_i (1 \leq i \leq M)

Input

The input is given from Standard Input in the following format:

N M
p_1 q_1 c_1
:
p_M q_M c_M

Output

Print the minimum required fare. If it is impossible to get to station N by subway, print -1 instead.

Sample Input 1

3 3
1 2 1
2 3 1
3 1 2

Sample Output 1

1

Use company 1's lines: 1 → 2 → 3. The fare is 1 yen.

Sample Input 2

8 11
1 3 1
1 4 2
2 3 1
2 5 1
3 4 3
3 6 3
3 7 3
4 8 4
5 6 1
6 7 5
7 8 5

Sample Output 2

2

First, use company 1's lines: 1 → 3 → 2 → 5 → 6. Then, use company 5's lines: 6 → 7 → 8. The fare is 2 yen.

Sample Input 3

2 0

Sample Output 3

-1

Solution in Scala (2.11.7) (judge verdict: Accepted)

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  class Zipper {
    type A = Long
    var gen = 0
    val id = mutable.HashMap[A, Int]()

    def get(x: A): Int = {
      id.getOrElseUpdate(x, {
        val i = gen
        gen += 1
        i
      })
    }
  }

  def solve(): Unit = {
    val N, M = ni()

    val From, To, C = Array.ofDim[Int](M)
    REP(M) { i =>
      val from, to = ni() - 1
      val c = ni()
      From(i) = from
      To(i) = to
      C(i) = c
    }

    case class Edge(v: Int, w: Int)

    // 会社に属さないノードは 駅id
    // 会社に属するノードは 会社id * 1e6 + 駅id
    val zip = new Zipper

    val cnt = Array.ofDim[Int](N + M * 2)
    val g = Array.ofDim[Array[Edge]](N + M * 2)

    // 方向あるよ
    def joint(from: Int, to: Int, w: Int): Unit = {
      cnt(from) -= 1
      g(from)(cnt(from)) = Edge(to, w)
    }

    REP(M) { i =>
      val from = zip.get(From(i))
      val to = zip.get(To(i))
      val c = C(i)
      val fromC = zip.get(From(i) + c * 1e6.toLong)
      val toC = zip.get(To(i) + c * 1e6.toLong)

      cnt(fromC) += 2
      cnt(toC) += 2
      cnt(from) += 1
      cnt(to) += 1
    }

    REP(N + M * 2) { i =>
      g(i) = new Array(cnt(i))
    }
    REP_r(M) { i =>
      val from = zip.get(From(i))
      val to = zip.get(To(i))
      val c = C(i)
      val fromC = zip.get(From(i) + c * 1e6.toLong)
      val toC = zip.get(To(i) + c * 1e6.toLong)

      joint(fromC, toC, 0)
      joint(toC, fromC, 0)
      joint(from, fromC, 1) // 乗るのは1
      joint(fromC, from, 0) // 降りるのは0
      joint(to, toC, 1)
      joint(toC, to, 0)
    }

//    zip.id.toSeq.sortBy(_._1).foreach { case (k, id) =>
//        debug(s"zip $k -> $id")
//    }
//
//    REP(N + M * 2) { i =>
//      debug(s"$i [${g(i).map(_.v).mkString(",")}]")
//    }

    val INF = Integer.MAX_VALUE / 2
    def dijk(start: Int, dest: Int): Int = {
      import java.util
      val d = Array.fill[Int](N + M * 2)(INF)
      case class Visit(node: Int, cost: Int)
      val queue = new util.ArrayDeque[Visit]()
      d(start) = 0
      queue.add(Visit(start, 0))

      while(!queue.isEmpty) {
        val v = queue.poll()
        if (d(v.node) == v.cost) {
          val es = g(v.node)
          REP(es.length) { i =>
            val e = es(i)
            val next = v.cost + e.w
            if (d(e.v) > next) {
              d(e.v) = next
              if (e.w == 0) queue.addFirst(Visit(e.v, next))
              else queue.addLast(Visit(e.v, next))
            }
          }
        }
      }

//      debug(d)

      d(dest)
    }

    val ans = dijk(zip.get(0), zip.get(N - 1))
    if (ans == INF) out.println(-1)
    else out.println(ans)
  }


  def debug(as: Array[Boolean]): Unit = {
    System.err.println(as.map(x => if(x) "1" else "0").mkString)
  }

  def debug(as: Array[Int]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(as: Array[Long]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(s: String): Unit = {
    System.err.println(s)
  }

  def debug(num: Long): Unit = {
    System.err.println(num)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }


  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}