#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
priority_queue<pair<pair<pair<int, int>, int>, pair<int, pair<int, int> > > > Q;
vector<pair<int, int> > V[100005], Y[100005];
int Pre[100005], m, n, u, v, i, j, h, w, g, x, a, b, ata;
bool H[100005];
int main() {
  cin >> n >> w;
  while (w--) {
    cin >> u >> v >> h;
    g += h;
    V[u].push_back(make_pair(v, h));
    V[v].push_back(make_pair(u, h));
    Y[u].push_back(make_pair(v, h));
  }
  Q.push(
      make_pair(make_pair(make_pair(0, g), 0), make_pair(1, make_pair(0, 0))));
  while (!Q.empty()) {
    u = Q.top().first.first.first;
    x = Q.top().second.first;
    ata = Q.top().first.second;
    a = Q.top().second.second.first;
    b = Q.top().second.second.second;
    Q.pop();
    if (H[x]) continue;
    Pre[x] = ata;
    H[x] = 1;
    if (x == n) {
      cout << b + g - a << '\n';
      break;
    }
    for (i = 0; i < V[x].size(); i++)
      Q.push(make_pair(
          make_pair(make_pair(u - 1,
                              -b - (!V[x][i].second) + -g + a + V[x][i].second),
                    x),
          make_pair(V[x][i].first,
                    make_pair(a + V[x][i].second, b + (!V[x][i].second)))));
  }
  memset(H, 0, sizeof H);
  m = n;
  while (m) {
    H[m] = 1;
    m = Pre[m];
  }
  for (i = 1; i <= n; i++)
    for (j = 0; j < Y[i].size(); j++) {
      if (H[i] && H[Y[i][j].first]) {
        if (!Y[i][j].second) cout << i << " " << Y[i][j].first << " 1\n";
      } else {
        if (Y[i][j].second) cout << i << " " << Y[i][j].first << " 0\n";
      }
    }
  return 0;
}
