#include <bits/stdc++.h>
using namespace std;
const int OO = 2e9;
const int MOD = 1e9 + 7;
const double Pi = 3.141592653589793;
const int N = 1e5 + 5;
vector<pair<int, int> > g[N];
int b[2][N], c[N], cl[N], s[2], trace[N], forw[N], u2[N], u1[N], w[N],
    t = 0, color = 1, usage = 0, n, m;
bool visit[N];
void push(int u, int t) {
  cl[u] = color;
  s[t]++;
  b[t][s[t]] = u;
}
int main() {
  int x, y, z, cost, u, v;
  scanf("%i %i", &n, &m);
  for (int i = (int)1; i <= (int)m; i++) {
    scanf("%i %i %i", &x, &y, &z);
    g[x].push_back(pair<int, int>(y, z));
    g[y].push_back(pair<int, int>(x, z));
    u1[i] = x;
    u2[i] = y;
    w[i] = z;
    usage += z;
  }
  memset(cl, 0, sizeof(cl));
  s[0] = s[1] = 0;
  c[1] = usage;
  push(1, 0);
  do {
    color++;
    s[1 - t] = 0;
    for (int i = (int)1; i <= (int)s[t]; i++) {
      u = b[t][i];
      for (int j = (int)0; j <= (int)(int)g[u].size() - 1; j++) {
        cost = c[u];
        if (g[u][j].second == 0)
          cost++;
        else
          cost--;
        v = g[u][j].first;
        if (cl[v] == 0) {
          push(v, 1 - t);
          c[v] = cost;
          trace[v] = u;
        } else if (cl[v] == color && c[v] > cost) {
          c[v] = cost;
          trace[v] = u;
        }
      }
    }
    t = 1 - t;
  } while (cl[n] == 0);
  x = n;
  memset(forw, 0, sizeof(forw));
  while (x != 1) {
    forw[trace[x]] = x;
    x = trace[x];
  }
  printf("%i\n", c[n]);
  for (int i = (int)1; i <= (int)m; i++) {
    if (w[i] == 0 && (forw[u1[i]] == u2[i] || forw[u2[i]] == u1[i]))
      printf("%i %i 1\n", u1[i], u2[i]);
    if (w[i] == 1 && forw[u1[i]] != u2[i] && forw[u2[i]] != u1[i])
      printf("%i %i 0\n", u1[i], u2[i]);
  }
}
