#include <bits/stdc++.h>
using namespace std;
int n, m, tot, mn = 1000 * 1000 * 100;
vector<int> p(100005, 0);
vector<int> mul_on(100005, 1000 * 1000 * 1000);
vector<int> d(100005, 0);
vector<int> fun[100005];
vector<int> mul[100005];
set<pair<int, int> > fu;
void solve(int f) {
  queue<pair<int, pair<int, int> > > q;
  q.push({1, {0, 0}});
  p.assign(100005, 0);
  d.assign(100005, 0);
  mul_on.assign(100005, 1000 * 1000 * 1000);
  p[1] = 1;
  mul_on[1] = -1;
  while (!q.empty()) {
    int cur = q.front().first;
    int mal = q.front().second.second;
    int dep = q.front().second.first;
    q.pop();
    if (cur == n && mal + tot - (dep - mal) < mn && !f) {
      mn = mal + tot - (dep - mal);
    } else if (cur == n && mal + tot - (dep - mal) == mn && f)
      return;
    for (auto v : fun[cur])
      if (!p[v] || (mal < mul_on[v] && d[cur] + 1 <= d[v])) {
        p[v] = cur;
        d[v] = d[cur] + 1;
        mul_on[v] = mal;
        q.push({v, {dep + 1, mal}});
      }
    for (auto v : mul[cur])
      if (!p[v] || (mal + 1 < mul_on[v] && d[cur] + 1 <= d[v])) {
        p[v] = cur;
        d[v] = d[cur] + 1;
        mul_on[v] = mal + 1;
        q.push({v, {dep + 1, mal + 1}});
      }
  }
  return;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0, a, b, c; i < m; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    if (c)
      fun[a].push_back(b), fun[b].push_back(a),
          fu.insert({min(a, b), max(a, b)});
    else
      mul[a].push_back(b), mul[b].push_back(a);
    tot += c;
  }
  solve(0);
  solve(1);
  printf("%d\n", mn);
  for (int i = n; p[i] != i; i = p[i]) {
    if (fu.find({min(p[i], i), max(p[i], i)}) == fu.end()) {
      printf("%d %d 1\n", i, p[i]);
    } else
      fu.erase({min(p[i], i), max(p[i], i)});
  }
  for (auto it : fu) printf("%d %d 0\n", it.first, it.second);
}
