#include <bits/stdc++.h>
using namespace std;
int head[500010], nex[500010], ver[500010], edge[500010];
int tot = 0;
void add(int x, int y, int z) {
  ver[++tot] = y;
  edge[tot] = z;
  nex[tot] = head[x];
  head[x] = tot;
}
queue<int> q;
int n, m, dis[500100], vis[500100];
void spfa(int x) {
  memset(dis, 0x7ffffff, sizeof(dis));
  dis[x] = 0;
  q.push(x);
  vis[x] = 1;
  while (!q.empty()) {
    int y = q.front();
    q.pop();
    vis[y] = 0;
    for (int i = head[y]; i; i = nex[i]) {
      int s = ver[i], z = edge[i];
      if (dis[s] > abs(dis[y] + z)) {
        dis[s] = abs(dis[y] + z);
        if (!vis[s]) {
          q.push(s);
          vis[s] = 1;
        }
      }
    }
  }
}
int main() {
  int k;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    add(x, y, z);
  }
  spfa(1);
  cout << dis[n] << endl;
  cout << endl;
  return 0;
}
