#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000;
const long long MAX = 100005;
int N, M, From[MAX], a, b, c, i;
long long D[MAX], n, d, x;
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
               greater<pair<long long, long long>>>
    Q;
set<pair<int, int>> B, R, E[MAX];
int main() {
  scanf("%d%d", &N, &M);
  for (i = 0; i < M; i++) {
    scanf("%d%d%d", &a, &b, &c);
    E[a].insert({b, c}), E[b].insert({a, c});
    if (c) B.insert({a, b});
  }
  D[1] = ((int)B.size());
  Q.push({((int)B.size()), 1});
  for (i = 2; i <= N; i++) D[i] = INF;
  while (!Q.empty()) {
    pair<long long, long long> p = Q.top();
    Q.pop();
    n = p.second;
    d = p.first;
    if (d != D[n]) continue;
    for (pair<int, int> pr : E[n]) {
      int a = pr.first;
      x = d + MAX + (pr.second ? -1 : 1);
      if (x < D[a]) {
        D[a] = x;
        From[a] = n;
        Q.push({x, a});
      }
    }
  }
  int n = N;
  while (n > 1) {
    int prev = From[n];
    if (D[prev] < D[n] - MAX)
      R.insert({prev, n});
    else
      B.erase({prev, n}), B.erase({n, prev});
    n = prev;
  }
  printf("%d\n", ((int)B.size()) + ((int)R.size()));
  for (pair<int, int> p : R) printf("%d %d 1\n", p.first, p.second);
  for (pair<int, int> p : B) printf("%d %d 0\n", p.first, p.second);
}
