#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int mxN = 2e5;
struct edge {
  int to;
  bool broken;
  edge(int to, bool broken) : to(to), broken(broken) {}
};
vector<int> from(mxN), to(mxN), broken(mxN);
vector<vector<edge>> g(mxN);
const long long INF = 1e15;
void dijkstra(int s, vector<int> &dist) {
  priority_queue<array<long long, 2>, vector<array<long long, 2>>,
                 greater<array<long long, 2>>>
      pq;
  dist[s] = 0;
  pq.push({0, s});
  while (!pq.empty()) {
    auto x = pq.top();
    pq.pop();
    long long d = x[0], u = x[1];
    for (edge ed : g[u]) {
      int v = ed.to, w = 1;
      if (dist[v] > dist[u] + w) {
        dist[v] = dist[u] + w;
        pq.push({dist[v], v});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, wall = 0;
  cin >> n >> m;
  for (int(i) = 0; (i) < (m); (i)++) {
    int u, v, z;
    cin >> u >> v >> z;
    from[i] = v;
    to[i] = u;
    broken[i] = 1 - z;
    g[u].push_back(edge(v, 1 - z));
    g[v].push_back(edge(u, 1 - z));
    wall += z;
  }
  vector<int> d1(mxN, 1e9), dn(mxN, 1e9), vis(mxN);
  dijkstra(1, d1);
  dijkstra(n, dn);
  vector<int> dp(mxN, 1e9), p(mxN, -1);
  dp[1] = 0;
  int d = d1[n];
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (vis[u]) continue;
    vis[u] = 1;
    for (edge ed : g[u]) {
      if (d1[ed.to] + dn[ed.to] != d || d1[u] + dn[ed.to] + 1 != d ||
          vis[ed.to])
        continue;
      if (dp[ed.to] != 1e9 && dp[ed.to] <= dp[u] + ed.broken) continue;
      dp[ed.to] = dp[u] + ed.broken;
      p[ed.to] = u;
      q.push(ed.to);
    }
  }
  vector<int> path(mxN);
  int node = n;
  while (node != 1) {
    path[node] = p[node];
    node = path[node];
  }
  cout << 2 * dp[n] + wall - d << "\n";
  for (int(i) = 0; (i) < (m); (i)++) {
    if (path[from[i]] == to[i] || path[to[i]] == from[i]) {
      if (broken[i]) cout << from[i] << " " << to[i] << " 1\n";
    } else if (!broken[i])
      cout << from[i] << " " << to[i] << " 0\n";
  }
  return 0;
}
