#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > adj[(100005)];
int dist[(100005)];
int working[(100005)];
map<pair<int, int>, int> edges;
bool seen[(100005)];
int from[(100005)];
int main() {
  int u, v, c;
  int N, M;
  int num_working = 0;
  scanf("%d %d", &N, &M);
  for (int(m) = 0; (m) < (M); ++(m)) {
    scanf("%d %d %d", &u, &v, &c);
    u--, v--;
    edges[pair<int, int>(u, v)] = c;
    edges[pair<int, int>(v, u)] = c;
    num_working += c;
    adj[u].push_back(pair<int, int>(v, c));
    adj[v].push_back(pair<int, int>(u, c));
  }
  memset(dist, (0x3f3f3f3f), sizeof(dist));
  dist[0] = 0;
  priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int> >,
                 greater<pair<pair<int, int>, int> > >
      Q;
  Q.push(pair<pair<int, int>, int>(pair<int, int>(0, 0), 0));
  while (!Q.empty()) {
    u = Q.top().second;
    Q.pop();
    if (seen[u]) continue;
    seen[u] = true;
    for (pair<int, int> p : adj[u]) {
      v = p.first;
      if (dist[v] > dist[u] + 1) {
        dist[v] = dist[u] + 1;
        working[v] = working[u] + p.second;
        Q.push(
            pair<pair<int, int>, int>(pair<int, int>(dist[v], -working[v]), v));
        from[v] = u;
      } else if (dist[v] == dist[u] + 1) {
        if (working[v] < working[u] + p.second) {
          working[v] = working[u] + p.second;
          from[v] = u;
        }
      }
    }
  }
  cout << dist[N - 1] - working[N - 1] + (num_working - working[N - 1]) << endl;
  int now = N - 1;
  while (now) {
    int pre = from[now];
    if (edges[pair<int, int>(pre, now)] == 0) {
      printf("%d %d 1\n", pre + 1, now + 1);
    }
    edges[pair<int, int>(pre, now)] = -2;
    edges[pair<int, int>(now, pre)] = -2;
    now = pre;
  }
  for (auto it = edges.begin(); it != edges.end(); it++) {
    if (it->second == 1) {
      printf("%d %d 0\n", it->first.first + 1, it->first.second + 1);
      edges[pair<int, int>(it->first.second, it->first.first)] = -2;
    }
  }
  return 0;
}
