#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool fs(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-') sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}
vector<int> node[3 * 1000002];
vector<long long int> cost[3 * 1000002];
long long int total = 0;
int par[3 * 1000002];
long long int d[3 * 1000002];
long long int piv[3 * 1000002];
map<pair<long long int, long long int>, bool> ed;
map<pair<long long int, long long int>, int> cell;
void bfs(int s, int n) {
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    piv[i] = 0;
    d[i] = 1000000000000000;
  }
  d[s] = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < node[u].size(); i++) {
      int v = node[u][i];
      long long int vcost = d[u] + cost[u][i];
      if (vcost < d[v]) {
        d[v] = vcost;
        total = total - piv[v] + cost[u][i];
        ed[pair<long long int, long long int>(par[v], v)] = false;
        ed[pair<long long int, long long int>(u, v)] = true;
        par[v] = u;
        piv[v] = cost[u][i];
        q.push(v);
      }
      if (vcost == d[v] && cost[u][i] < piv[v]) {
        d[v] = vcost;
        total = total - piv[v] + cost[u][i];
        ed[pair<long long int, long long int>(par[v], v)] = false;
        ed[pair<long long int, long long int>(u, v)] = true;
        par[v] = u;
        piv[v] = cost[u][i];
        q.push(v);
      }
    }
  }
}
int main() {
  int v, e, a, c, b;
  fs(v);
  fs(e);
  for (int i = 1; i <= e; i++) {
    fs(a);
    fs(b);
    fs(c);
    node[a].push_back(b);
    node[b].push_back(a);
    cost[a].push_back(c);
    cost[b].push_back(c);
    cell[pair<long long int, long long int>(a, b)] = i;
    cell[pair<long long int, long long int>(b, a)] = i;
  }
  fs(a);
  bfs(a, v);
  cout << total << endl;
  for (map<pair<long long int, long long int>, bool>::iterator it = ed.begin();
       it != ed.end(); it++)
    if (it->second) cout << cell[it->first] << ' ';
  cout << endl;
  return 0;
}
