#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long _INF = 0xc0c0c0c0c0c0c0c0;
const long long mod = (int)1e9 + 7;
const int N = 1e5 + 100;
vector<pair<int, int> > vc[N];
struct Node {
  int o, u, d, p;
  bool operator<(const Node &x) const {
    if (d == x.d) return p > x.p;
    return d > x.d;
  }
};
int pre[N];
int vis[N];
priority_queue<Node> pq;
void dij() {
  pq.push({0, 1, 0, 0});
  while (!pq.empty()) {
    Node t = pq.top();
    pq.pop();
    if (vis[t.u]) continue;
    vis[t.u] = 1;
    pre[t.u] = t.o;
    for (pair<int, int> tmp : vc[t.u]) {
      if (vis[tmp.first]) continue;
      pq.push({t.u, tmp.first, t.d + 1, t.p + !tmp.second});
    }
  }
}
vector<Node> ans;
int fpre[N];
int main() {
  int n, m, u, v, op;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &u, &v, &op);
    vc[u].push_back(pair<int, int>(v, op));
    vc[v].push_back(pair<int, int>(u, op));
  }
  dij();
  int x = n;
  while (x) {
    fpre[x] = pre[x];
    x = pre[x];
  }
  for (int i = 1; i <= n; ++i) {
    for (pair<int, int> &t : vc[i]) {
      if (t.first < i) {
        int v = t.first;
        if ((fpre[i] == v || fpre[v] == i)) {
          if (!t.second) ans.push_back({v, i, 1, 0});
        } else if (t.second)
          ans.push_back({v, i, 0, 0});
      }
    }
  }
  printf("%d\n", ans.size());
  for (Node &t : ans) {
    printf("%d %d %d\n", t.o, t.u, t.d);
  }
  return 0;
}
