#include <bits/stdc++.h>
using namespace std;
int n, ed;
int ct[100005];
int parent[100005];
int visited[100005];
signed main() {
  int t = 1;
  ;
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(NULL);
  cout << fixed << setprecision(10);
  while (t-- > 0) {
    cin >> n >> ed;
    map<pair<int, int>, int> m;
    map<pair<int, int>, int> s;
    int ct1 = 0, ct2 = 0;
    memset(ct, 0, sizeof(ct));
    vector<vector<int>> adj(n);
    int tmp1, tmp2, tmp3;
    for (int i = 0; i < ed; i++) {
      cin >> tmp1 >> tmp2 >> tmp3;
      tmp1--;
      tmp2--;
      adj[tmp1].push_back(tmp2);
      adj[tmp2].push_back(tmp1);
      if (tmp3 == 0) {
        m[make_pair(tmp1, tmp2)] = 1;
        m[make_pair(tmp2, tmp1)] = 1;
        ct1++;
      } else {
        m[make_pair(tmp1, tmp2)] = 0;
        m[make_pair(tmp2, tmp1)] = 0;
      }
    }
    int start = 0;
    queue<int> q;
    q.push(start);
    memset(visited, 0, sizeof(visited));
    visited[start] = 1;
    while (!q.empty()) {
      start = q.front();
      q.pop();
      for (auto i : adj[start]) {
        if (visited[i] == (visited[start] + 1)) {
          if (m[{start, i}] == 1) {
            if (ct[i] > ct[start]) {
              ct[i] = ct[start] + 1;
              parent[i] = start;
            }
          } else {
            ct[i] = min(ct[i], ct[start]);
            if (ct[i] == ct[start]) {
              parent[i] = start;
            }
          }
        } else if (visited[i] == 0) {
          visited[i] = visited[start] + 1;
          parent[i] = start;
          ct[i] = ct[start];
          if (m[{start, i}] == 1) {
            ct[i]++;
          }
          q.push(i);
        }
      }
    }
    start = n - 1;
    int used = 0;
    while (start != 0) {
      used++;
      if (m[{start, parent[start]}] == 1) {
        s[{start, parent[start]}] = 1;
        m.erase({start, parent[start]});
        m.erase({parent[start], start});
        ct2++;
      } else {
        m.erase({start, parent[start]});
        m.erase({parent[start], start});
      }
      start = parent[start];
    }
    cout << ed - used - ct1 + 2 * ct2 << "\n";
    for (auto itr = s.begin(); itr != s.end(); itr++) {
      cout << itr->first.first + 1 << " " << itr->first.second + 1 << " 1\n";
    }
    for (auto itr = m.begin(); itr != m.end(); itr++) {
      m.erase({itr->first.second, itr->first.first});
    }
    for (auto itr = m.begin(); itr != m.end(); itr++) {
      if (itr->second != 1) {
        cout << itr->first.first + 1 << " " << itr->first.second + 1 << " 0\n";
      }
    }
  };
  return 0;
}
