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

class Station {
  final int stationIndex;
  final int trainIndex;
  
  Station (int stationIndex, int trainIndex) {
    this.stationIndex = stationIndex;
    this.trainIndex = trainIndex;
  }
  
  public int hashCode() {
    return 1000003 * stationIndex + trainIndex;
  }
  
  public boolean equals(Object object) {
    if (!(object instanceof Station)) {
      return false;
    }
    Station station = (Station) object;
    return this.stationIndex == station.stationIndex && this.trainIndex == station.trainIndex;
  }
}

class Node implements Comparable<Node> {
  Station station;
  int cost;
  
  Node(Station station, int cost) {
    this.station = station;
    this.cost = cost;
  }
  
  public int compareTo(Node node) {
    return this.cost - node.cost;
  }
}

class Solver {
  final int n, m;
  final int[] ps, qs, cs;
  
  public Solver(int n, int m, int[] ps, int[] qs, int[] cs) {
    this.n = n;
    this.m = m;
    this.ps = ps;
    this.qs = qs;
    this.cs = cs;
  }
  
  private void addSameTrainEdge(Map<Station, List<Station>> sameTrainMatrix, Station src, Station dst) {
    if (sameTrainMatrix.get(src) == null) {
      sameTrainMatrix.put(src, new ArrayList<>());
      sameTrainMatrix.get(src).add(new Station(src.stationIndex, -1));
    }
    sameTrainMatrix.get(src).add(dst);
  }
  
  public int solve() {
    Map<Station, List<Station>> sameTrainMatrix = new HashMap<>();
    for (int i = 0; i < m; i++) {
      Station s1 = new Station(ps[i], cs[i]);
      Station s2 = new Station(qs[i], cs[i]);
      addSameTrainEdge(sameTrainMatrix, s1, s2);
      addSameTrainEdge(sameTrainMatrix, s2, s1);
    }
    List<Set<Integer>> stationToTrainSet = new ArrayList<>();
    for (int i = 0; i <= n; i++) {
      stationToTrainSet.add(new HashSet<>());
    }
    for (int i = 0; i < m; i++) {
      stationToTrainSet.get(ps[i]).add(cs[i]);
      stationToTrainSet.get(qs[i]).add(cs[i]);
    }
    
    PriorityQueue<Node> queue = new PriorityQueue<>();
    Set<Station> visited = new HashSet<>();
    queue.add(new Node(new Station(1, -1), 0));
    while (!queue.isEmpty()) {
      Node node = queue.poll();
      if (visited.contains(node.station)) {
        continue;
      }
      
      Station station = node.station;
      visited.add(station);
      if (station.stationIndex == n) {
        return node.cost;
      }
      if (station.trainIndex >= 0) {
        for (Station nextStation : sameTrainMatrix.get(station)) {
          if (visited.contains(nextStation)) {
            continue;
          }
          queue.add(new Node(nextStation, node.cost));
        }
      } else {
        for (int trainIndex : stationToTrainSet.get(station.stationIndex)) {
          Station nextStation = new Station(station.stationIndex, trainIndex);
          if (visited.contains(nextStation)) {
            continue;
          }
          queue.add(new Node(nextStation, node.cost + 1));
        }
      }
    }
    return -1;
  }
}

public class Main {
  private static void execute(ContestReader reader, PrintWriter out) {
    int n = reader.nextInt();
    int m = reader.nextInt();
    int[] ps = new int[m];
    int[] qs = new int[m];
    int[] cs = new int[m];
    for (int i = 0; i < m; i++) {
      ps[i] = reader.nextInt();
      qs[i] = reader.nextInt();
      cs[i] = reader.nextInt();
    }
    out.println(new Solver(n, m, ps, qs, cs).solve());
  }
  
  public static void main(String[] args) {
    ContestReader reader = new ContestReader(System.in);
    PrintWriter out = new PrintWriter(System.out);
    execute(reader, out);
    out.flush();
  }
}

class ContestReader {
  private BufferedReader reader;
  private StringTokenizer tokenizer;
  
  ContestReader(InputStream in) {
    reader = new BufferedReader(new InputStreamReader(in));
  }
  
  public String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }
  
  public int nextInt() {
    return Integer.parseInt(next());
  }
  
  public long nextLong() {
    return Long.parseLong(next());
  }
  
  public double nextDouble() {
    return Double.parseDouble(next());
  }
  
  public String[] nextArray(int n) {
    String[] array = new String[n];
    for (int i = 0; i < n; i++) {
      array[i] = next();
    }
    return array;
  }
  
  public int[] nextIntArray(int n) {
    int[] array = new int[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextInt();
    }
    return array;
  }
  
  public long[] nextLongArray(int n) {
    long[] array = new long[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextLong();
    }
    return array;
  }
  
  public double[] nextDoubleArray(int n) {
    double[] array = new double[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextDouble();
    }
    return array;
  }
  
  public int[][] nextIntMatrix(int n, int m) {
    int[][] matrix = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextInt();
      }
    }
    return matrix;
  }
  
  public long[][] nextLongMatrix(int n, int m) {
    long[][] matrix = new long[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextLong();
      }
    }
    return matrix;
  }
  
  public double[][] nextDoubleMatrix(int n, int m) {
    double[][] matrix = new double[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextDouble();
      }
    }
    return matrix;
  }
}

class ModCalculator {
  private final long mod;
  
  ModCalculator(long mod) {
    this.mod = mod;
  }
  
  public long add(long a, long b) {
    return (a + b) % mod;
  }
  
  public long mul(long a, long b) {
    return (a * b) % mod;
  }
  
  public long pow(long a, long b) {
    if (b == 0) {
      return 1;
    }
    long v = pow(mul(a, a), b / 2);
    if (b % 2 == 1) {
      return mul(v, a);
    } else {
      return v;
    }
  }
  
  public long inverse(long a) {
    return pow(a, mod - 2);
  }
  
  public long div(long a, long b) {
    return mul(a, inverse(b));
  }
}
 
class ModCombinationCache {
  private final ModCalculator modCalculator;
  private final long[] factorialCache;
  private final long[] factorialInverseCache;
  
  public ModCombinationCache(int n, ModCalculator modCalculator) {
    this.modCalculator = modCalculator;
    factorialCache = new long[n];
    factorialCache[0] = 1;
    for (int i = 1; i < n; i++) {
      factorialCache[i] = modCalculator.mul(factorialCache[i-1], i);
    }
    
    factorialInverseCache = new long[n];
    for (int i = 0; i < n; i++) {
      factorialInverseCache[i] = modCalculator.inverse(factorialCache[i]);
    }
  }
  
  public long getFactorial(int n) {
    return factorialCache[n];
  }
  
  public long getCombination(int n, int k) {
    return modCalculator.mul(factorialCache[n], modCalculator.mul(factorialInverseCache[k], factorialInverseCache[n-k]));
  }
}

class Algorithm {
  private static void swap(Object[] list, int a, int b) {
    Object tmp = list[a];
    list[a] = list[b];
    list[b] = tmp;
  }
  
  public static <T extends Comparable<? super T>> boolean nextPermutation(T[] ts) {
    int rightMostAscendingOrderIndex = ts.length - 2;
    while (rightMostAscendingOrderIndex >= 0 &&
        ts[rightMostAscendingOrderIndex].compareTo(ts[rightMostAscendingOrderIndex + 1]) >= 0) {
      rightMostAscendingOrderIndex--;
    }
    if (rightMostAscendingOrderIndex < 0) {
      return false;
    }
    
    int rightMostGreatorIndex = ts.length - 1;
    while (ts[rightMostAscendingOrderIndex].compareTo(ts[rightMostGreatorIndex]) >= 0) {
      rightMostGreatorIndex--;
    }
    
    swap(ts, rightMostAscendingOrderIndex, rightMostGreatorIndex);
    for (int i = 0; i < (ts.length - rightMostAscendingOrderIndex - 1) / 2; i++) {
      swap(ts, rightMostAscendingOrderIndex + 1 + i, ts.length - 1 - i);
    }
    return true;
  }
  
  public static void shuffle(int[] array) {
    Random random = new Random();
    int n = array.length;
    for (int i = 0; i < n; i++) {
      int randomIndex = i + random.nextInt(n - i);
      
      int temp = array[i];
      array[i] = array[randomIndex];
      array[randomIndex] = temp;
    }
  }
  
  public static void shuffle(long[] array) {
    Random random = new Random();
    int n = array.length;
    for (int i = 0; i < n; i++) {
      int randomIndex = i + random.nextInt(n - i);
      
      long temp = array[i];
      array[i] = array[randomIndex];
      array[randomIndex] = temp;
    }
  }
  
  public static void sort(int[] array) {
    shuffle(array);
    Arrays.sort(array);
  }
  
  public static void sort(long[] array) {
    shuffle(array);
    Arrays.sort(array);
  }
}



Measured memory_bytes: 197408