#include <bits/stdc++.h>
using namespace std;
long long int n, m, t1, t2, t3, dist[100001], parent[100001], blownup[100001],
    inpath[100001], ans = 0, visit[100001];
vector<vector<pair<long long int, long long int> > > graph(100001);
void bfs(long long int n, long long int steps, long long int blown) {
  for (int i = 0; i < graph[n].size(); i++) {
    pair<long long int, long long int> temp = graph[n][i];
    long long int blown_for_this = blown + (temp.second == 0 ? 1 : 0);
    if (dist[temp.first] > steps + 1) {
      dist[temp.first] = steps + 1;
      parent[temp.first] = n;
      blownup[temp.first] = blown_for_this;
      bfs(temp.first, steps + 1, blown_for_this);
    } else if (dist[temp.first] == steps + 1) {
      if (blownup[temp.first] > blown_for_this) {
        dist[temp.first] = steps + 1;
        parent[temp.first] = n;
        blownup[temp.first] = blown_for_this;
        bfs(temp.first, steps + 1, blown_for_this);
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> t1 >> t2 >> t3;
    graph[t1].push_back(make_pair(t2, t3));
    graph[t2].push_back(make_pair(t1, t3));
  }
  for (int i = 0; i <= n; i++) {
    dist[i] = blownup[i] = 1000000000;
    inpath[i] = visit[i] = 0;
  }
  dist[1] = 0;
  priority_queue<
      pair<long long int,
           pair<long long int, pair<long long int, long long int> > > >
      q;
  q.push(make_pair(0, make_pair(0, make_pair(1, 1))));
  long long int temp2 = 0;
  while (!q.empty() and temp2 != n) {
    pair<long long int,
         pair<long long int, pair<long long int, long long int> > >
        temp = q.top();
    q.pop();
    long long int node = temp.second.second.second, steps = -temp.first,
                  blown = -temp.second.first;
    if (dist[node] > steps or (dist[node] == steps and blownup[node] > blown)) {
      dist[node] = steps;
      blownup[node] = blown;
      parent[node] = temp.second.second.first;
      for (int i = 0; i < graph[node].size(); i++) {
        long long int blown_for_this =
            blown + (graph[node][i].second == 0 ? 1 : 0);
        q.push(make_pair(
            -(steps + 1),
            make_pair(-blown_for_this, make_pair(node, graph[node][i].first))));
      }
    }
    if (visit[node] == 0) {
      visit[node] = 1;
      temp2++;
    }
  }
  vector<pair<long long int, long long int> > blow_up, repair;
  int j = n;
  while (j != 1) {
    inpath[j] = 1;
    j = parent[j];
  }
  inpath[1] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < graph[i].size(); j++) {
      pair<long long int, long long int> temp = graph[i][j];
      if (temp.first < i) continue;
      if (inpath[i] and inpath[temp.first]) {
        if (parent[i] == temp.first or parent[temp.first] == i) {
          if (temp.second == 0) {
            ans++;
            repair.push_back(make_pair(i, temp.first));
          }
          continue;
        }
      }
      if (temp.second == 1) {
        ans++;
        blow_up.push_back(make_pair(i, temp.first));
      }
    }
  }
  cout << ans << "\n";
  for (int i = 0; i < blow_up.size(); i++) {
    cout << blow_up[i].first << " " << blow_up[i].second << " " << 0 << "\n";
  }
  for (int i = 0; i < repair.size(); i++) {
    cout << repair[i].first << " " << repair[i].second << " " << 1 << "\n";
  }
}
