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.

Examples

Input

3 3
1 2 1
2 3 1
3 1 2


Output

1


Input

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


Output

2


Input

2 0


Output

-1

import java.io.*;
import java.math.*;
import java.util.*;

import static java.util.Arrays.*;

public class Main {
	private static final int mod = (int)1e9+7;

	final Random random = new Random(0);
	final IOFast io = new IOFast();

	/// MAIN CODE
	public void run() throws IOException {
//		int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim());
		int TEST_CASE = 1;
		while(TEST_CASE-- != 0) {
			int n = io.nextInt();
			int m = io.nextInt();
			TreeMap<Integer, Integer> reindex = new TreeMap<>();
			SimpleAdjListGraph same = new SimpleAdjListGraph(m, 2 * m);
			for(int i = 0; i < m; i++) {
				int s = io.nextInt() - 1;
				int t = io.nextInt() - 1;
				int c = io.nextInt() - 1;
				if(!reindex.containsKey(c)) reindex.put(c, reindex.size());
				c = reindex.get(c);
				same.addEdge(c, s);
				same.addEdge(c, t);
			}
			
			UnionFindRich uf = new UnionFindRich(n);
			WeightedAdjListGraph g = new WeightedAdjListGraph(n + m + reindex.size(), 4 * m);
			for(int i = 0, j = n; i < m; i++) {
				uf.clear();
				for(int e = same.head[i]; e != -1; e = same.next[e]) {
					int t1 = same.to[e];
					int t2 = same.to[e = same.next[e]];
					uf.union(t1, t2);
				}
				TreeSet<Integer> vis = new TreeSet<>();
				for(int e = same.head[i]; e != -1; e = same.next[e]) {
					int root = uf.root(same.to[e]);
					if(vis.add(root)) {
						for(int v = root; v != -1; v = uf.next[v]) {
							g.addEdge(j, v, 1);
							g.addEdge(v, j, 0);
						}
						j++;
					}
				}
			}
			
			io.out.println(g.dijkstra(0)[n-1]);
		}
	}

	static
	class UnionFindRich {
		private int[] data;
		private int[] next;
		private int[] last;
		
		private int gid;
		private int[] id;
		
		public UnionFindRich(int size) {
			data = new int[size];
			next = new int[size];
			last = new int[size];
			id = new int[size];
			clear();
		}
		
		public void clear() {
			gid++;
			/*
			Arrays.fill(data, -1);
			Arrays.fill(next, -1);
			Arrays.fill(cost, 0);
			for(int i = 0; i < last.length; i++) {
				last[i] = i;
			}
			*/
		}

		private void access(int x) {
			if(gid != id[x]) {
				id[x] = gid;
				data[x] = -1;
				next[x] = -1;
				last[x] = x;
			}
		}
		public int root(int x) { access(x); return data[x] < 0 ? x : (data[x] = root(data[x])); }
		
		public void union(int x, int y) {
			access(x);
			access(y);
			if((x = root(x)) != (y = root(y))) {
				if(data[y] < data[x]) { final int t = x; x = y; y = t; }
				next[last[x]] = y;
				last[x] = last[y];
				data[x] += data[y];
				data[y] = x;
			}
		}
		
		public boolean same(int x, int y) { return root(x) == root(y); }
		public int size(int x) { access(x); return -data[root(x)]; }
	}
	
	static
	class SimpleAdjListGraph {
		int m, V, E;
		int[] head, next, to;
	
		public SimpleAdjListGraph(int V, int E) {
			head = new int[V];
			next = new int[E];
			to = new int[E];
			this.V = V;
			this.E = E;
			clear();
		}
	
		public void clear() {
			m = 0;
			Arrays.fill(head, -1);
		}
	
		public void addEdge(int s, int t) {
			next[m] = head[s];
			head[s] = m;
			to[m++] = t;
		}
	}
	static
	class WeightedAdjListGraph {
		int m, V, E;
		int[] head, next, cost, to;
	
		public WeightedAdjListGraph(int V, int E) {
			head = new int[V];
			next = new int[E];
			cost = new int[E];
			to = new int[E];
			this.V = V;
			this.E = E;
			clear();
		}
	
		public void clear() {
			m = 0;
			Arrays.fill(head, -1);
		}
	
		public void addEdge(int s, int t, int c) {
			next[m] = head[s];
			cost[m] = c;
			head[s] = m;
			to[m++] = t;
		}
		
		public int[] dijkstra(final int s) {
			final int[] dist = new int[head.length];
			final PriorityQueue<int[]> q = new PriorityQueue<int[]>(10, new Comparator<int[]>() {
				@Override
				public int compare(int[] o1, int[] o2) {
					return o1[0] - o2[0];
				}
			});

			Arrays.fill(dist, Integer.MAX_VALUE);
			dist[s] = 0;
			q.add(new int[] { 0, s, });
			while(!q.isEmpty()) {
				final int[] xs = q.poll();
				final int v = xs[1];
				final int d = xs[0];
				if(dist[v] < d) continue;
				
				for(int e = head[v]; e != -1; e = next[e]) {
					final int t = to[e];
					final int w = d + cost[e];
					if(w < dist[t]) {
						dist[t] = w;
						q.add(new int[] { w, t, });
					}
				}
			}
			for(int i = 0; i < dist.length; i++) {
				if(dist[i] == Integer.MAX_VALUE) {
					dist[i] = -1;
				}
			}
			return dist;
		}
		
	}

	/// TEMPLATE
	static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); }
	static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); }
	
	static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; }
	static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; }

	void printArrayLn(int[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	void printArrayLn(long[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	
	static void dump(Object... o) { System.err.println(Arrays.deepToString(o)); } 
	
	void main() throws IOException {
		//		IOFast.setFileIO("rle-size.in", "rle-size.out");
		try { run(); }
		catch (EndOfFileRuntimeException e) { }
		io.out.flush();
	}
	public static void main(String[] args) throws IOException { new Main().main(); }
	
	static class EndOfFileRuntimeException extends RuntimeException {
		private static final long serialVersionUID = -8565341110209207657L; }

	static
	public class IOFast {
		private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		private PrintWriter out = new PrintWriter(System.out);

		void setFileIn(String ins) throws IOException { in.close(); in = new BufferedReader(new FileReader(ins)); }
		void setFileOut(String outs) throws IOException { out.flush(); out.close(); out = new PrintWriter(new FileWriter(outs)); }
		void setFileIO(String ins, String outs) throws IOException { setFileIn(ins); setFileOut(outs); }

		private static int pos, readLen;
		private static final char[] buffer = new char[1024 * 8];
		private static char[] str = new char[500*8*2];
		private static boolean[] isDigit = new boolean[256];
		private static boolean[] isSpace = new boolean[256];
		private static boolean[] isLineSep = new boolean[256];

		static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; }
		public int read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw new EndOfFileRuntimeException(); } } return buffer[pos++]; }
		public int nextInt() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public long nextLong() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } }
		int reads(int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } if(str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } cs[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isLineSep); try { if(str[len-1] == '\r') { len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); }
		public String nextString() throws IOException { return new String(next()); }
		public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); return Arrays.copyOf(str, len); }
//		public int next(char[] cs) throws IOException { int len = 0; cs[len++] = nextChar(); len = reads(cs, len, isSpace); return len; }
		public double nextDouble() throws IOException { return Double.parseDouble(nextString()); }
		public long[] nextLongArray(final int n) throws IOException { final long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextLong(); } return res; }
		public int[] nextIntArray(final int n) throws IOException { final int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextInt(); } return res; }
		public int[][] nextIntArray2D(final int n, final int k) throws IOException { final int[][] res = new int[n][]; for(int i = 0; i < n; i++) { res[i] = nextIntArray(k); } return res; }
		public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException { final int[][] res = new int[n][k+1]; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { res[i][j] = nextInt(); } res[i][k] = i; } return res; }
		public double[] nextDoubleArray(final int n) throws IOException { final double[] res = new double[n]; for(int i = 0; i < n; i++) { res[i] = nextDouble(); } return res; }
	}
}
