#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
long long n, m, from[N], to[N], cond[N], i;
vector<pair<long long, long long> > v[N];
vector<pair<long long, long long> > nv[N];
queue<long long> q;
long long dist[N];
int vis[N] = {0};
void bfs(long long s) {
  q.push(s);
  dist[s] = 0;
  vis[s] = 1;
  while (!q.empty()) {
    long long x = q.front();
    q.pop();
    for (i = 0; i < v[x].size(); i++) {
      if (!vis[v[x][i].first]) {
        vis[v[x][i].first] = 1;
        dist[v[x][i].first] = dist[x] + 1;
        nv[v[x][i].first].push_back(make_pair(x, v[x][i].second));
        q.push(v[x][i].first);
      } else if (dist[v[x][i].first] == dist[x] + 1) {
        nv[v[x][i].first].push_back(make_pair(x, v[x][i].second));
      }
    }
  }
}
long long cnt1 = 0;
deque<long long> q1;
long long pred[N];
bool onPath[N] = {0};
void bfs1(long long s) {
  dist[s] = 0;
  q1.push_back(s);
  while (!q1.empty()) {
    long long x = q1.front();
    q1.pop_front();
    for (i = 0; i < nv[x].size(); i++) {
      long long y = nv[x][i].first;
      if (dist[y] > dist[x] + nv[x][i].second) {
        dist[y] = dist[x] + nv[x][i].second;
        pred[y] = x;
        if (nv[x][i].second == 0)
          q1.push_front(y);
        else
          q1.push_back(y);
      }
    }
  }
}
long long corr = 0;
int main() {
  scanf("%lld%lld", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &from[i], &to[i], &cond[i]);
    v[from[i]].push_back(make_pair(to[i], 1 - cond[i]));
    v[to[i]].push_back(make_pair(from[i], 1 - cond[i]));
    if (cond[i] == 1) corr++;
  }
  for (i = 0; i <= n; i++) dist[i] = 1000006;
  bfs(1);
  for (i = 0; i <= n; i++) dist[i] = 1000006;
  bfs1(n);
  long long j = 1;
  while (pred[j] != n) {
    onPath[j] = 1;
    j = pred[j];
    cnt1++;
  }
  cnt1++;
  onPath[n] = 1, onPath[j] = 1, onPath[1] = 1;
  long long ans = dist[1];
  ans += (corr - (cnt1 - dist[1]));
  printf("%lld\n", ans);
  for (i = 0; i < m; i++) {
    if (onPath[from[i]] == 1 && onPath[to[i]] == 1 && cond[i] == 0)
      printf("%lld %lld %lld\n", from[i], to[i], 1 - cond[i]);
    else if (cond[i] == 1 && (onPath[from[i]] == 0 || onPath[to[i]] == 0))
      printf("%lld %lld %lld\n", from[i], to[i], 1 - cond[i]);
  }
}
