#include <bits/stdc++.h>
using namespace std;
const int INF = 1234567890;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<vector<pair<int, bool> > > g(n);
  for (int i = 0; i < m; i++) {
    int a, b, z;
    scanf("%d%d%d", &a, &b, &z);
    --a, --b;
    g[a].push_back({b, 1 - z});
    g[b].push_back({a, 1 - z});
  }
  vector<pair<int, int> > dist(n, pair<int, int>(INF, INF));
  dist[0] = {0, 0};
  set<pair<pair<int, int>, int> > q;
  q.insert({dist[0], 0});
  vector<int> p(n, -1);
  while (!q.empty()) {
    int v = q.begin()->second;
    q.erase(q.begin());
    for (auto to : g[v]) {
      int u = to.first;
      int z = to.second;
      pair<int, int> newd = {dist[v].first + 1, dist[v].second + z};
      if (dist[u] > newd) {
        q.erase({dist[u], u});
        dist[u] = newd;
        p[u] = v;
        q.insert({dist[u], u});
      }
    }
  }
  set<pair<int, int> > in, repair;
  for (int v = n - 1; v != 0; v = p[v]) {
    int mn = min(v, p[v]), mx = max(v, p[v]);
    in.insert({mn, mx});
    if (dist[v].second == dist[p[v]].second + 1) repair.insert({mn, mx});
  }
  set<pair<pair<int, int>, int> > ans;
  for (int v = 0; v < n; v++) {
    for (auto to : g[v]) {
      int u = to.first;
      int z = to.second;
      int mn = min(u, v);
      int mx = max(u, v);
      if (repair.find({mn, mx}) != repair.end()) {
        ans.insert({{mn, mx}, 1});
      } else if (in.find({mn, mx}) == in.end() && z == 0) {
        ans.insert({{mn, mx}, 0});
      }
    }
  }
  printf("%d\n", (int)ans.size());
  for (auto x : ans) {
    printf("%d %d %d\n", x.first.first + 1, x.first.second + 1, x.second);
  }
}
