#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, a[100000], b[100000], c[100000];
long long d[100000], p[100000], cnt;
vector<pair<long long, long long> > g[100000];
set<pair<long long, long long> > s, e;
void rec(long long v) {
  if (p[v] != -1) {
    rec(p[v]);
    e.insert({min(v, p[v]), max(v, p[v])});
  }
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    g[a[i]].push_back({b[i], 1000000 + 1 - c[i]});
    g[b[i]].push_back({a[i], 1000000 + 1 - c[i]});
    cnt += c[i];
  }
  fill(d, d + n, 1000000000000000000LL);
  d[0] = 0;
  p[0] = -1;
  s.insert({d[0], 0});
  for (j = 0; j < n && !s.empty(); j++) {
    int v = s.begin()->second;
    s.erase(s.begin());
    for (i = 0; i < g[v].size(); i++) {
      int to = g[v][i].first, cost = g[v][i].second;
      if (d[to] > d[v] + cost) {
        s.erase({d[to], to});
        d[to] = d[v] + cost;
        s.insert({d[to], to});
        p[to] = v;
      }
    }
  }
  rec(n - 1);
  cout << d[n - 1] % 1000000 + (cnt - (d[n - 1] / 1000000 - d[n - 1] % 1000000))
       << endl;
  for (i = 0; i < m; i++) {
    if (e.find({min(a[i], b[i]), max(a[i], b[i])}) == e.end()) {
      if (c[i]) cout << a[i] + 1 << " " << b[i] + 1 << " " << 0 << endl;
    } else {
      if (!c[i]) cout << a[i] + 1 << " " << b[i] + 1 << " " << 1 << endl;
    }
  }
  return 0;
}
