#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int M = 10001;
const int inf = 1e9 + 7;
const long long base = 1e18;
const double pi = acos(-1);
const double ep = 1e-9;
deque<int> q;
vector<pair<int, int> > adj[N];
pair<pair<int, int>, int> ed[N];
int d[N], sl[N];
int pa[N];
int kt[N];
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  int k = 0;
  for (int i = (int)0; i < (int)m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    adj[u].push_back(make_pair(v, c));
    adj[v].push_back(make_pair(u, c));
    k += c;
  }
  memset(d, -1, sizeof(d));
  q.push_back(1);
  d[1] = 0;
  pa[1] = -1;
  while (!q.empty()) {
    int u = q.front();
    q.pop_front();
    for (int j = (int)0; j < (int)adj[u].size(); j++) {
      int v = adj[u][j].first;
      int c = adj[u][j].second;
      if (d[v] == -1) {
        d[v] = d[u] + 1;
        pa[v] = u;
        sl[v] = sl[u] + c;
        q.push_back(v);
      } else {
        if (d[v] == d[u] + 1) {
          if (sl[u] + c > sl[v]) {
            sl[v] = sl[u] + c;
            pa[v] = u;
          }
        }
      }
    }
  }
  int t = n;
  kt[1] = 1;
  while (t != 1) {
    kt[t] = 1;
    t = pa[t];
  }
  cout << d[n] - 2 * sl[n] + k << endl;
  for (int u = 1; u <= n; u++) {
    for (int j = (int)0; j < (int)adj[u].size(); j++) {
      int v = adj[u][j].first;
      int c = adj[u][j].second;
      if (u < v) {
        if (kt[u] && kt[v] && (pa[v] == u || pa[u] == v)) {
          if (c == 0) cout << u << " " << v << " 1\n";
        } else if (c == 1)
          cout << u << " " << v << " 0\n";
      }
    }
  }
}
