#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (b == 0)
    return a;
  else
    return (b, a % b);
}
long long int expo(long long int a, long long int b) {
  long long int r = 1;
  a = a % 1000000007;
  while (b) {
    if (b % 2) r = (r * a) % 1000000007;
    a = (a * a) % 1000000007;
    b = b / 2;
  }
  return r;
}
vector<pair<int, int> > adj[100001];
vector<int> vis(100001, 0);
vector<int> good(100001, 0);
vector<int> par(100001, 0);
vector<int> dis(100001, 0);
void bfs() {
  queue<int> q;
  vis[1] = 1;
  q.push(1);
  while (!q.empty()) {
    int i = q.front();
    q.pop();
    for (int j = 0; j < adj[i].size(); j++) {
      if (vis[adj[i][j].first] == 0) {
        q.push(adj[i][j].first);
        par[adj[i][j].first] = i;
        good[adj[i][j].first] = good[i];
        if (adj[i][j].second == 1) {
          good[adj[i][j].first]++;
        }
        dis[adj[i][j].first] = dis[i] + 1;
        vis[adj[i][j].first] = 1;
      } else if (adj[i][j].first != par[i] &&
                 (dis[adj[i][j].first] == (dis[i] + 1))) {
        if (adj[i][j].second == 0) {
          if (good[adj[i][j].first] < good[i]) {
            par[adj[i][j].first] = i;
            good[adj[i][j].first] = good[i];
          }
        } else {
          if (good[adj[i][j].first] < (good[i] + 1)) {
            par[adj[i][j].first] = i;
            good[adj[i][j].first] = good[i] + 1;
          }
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, yy = 0;
  cin >> n >> m;
  map<pair<int, int>, int> ma;
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (z == 1) yy++;
    ma[make_pair(x, y)] = ((z == 0) ? -1 : 1);
    ma[make_pair(y, x)] = ((z == 0) ? -1 : 1);
    adj[x].push_back(make_pair(y, z));
    adj[y].push_back(make_pair(x, z));
  }
  bfs();
  cout << (dis[n] + yy - 2 * good[n]) << endl;
  int i = n;
  while (par[i] != 0) {
    if (ma[make_pair(i, par[i])] == -1) {
      cout << i << " " << par[i] << " " << 1 << endl;
    }
    ma[make_pair(i, par[i])] = ma[make_pair(par[i], i)] = -1;
    i = par[i];
  }
  for (auto it = (ma).begin(); it != (ma).end(); it++) {
    if (ma[make_pair(it->first.first, it->first.second)] == 1) {
      cout << it->first.first << " " << it->first.second << " " << 0 << endl;
      ma[make_pair(it->first.first, it->first.second)] =
          ma[make_pair(it->first.second, it->first.first)] = -1;
    }
  }
  return 0;
}
