#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 1013;
const long long INFL = 8e18 + 13ll;
const long long INF = 2e9 + 13;
const long long MOD = (int)1e9 + 7, MOD2 = 998244353;
const long long base = 101, base2 = 37;
vector<pair<int, int>> gr[maxn];
int dist[maxn], bad[maxn], p[maxn];
bool vis[maxn];
map<pair<int, int>, int> mp;
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  ;
  int n, m;
  cin >> n >> m;
  for (int i = 1, u, v, t; i <= m; ++i) {
    cin >> u >> v >> t;
    gr[u].push_back({v, !t});
    gr[v].push_back({u, !t});
    mp[{min(u, v), max(v, u)}] = !t;
  }
  for (int i = 1; i <= n; ++i) bad[i] = INF;
  bad[1] = 0;
  queue<int> q;
  q.push(1);
  p[1] = 1;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    vis[v] = 1;
    for (auto t : gr[v]) {
      if (!vis[t.first]) {
        q.push(t.first);
        vis[t.first] = 1;
        dist[t.first] = dist[v] + 1;
        bad[t.first] = bad[v] + t.second;
        p[t.first] = v;
      } else {
        if (dist[v] + 1 != dist[t.first]) continue;
        if (bad[t.first] > bad[v] + t.second) {
          bad[t.first] = bad[v] + t.second;
          p[t.first] = v;
        }
      }
    }
  }
  set<pair<pair<int, int>, int>> way;
  vector<pair<pair<int, int>, int>> ans;
  int v = n;
  while (v != p[v]) {
    way.insert(
        {{min(p[v], v), max(p[v], v)}, mp[{min(v, p[v]), max(v, p[v])}]});
    v = p[v];
  }
  for (auto t : mp) {
    if (way.find(t) == way.end()) {
      if (t.second == 0) {
        ans.push_back(t);
      }
    } else {
      if (t.second == 1) {
        ans.push_back(t);
      }
    }
  }
  cout << (int)(ans).size() << "\n";
  for (auto t : ans)
    cout << t.first.first << " " << t.first.second << " " << t.second << "\n";
  return 0;
}
