#include <bits/stdc++.h>
using namespace std;
const int mx = 100011;
vector<vector<int> > v;
map<pair<int, int>, bool> mp;
pair<int, int> arr[mx];
int parent[mx] = {0};
vector<pair<int, int> > vp;
map<pair<int, int>, bool> vis;
int main() {
  for (int i = 0; i < mx; i++) arr[i] = pair<int, int>(9999999, 8999999);
  int n, m;
  cin >> n >> m;
  v.resize(n + 1);
  for (int i = 0; i < m; i++) {
    pair<int, int> p;
    bool a;
    cin >> p.first >> p.second >> a;
    v[p.first].push_back(p.second);
    v[p.second].push_back(p.first);
    vp.push_back(p);
    mp[p] = a;
    swap(p.first, p.second);
    vp.push_back(p);
    mp[p] = a;
  }
  queue<pair<int, int> > q;
  q.push(pair<int, int>(n, 0));
  arr[n] = pair<int, int>(0, 0);
  while (q.size()) {
    pair<int, int> cur = q.front();
    q.pop();
    for (int i = 0; i < v[cur.first].size(); i++) {
      if (arr[v[cur.first][i]].first >= cur.second + 1) {
        pair<int, int> pp = make_pair(cur.first, v[cur.first][i]);
        if (arr[v[cur.first][i]].second >
            arr[cur.first].second + (mp[pp] == 0)) {
          arr[v[cur.first][i]] =
              make_pair(cur.second + 1, arr[cur.first].second + (mp[pp] == 0));
          q.push(pair<int, int>(v[cur.first][i], cur.second + 1));
          parent[v[cur.first][i]] = cur.first;
        }
      }
    }
  }
  int cur = 1;
  map<pair<int, int>, bool> res;
  while (cur != n) {
    pair<int, int> pp = pair<int, int>(cur, parent[cur]);
    vis[pair<int, int>(cur, parent[cur])] = 1;
    vis[pair<int, int>(parent[cur], cur)] = 1;
    if (mp[pp] == 0) res[pp] = 1;
    cur = parent[cur];
  }
  for (int i = 0; i < vp.size(); i += 2) {
    if (!vis[vp[i]] && mp[vp[i]] == 1) res[vp[i]] = 0;
  }
  cout << res.size() << endl;
  map<pair<int, int>, bool>::iterator it = res.begin();
  for (int i = 0; i < res.size(); i++, it++) {
    cout << it->first.first << ' ' << it->first.second << ' ' << it->second
         << endl;
  }
}
