#include <bits/stdc++.h>
using namespace std;
long long n, m;
pair<pair<long long, long long>, long long> e[100000 + 1];
vector<pair<long long, long long>> v[100000 + 1];
vector<long long> d1, dn;
vector<long long> dij(long long node) {
  vector<long long> dist(n + 1, 100000000000000);
  priority_queue<pair<long long, long long>> pq;
  pq.push({0, node});
  dist[node] = 0;
  while (!pq.empty()) {
    long long node = pq.top().second, d = -pq.top().first;
    pq.pop();
    for (auto x : v[node]) {
      if (dist[x.first] > d + 1) {
        pq.push({-d - 1, x.first});
        dist[x.first] = d + 1;
      }
    }
  }
  return dist;
}
vector<pair<long long, long long>> bfs(long long l) {
  vector<pair<long long, long long>> dp(n + 1, {100000000000000, -1});
  dp[1] = {0, -1};
  deque<long long> q;
  q.push_back(1);
  while (!q.empty()) {
    long long node = q.front();
    q.pop_front();
    for (auto x : v[node]) {
      if (d1[x.first] + dn[x.first] > l) continue;
      if (e[x.second].second == 1 and dp[x.first].first > dp[node].first) {
        q.push_front(x.first);
        dp[x.first] = {dp[node].first, x.second};
      } else if (e[x.second].second == 0 and
                 dp[x.first].first > dp[node].first + 1) {
        q.push_back(x.first);
        dp[x.first] = {dp[node].first + 1, x.second};
      }
    }
  }
  return dp;
}
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  long long a = 0;
  for (long long i = 1; i <= m; i++) {
    long long x, y, z;
    cin >> x >> y >> z;
    e[i] = {{x, y}, z};
    a += z;
    v[x].push_back({y, i});
    v[y].push_back({x, i});
  }
  d1 = dij(1), dn = dij(n);
  long long l = d1[n];
  auto dp = bfs(l);
  assert(dn[1] == d1[n]);
  cout << a - l + 2 * dp[n].first << '\n';
  long long node = n;
  set<long long> in;
  long long len = l;
  while (node != 1) {
    len--;
    in.insert(dp[node].second);
    if (e[dp[node].second].first.first == node) {
      node = e[dp[node].second].first.second;
    } else if (e[dp[node].second].first.second == node) {
      node = e[dp[node].second].first.first;
    }
  }
  assert(len == 0);
  long long b = 0;
  for (long long i = 1; i <= m; i++) {
    if (!in.count(i) and e[i].second == 1) {
      b++;
      cout << e[i].first.first << " " << e[i].first.second << " 0\n";
    } else if (in.count(i) and e[i].second == 0) {
      b++;
      cout << e[i].first.first << " " << e[i].first.second << " 1\n";
    }
  }
  assert(b == a - l + 2 * dp[n].first);
}
