#include <bits/stdc++.h>
using namespace std;
vector<int> v1[100005], v2[100005], val;
vector<pair<int, int> > edges;
set<pair<int, int> > s;
vector<pair<pair<int, int>, int> > ans;
set<int> num;
int cnt[100005], level[100005], bp[100005];
struct comp {
  bool operator()(pair<pair<pair<int, int>, int>, int> a,
                  pair<pair<pair<int, int>, int>, int> b) {
    if (a.first.second == b.first.second)
      return a.first.first.second < b.first.first.second;
    return a.first.second > b.first.second;
  }
};
int main() {
  int n, m, x, y, w, i, sum = 0;
  scanf("%d", &n);
  scanf("%d", &m);
  for (i = 0; i <= n; i++) {
    level[i] = INT_MAX;
    cnt[i] = INT_MIN;
  }
  for (i = 0; i < m; i++) {
    scanf("%d", &x);
    scanf("%d", &y);
    scanf("%d", &w);
    v1[x].push_back(y);
    v1[y].push_back(x);
    v2[x].push_back(w);
    v2[y].push_back(w);
    edges.push_back(make_pair(x, y));
    val.push_back(w);
  }
  priority_queue<pair<pair<pair<int, int>, int>, int>,
                 vector<pair<pair<pair<int, int>, int>, int> >, comp>
      pq;
  pq.push(make_pair(make_pair(make_pair(1, 0), 0), 0));
  while (!pq.empty()) {
    pair<pair<pair<int, int>, int>, int> a = pq.top();
    pq.pop();
    if (cnt[a.first.first.first] != INT_MIN) continue;
    cnt[a.first.first.first] = a.first.first.second;
    level[a.first.first.first] = a.first.second;
    bp[a.first.first.first] = a.second;
    for (i = 0; i < v1[a.first.first.first].size(); i++) {
      if (cnt[v1[a.first.first.first][i]] != INT_MIN) continue;
      if (v2[a.first.first.first][i] == 1)
        pq.push(make_pair(make_pair(make_pair(v1[a.first.first.first][i],
                                              a.first.first.second + 1),
                                    a.first.second + 1),
                          a.first.first.first));
      else
        pq.push(make_pair(make_pair(make_pair(v1[a.first.first.first][i],
                                              a.first.first.second),
                                    a.first.second + 1),
                          a.first.first.first));
    }
  }
  for (i = n; i != 1;) {
    s.insert(make_pair(i, bp[i]));
    i = bp[i];
  }
  for (i = 0; i < edges.size(); i++) {
    if (s.find(edges[i]) == s.end() &&
        s.find(make_pair(edges[i].second, edges[i].first)) == s.end()) {
      if (val[i] == 1) {
        sum++;
        ans.push_back(make_pair(edges[i], 0));
      }
    } else {
      if (val[i] == 0) {
        sum++;
        ans.push_back(make_pair(edges[i], 1));
      }
    }
  }
  cout << sum << endl;
  for (i = 0; i < ans.size(); i++) {
    cout << ans[i].first.first << " " << ans[i].first.second << " "
         << ans[i].second << endl;
  }
  return 0;
}
