#include <bits/stdc++.h>
using namespace std;
struct pt {
  int i, m, b;
  pt() {}
  pt(int m, int b, int i) : m(m), b(b), i(i) {}
  friend bool operator<(pt x, pt y) {
    if (x.m != y.m) return x.m < y.m;
    if (x.b != y.b) return x.b < y.b;
    return x.i < y.i;
  }
  friend pt operator+(pt x, pt y) {
    return pt(x.m + y.m, x.b + y.b, x.i + y.i);
  }
};
const int INF = 1e9 + 7;
int n;
vector<vector<int> > v, z;
vector<pair<int, int> > p;
pt res;
void dej() {
  set<pt> dist;
  for (int i = 1; i < n; ++i) dist.insert(pt(INF, 0, i));
  dist.insert(pt(0, 0, 0));
  pt st = pt(0, 0, 0);
  vector<pair<int, int> > d(n);
  d[0] = make_pair(0, 0);
  for (int i = 1; i < n; ++i) d[i] = make_pair(INF, 0);
  p.resize(n);
  p[0] = make_pair(-1, 0);
  while (st.i != -1) {
    int k = st.i;
    for (int i = 0; i < v[k].size(); ++i) {
      int to = v[k][i];
      if ((st + pt(1, z[k][i], 0)) < pt(d[to].first, d[to].second, to)) {
        dist.erase(pt(d[to].first, d[to].second, to));
        pt nw = st + pt(1, z[k][i], 0);
        nw.i = to;
        dist.insert(nw);
        d[to].first = nw.m;
        d[to].second = nw.b;
        p[to] = make_pair(k, z[k][i]);
      }
    }
    dist.erase(st);
    if (dist.size() == 0) break;
    st = *dist.begin();
  }
  res = pt(d[n - 1].first, d[n - 1].second, n - 1);
}
int main() {
  int m;
  scanf("%d %d", &n, &m);
  v.resize(n);
  z.resize(n);
  int B = 0;
  vector<pair<pair<int, int>, int> > roads(m);
  for (int i = 0; i < m; ++i) {
    int x, y, zz;
    scanf("%d %d %d", &x, &y, &zz);
    --x;
    --y;
    zz = !zz;
    v[x].push_back(y);
    v[y].push_back(x);
    z[x].push_back(zz);
    z[y].push_back(zz);
    if (zz) ++B;
    roads[i].first = make_pair(x, y);
    roads[i].second = zz;
  }
  dej();
  printf("%d\n", res.b + m - B - (res.m - res.b));
  vector<pair<int, int> > inway;
  for (int i = n - 1; i > 0; i = p[i].first) {
    if (p[i].second == 1) {
      printf("%d %d 1\n", p[i].first + 1, i + 1);
    } else {
      inway.push_back(make_pair(p[i].first, i));
      inway.push_back(make_pair(i, p[i].first));
    }
  }
  sort(inway.begin(), inway.end());
  for (int i = 0; i < m; ++i)
    if (roads[i].second == 0 &&
        !binary_search(inway.begin(), inway.end(), roads[i].first)) {
      printf("%d %d 0\n", roads[i].first.first + 1, roads[i].first.second + 1);
    }
  return 0;
}
