#include <bits/stdc++.h>
using namespace std;
inline bool equal(double _x, double _y) {
  return _x > _y - 1e-9 && _x < _y + 1e-9;
}
pair<int, int> add(pair<int, int> a, pair<int, int> b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
struct node {
  int fr, to, val;
} e[200005];
vector<pair<int, int> > v[200005];
priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int> >,
               greater<pair<pair<int, int>, int> > >
    pq;
int fr[200005], id[200005], n, m, cnt, us[200005];
bool vis[200005];
pair<int, int> d[200005], tmp;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) d[i] = make_pair(1e9, 1e9);
  d[1] = make_pair(0, 0);
  for (int i = 0; i < m; i++) {
    int fr, to, val;
    scanf("%d%d%d", &fr, &to, &val);
    e[i] = {fr, to, val};
    v[fr].push_back(make_pair(to, i));
    v[to].push_back(make_pair(fr, i));
  }
  pq.push(make_pair(make_pair(0, 0), 1));
  while (!pq.empty()) {
    int x = pq.top().second;
    pq.pop();
    if (vis[x]) continue;
    vis[x] = 1;
    for (auto i : v[x]) {
      if (d[i.first] > (tmp = add(d[x], make_pair(1, !e[i.second].val)))) {
        fr[i.first] = x;
        id[i.first] = i.second;
        d[i.first] = tmp;
        pq.push(make_pair(d[i.first], i.first));
      }
    }
  }
  int now = n;
  while (now != 1) {
    us[id[now]] = 1;
    now = fr[now];
  }
  for (int i = 0; i < m; i++)
    if (us[i] && !e[i].val || !us[i] && e[i].val) cnt++;
  printf("%d\n", cnt);
  for (int i = 0; i < m; i++)
    if (us[i] ^ e[i].val) printf("%d %d %d\n", e[i].fr, e[i].to, us[i]);
}
