#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const long long INFll = 10000000000000000ll;
struct e {
  int to;
  int w;
  int num;
};
vector<vector<e> > v;
e new_e(int to, int w, int num) {
  e t;
  t.to = to;
  t.w = w;
  t.num = num;
  return t;
}
bool b[500100];
int main() {
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  v.resize(n + 1);
  int x, y, z;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    v[x].push_back(new_e(y, z, i + 1));
    v[y].push_back(new_e(x, z, i + 1));
  }
  int root;
  cin >> root;
  priority_queue<pair<pair<long long, long long>, pair<int, int> > > q;
  vector<int> ans;
  q.push(make_pair(make_pair(0, 0), make_pair(root, 0)));
  long long ansv = 0;
  long long d1, d2;
  while (!q.empty()) {
    d1 = -q.top().first.first;
    d2 = -q.top().first.second;
    x = q.top().second.first;
    y = q.top().second.second;
    q.pop();
    if (b[x]) {
      continue;
    }
    ans.push_back(y);
    b[x] = true;
    ansv += d2;
    for (int i = 0; i < v[x].size(); i++) {
      if (b[v[x][i].to]) continue;
      q.push(make_pair(make_pair(-d1 - v[x][i].w, -v[x][i].w),
                       make_pair(v[x][i].to, v[x][i].num)));
    }
  }
  cout << ansv << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] << " ";
  }
  return 0;
}
