#include <bits/stdc++.h>
using namespace std;
void __print(int first) { cerr << first; }
void __print(long first) { cerr << first; }
void __print(long long first) { cerr << first; }
void __print(unsigned first) { cerr << first; }
void __print(unsigned long first) { cerr << first; }
void __print(unsigned long long first) { cerr << first; }
void __print(float first) { cerr << first; }
void __print(double first) { cerr << first; }
void __print(long double first) { cerr << first; }
void __print(char first) { cerr << '\'' << first << '\''; }
void __print(const char *first) { cerr << '\"' << first << '\"'; }
void __print(const string &first) { cerr << '\"' << first << '\"'; }
void __print(bool first) { cerr << (first ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &first) {
  cerr << '{';
  __print(first.first);
  cerr << ',';
  __print(first.second);
  cerr << '}';
}
template <typename T>
void __print(const T &first) {
  int f = 0;
  cerr << '{';
  for (auto &i : first) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
struct edge {
  long long node;
  long long w;
  long long total_w;
  long long no;
};
map<pair<long long, long long>, long long> edge_no;
struct comp {
  bool operator()(edge a, edge b) const {
    if (a.total_w != b.total_w)
      return a.total_w < b.total_w;
    else if (a.w != b.w)
      return a.w < b.w;
    else
      return a.node < b.node;
  }
};
vector<vector<pair<long long, long long> > > vect;
vector<long long> ans;
long long visited[500000];
long long sum;
void solve(long long root) {
  set<edge, comp> s;
  s.insert({root, 0});
  while (!s.empty()) {
    edge temp = *(s.begin());
    s.erase(s.begin());
    if (visited[temp.node]) continue;
    visited[temp.node] = 1;
    sum += temp.w;
    if (temp.node != root) ans.push_back(temp.no);
    for (auto &child : vect[temp.node]) {
      s.insert({child.first, child.second, child.second + temp.total_w,
                edge_no[{min(child.first, temp.node),
                         max(child.first, temp.node)}]});
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  vect.resize(n + 1);
  long long u, v, w;
  for (long long i = 0; i < m; i++) {
    cin >> u >> v >> w;
    vect[u].push_back({v, w});
    vect[v].push_back({u, w});
    edge_no[{min(u, v), max(u, v)}] = i + 1;
  }
  long long root;
  cin >> root;
  solve(root);
  sort(ans.begin(), ans.end());
  cout << sum << "\n";
  for (auto &node : ans) cout << node << " ";
}
