#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int maxm = 200005;
const int inf = 1e9 + 9;
int u[maxm], v[maxm], w[maxm], fst[maxn], nxt[maxm];
int n, m, s, dis[maxn];
bool book[maxn];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    que;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> s;
  for (register int i = 1; i <= m; ++i) {
    int p, q, r;
    cin >> p >> q >> r;
    if (p == q) continue;
    u[i] = p, v[i] = q, w[i] = r;
    nxt[i] = fst[u[i]];
    fst[u[i]] = i;
  }
  for (register int i = 1; i <= n; ++i) dis[i] = inf;
  dis[s] = 0;
  que.push(make_pair(0, s));
  while (!que.empty()) {
    int x = que.top().second;
    que.pop();
    if (book[x]) continue;
    book[x] = 1;
    for (register int i = fst[x]; i; i = nxt[i])
      if (dis[v[i]] > dis[u[i]] + w[i]) {
        dis[v[i]] = dis[u[i]] + w[i];
        que.push(make_pair(dis[v[i]], v[i]));
      }
  }
  if (dis[n] == inf)
    cout << "-1" << endl;
  else
    cout << dis[n] << endl;
  return 0;
}
