#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
struct edge {
  int u, v, kid;
} e[400000];
struct node {
  int i, l, cost;
  friend bool operator<(node a, node b) { return a.l > b.l; }
};
vector<int> G[400000];
int frt[400000];
int eee[400000];
int n, m;
priority_queue<node> q;
bool mark[400000];
node d[400000];
int zz[400000];
void Dstra() {
  int st = 1, nd = n;
  while (!q.empty()) q.pop();
  memset(mark, 0, sizeof(mark));
  for (int i = 0; i <= n; i++) d[i].l = INF, d[i].cost = 0, d[i].i = i;
  d[st].l = 0;
  q.push(d[st]);
  while (!q.empty()) {
    node x = q.top();
    q.pop();
    int u = x.i;
    if (mark[u]) continue;
    mark[u] = 1;
    for (int i = 0; i < G[u].size(); i++) {
      edge ee = e[G[u][i]];
      int v = ee.v;
      if (v == 8) {
        int a = 1;
      }
      int kid = ee.kid;
      if (d[v].l < d[u].l + 1) continue;
      if (d[v].l > d[u].l + 1) {
        d[v].l = d[u].l + 1;
        d[v].cost = d[u].cost + (kid ^ 1);
        frt[v] = u;
        eee[v] = G[u][i];
        q.push(d[v]);
      }
      if (d[v].l == d[u].l + 1) {
        if (d[v].cost > d[u].cost + (kid ^ 1)) {
          d[v].cost = d[u].cost + (kid ^ 1);
          frt[v] = u;
          eee[v] = G[u][i];
          q.push(d[v]);
        }
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i <= n; i++) G[i].clear();
  for (int i = 0; i < m; i++) {
    int u, v, kid;
    scanf("%d%d%d", &u, &v, &kid);
    e[i << 1].u = u;
    e[i << 1].v = v;
    e[i << 1].kid = kid;
    e[i << 1 | 1].u = v;
    e[i << 1 | 1].v = u;
    e[i << 1 | 1].kid = kid;
    G[u].push_back(i << 1);
    G[v].push_back(i << 1 | 1);
  }
  memset(frt, -1, sizeof(frt));
  memset(zz, 0, sizeof(zz));
  Dstra();
  int k = n;
  while (k != 1) {
    zz[eee[k] / 2] = 1;
    k = frt[k];
  }
  int cnt = 0;
  for (int i = 0; i < m; i++)
    if (e[i << 1].kid ^ zz[i]) cnt++;
  printf("%d\n", cnt);
  for (int i = 0; i < m; i++)
    if (e[i << 1].kid ^ zz[i])
      printf("%d %d %d\n", e[i << 1].u, e[i << 1].v, zz[i]);
}
