#include <bits/stdc++.h>
using namespace std;
struct q {
  int i, to, le;
  long long int tt;
} qt;
bool operator<(const q& a, const q& b) {
  if (a.tt != b.tt) return a.tt > b.tt;
  return a.le > b.le;
}
int n, m, u;
int ta, tb, tc;
bool v[300005];
long long int d[300005];
long long int ans;
vector<int> aa;
vector<int> ed[300005], to[300005], le[300005];
priority_queue<q> qq;
int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int a = 1; a <= m; a++) {
    scanf("%d", &ta);
    scanf("%d", &tb);
    scanf("%d", &tc);
    ed[ta].push_back(a);
    ed[tb].push_back(a);
    to[ta].push_back(tb);
    to[tb].push_back(ta);
    le[ta].push_back(tc);
    le[tb].push_back(tc);
  }
  scanf("%d", &u);
  v[u] = 1;
  for (int a = 0; a < ed[u].size(); a++)
    qq.push((q){ed[u][a], to[u][a], le[u][a], le[u][a]});
  while (!qq.empty()) {
    qt = qq.top();
    qq.pop();
    if (v[qt.to]) continue;
    v[qt.to] = 1;
    aa.push_back(qt.i);
    ans += qt.le;
    d[qt.to] = qt.tt;
    for (int a = 0; a < ed[qt.to].size(); a++) {
      if (!v[to[qt.to][a]])
        qq.push((q){ed[qt.to][a], to[qt.to][a], le[qt.to][a],
                    d[qt.to] + le[qt.to][a]});
    }
  }
  printf("%I64d\n", ans);
  for (int a = 0; a < aa.size(); a++)
    printf("%d%c", aa[a], a == aa.size() - 1 ? '\n' : ' ');
  return 0;
}
