#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
long long power(long long b, long long e, long long m) {
  if (e == 0) return 1;
  if (e & 1) return b * power(b * b % m, e / 2, m) % m;
  return power(b * b % m, e / 2, m);
}
long long power(long long b, long long e) {
  if (e == 0) return 1;
  if (e & 1) return b * power(b * b, e / 2);
  return power(b * b, e / 2);
}
template <typename T, typename U>
static inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
  if (x < y) x = y;
}
const int N = 300005;
long long n;
bool vis[N];
map<pair<long long, long long>, long long> mp, we;
long long dis[N], par[N];
vector<pair<long long, long long>> v[N];
void djikstra(long long so) {
  set<pair<long long, long long>> s;
  par[so] = so;
  for (long long i = 1; i <= n; ++i) dis[i] = 2000000000000000000;
  dis[so] = 0;
  s.insert({0, so});
  while (!s.empty()) {
    pair<long long, long long> p = *s.begin();
    s.erase(s.begin());
    for (auto j : v[p.second]) {
      if (dis[p.second] + j.second <= dis[j.first]) {
        if (dis[j.first] != 2000000000000000000)
          s.erase({dis[j.first], j.first});
        dis[j.first] = dis[p.second] + j.second;
        par[j.first] = p.second;
        s.insert({dis[j.first], j.first});
      }
    }
  }
}
int _runtimeTerror_() {
  long long m, i;
  cin >> n >> m;
  for (i = 1; i <= m; ++i) {
    long long x, y, w;
    cin >> x >> y >> w;
    if (x > y) swap(x, y);
    mp[{x, y}] = i;
    we[{x, y}] = w;
    v[x].push_back({y, w}), v[y].push_back({x, w});
  }
  long long u;
  cin >> u;
  djikstra(u);
  vector<long long> ans;
  set<long long> s;
  s.insert(u);
  long long val = 0;
  for (i = 1; i <= n; ++i) {
    if (s.count(i)) continue;
    long long x = i;
    while (par[x] != x && !s.count(x)) {
      s.insert(x);
      ans.push_back(mp[{min(x, par[x]), max(x, par[x])}]);
      val += we[{min(par[x], x), max(par[x], x)}];
      x = par[x];
    }
  }
  cout << val << "\n";
  for (auto j : ans) cout << j << " ";
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) _runtimeTerror_();
  cerr << "\n" << 1.0 * clock() / CLOCKS_PER_SEC;
  return 0;
}
