#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
struct sw {
  int u, to, w, loc;
  sw() {}
  sw(int uu, int t, int ww, int lo) { u = uu, to = t, w = ww, loc = lo; }
} in[maxn];
struct ssw {
  int dist, u;
};
queue<ssw> Q;
vector<sw> v[maxn];
vector<int> hao;
vector<int> mei;
int n, m;
int vis[maxn];
int evis[maxn];
int dp1[maxn], dp2[maxn], fu[maxn], fue[maxn];
void dfs(int u) {
  sw t;
  ssw now, ne;
  now.dist = 0, now.u = u;
  Q.push(now);
  while (!Q.empty()) {
    now = Q.front();
    Q.pop();
    if (now.u == 1) continue;
    if (dp1[1] != -1 && now.dist > dp1[1]) break;
    u = now.u;
    for (int i = 0; i < v[u].size(); i++) {
      t = v[u][i];
      if (dp1[t.to] == -1 ||
          (dp1[u] + 1 <= dp1[t.to] && dp2[u] + t.w > dp2[t.to])) {
        fu[t.to] = u;
        fue[t.to] = t.loc;
        dp1[t.to] = dp1[u] + 1;
        dp2[t.to] = dp2[u] + t.w;
        ne.dist = now.dist + 1;
        ne.u = t.to;
        Q.push(ne);
      }
    }
  }
}
int main(void) {
  int res;
  int t;
  while (~scanf("%d%d", &n, &m)) {
    res = 0;
    memset(dp1, -1, sizeof(dp1));
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d", &in[i].u, &in[i].to, &in[i].w);
      in[i].loc = i;
      res += in[i].w;
      v[in[i].u].push_back(in[i]);
      v[in[i].to].push_back(sw(in[i].to, in[i].u, in[i].w, in[i].loc));
      if (in[i].w) hao.push_back(i);
    }
    dp1[n] = dp2[n] = 0;
    dfs(n);
    int s = 1;
    for (int i = 0; i < dp1[1]; i++) {
      t = fue[s];
      s = fu[s];
      if (in[t].w) {
        evis[t] = 1;
      } else
        mei.push_back(t);
    }
    printf("%d\n", res + dp1[1] - 2 * dp2[1]);
    for (int i = 0; i < mei.size(); i++) {
      t = mei[i];
      printf("%d %d %d\n", in[t].u, in[t].to, 1);
    }
    for (int i = 0; i < hao.size(); i++) {
      t = hao[i];
      if (!evis[t]) printf("%d %d %d\n", in[t].u, in[t].to, 0);
    }
  }
  return 0;
}
