#include <bits/stdc++.h>
using namespace std;
int n, m, tot, edg[100005][3];
vector<pair<int, int> > e[100005];
long long res[100005], dis[100005];
struct edge {
  int u, t, p, i, d;
  edge(int _u, int _t, int _p, int _i, int _d) {
    u = _u;
    t = _t;
    p = _p;
    i = _i;
    d = _d;
  }
  edge() {
    u = t = i = d = 0;
    p = -1;
  }
} par[100005];
inline void doit(edge& t) {
  if (t.d == dis[t.u] && t.p >= 0 && res[t.p] + t.t >= res[t.u]) {
    res[t.u] = res[t.p] + t.t;
    par[t.u] = t;
  }
}
void dijkstra() {
  queue<edge> q;
  q.push(edge(0, 0, -1, -1, 0));
  while (!q.empty()) {
    edge t = q.front();
    q.pop();
    if (dis[t.u] != -1) {
      doit(t);
      continue;
    }
    dis[t.u] = t.d;
    doit(t);
    for (int i = 0; i < e[t.u].size(); i++) {
      int v = e[t.u][i].first, tv = e[t.u][i].second % 2,
          iv = e[t.u][i].second / 2;
      q.push(edge(v, tv, t.u, iv, t.d + 1));
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    edg[i][0] = u;
    edg[i][1] = v;
    edg[i][2] = c;
    e[u].push_back(make_pair(v, 2 * i + c));
    e[v].push_back(make_pair(u, 2 * i + c));
    tot += c;
  }
  memset(dis, -1, sizeof dis);
  dijkstra();
  cout << dis[n - 1] + tot - 2 * res[n - 1] << "\n";
  int nd = n - 1;
  while (nd > 0) {
    int idx = par[nd].i;
    if (!edg[idx][2])
      cout << edg[idx][0] + 1 << " " << edg[idx][1] + 1 << " " << 1 << "\n";
    else
      edg[idx][2] = 0;
    nd = par[nd].p;
  }
  for (int i = 0; i < m; i++)
    if (edg[i][2])
      cout << edg[i][0] + 1 << " " << edg[i][1] + 1 << " " << 0 << "\n";
  return 0;
}
