#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[100005], p[100005];
vector<pair<int, int> > g[100005];
int w[100005], v[100005], d[100005], f[100005];
int main() {
  int T, i, j, k, n, ca = 0, m;
  while (~scanf("%d%d", &n, &m)) {
    int sum = 0;
    for (int it = 0; it < m; it++) {
      scanf("%d%d%d", &i, &j, &k);
      g[i].push_back(make_pair(j, it));
      g[j].push_back(make_pair(i, it));
      w[it] = k;
      a[it] = make_pair(i, j);
      sum += k;
    }
    for (i = 1; i <= n; i++) d[i] = 1000000007, v[i] = 0;
    queue<int> q;
    q.push(1);
    d[1] = 0;
    v[1] = 1;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      v[u] = 0;
      for (i = 0; i < g[u].size(); i++) {
        j = g[u][i].first;
        k = g[u][i].second;
        if (d[j] > d[u] + 1) {
          d[j] = d[u] + 1;
          f[j] = f[u] + w[k];
          p[j] = make_pair(u, k);
          if (!v[j]) v[j] = 1, q.push(j);
        } else if (d[j] == d[u] + 1 && f[j] < f[u] + w[k]) {
          f[j] = f[u] + w[k];
          p[j] = make_pair(u, k);
          if (!v[j]) v[j] = 1, q.push(j);
        }
      }
    }
    int ans = sum + d[n] - f[n] * 2;
    printf("%d\n", ans);
    vector<int> res;
    i = n;
    while (i != 1) {
      j = p[i].second;
      if (w[j] == 0) printf("%d %d %d\n", i, p[i].first, 1);
      v[j] = 1;
      i = p[i].first;
    }
    for (i = 0; i < m; i++)
      if (!v[i] && w[i] == 1) printf("%d %d %d\n", a[i].first, a[i].second, 0);
  }
}
