#include <bits/stdc++.h>
using namespace std;
int get() {
  int ss = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9') {
    if (c == '-') ss = -1;
  }
  int res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') {
    res = res * 10 + c - '0';
  }
  return res * ss;
}
int go[100300 * 2], nex[100300 * 2], head[100300 * 2], jz[100300 * 2], tot;
int m, n;
bool vv[100300];
int lu[100300];
vector<int> fa[100300];
vector<int> zhi[100300];
queue<int> q;
void add(int p, int q, int o) {
  go[++tot] = q;
  nex[tot] = head[p];
  head[p] = tot;
  jz[tot] = o;
}
void spfa() {
  memset(lu, 0x3f3f3f, sizeof(lu));
  q.push(1);
  vv[1] = true;
  lu[1] = 0;
  int tem, tt;
  while (!q.empty()) {
    tem = q.front();
    q.pop();
    vv[tem] = false;
    for (int i = head[tem]; i; i = nex[i]) {
      tt = go[i];
      if (lu[tt] == 1 + lu[tem]) {
        fa[tt].push_back(tem);
        zhi[tt].push_back(jz[i]);
        if (!vv[tt]) {
          vv[tt] = true;
          q.push(tt);
        }
      }
      if (lu[tt] > lu[tem] + 1) {
        lu[tt] = lu[tem] + 1;
        while (!fa[tt].empty()) {
          fa[tt].clear();
          zhi[tt].clear();
        }
        fa[tt].push_back(tem);
        zhi[tt].push_back(jz[i]);
        if (!vv[tt]) {
          vv[tt] = true;
          q.push(tt);
        }
      }
    }
  }
}
int ans[100300], lo[100300], last;
void dfs(int u, int dep) {
  if (u == 1) {
    if (dep >= last) {
      last = dep;
      for (int i = 1; i <= n; i++) {
        ans[i] = lo[i];
      }
    }
    return;
  }
  for (int i = 0; i < fa[u].size(); i++) {
    lo[fa[u][i]] = u;
    dfs(fa[u][i], dep + zhi[u][i]);
    lo[fa[u][i]] = 0;
  }
}
bool zoul[100300];
int z1[100300], z2[100300], z3[100300], qwq;
void dfs2(int u, int fa) {
  int tem;
  for (int i = head[u]; i; i = nex[i]) {
    tem = go[i];
    if (zoul[(1 + i) / 2] == true) continue;
    if (ans[tem] == u || ans[u] == tem) {
      if (jz[i] == 0) {
        z1[++qwq] = u;
        z2[qwq] = tem;
        z3[qwq] = 1;
      }
    } else {
      if (jz[i] == 1) {
        z1[++qwq] = u;
        z2[qwq] = tem;
        z3[qwq] = 0;
      }
    }
    zoul[(i + 1) / 2] = true;
    dfs2(tem, u);
  }
}
int main() {
  n = get(), m = get();
  int x, y, z;
  for (int i = 1; i <= m; i++) {
    x = get();
    y = get();
    z = get();
    add(x, y, z);
    add(y, x, z);
  }
  spfa();
  memset(lo, 0, sizeof(lo));
  memset(zoul, 0, sizeof(zoul));
  last = 0;
  dfs(n, 0);
  dfs2(1, 0);
  cout << qwq << endl;
  for (int i = 1; i <= qwq; i++) {
    cout << z1[i] << " " << z2[i] << " " << z3[i] << endl;
  }
  return 0;
}
