#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
const long long int INF = 1000000000000LL;
vector<pair<int, int> > g[N];
long long int dist[N];
int _prev[N];
int n;
set<pair<int, int> > pq;
set<pair<int, int> > s;
vector<pair<pair<int, int>, int> > ans;
void solve() {
  for (int i = 2; i < n + 1; i++) {
    dist[i] = INF;
  }
  pq.insert(make_pair(0, 1));
  while (pq.size()) {
    pair<int, int> p = *pq.begin();
    pq.erase(p);
    int u = p.second;
    if (u == n) break;
    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i].first;
      int d = g[u][i].second;
      if (dist[u] + d < dist[v]) {
        if (dist[v] != INF) pq.erase(make_pair(dist[v], v));
        dist[v] = dist[u] + d;
        _prev[v] = u;
        pq.insert(make_pair(dist[v], v));
      }
    }
  }
  int p = n;
  while (p != 1) {
    int u = p;
    int v = _prev[p];
    if (v < u) swap(u, v);
    s.insert(make_pair(u, v));
    p = _prev[p];
  }
  for (int i = 1; i < n + 1; i++) {
    for (int j = 0; j < g[i].size(); j++) {
      int u = i;
      int v = g[i][j].first;
      if (v < u) continue;
      if (s.find(make_pair(u, v)) != s.end()) {
        if (g[i][j].second == 100001)
          ans.push_back(make_pair(make_pair(u, v), 1));
      } else {
        if (g[i][j].second == 100000)
          ans.push_back(make_pair(make_pair(u, v), 0));
      }
    }
  }
  printf("%d\n", (int)ans.size());
  for (int i = 0; i < ans.size(); i++) {
    printf("%d %d %d\n", ans[i].first.first, ans[i].first.second,
           ans[i].second);
  }
}
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    c = 100001 - c;
    g[u].push_back(make_pair(v, c));
    g[v].push_back(make_pair(u, c));
  }
  solve();
  return 0;
}
