#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> e[100010];
vector<int> bad[100010];
int x;
int dist[100010], len[100010], pre[100010];
;
const int INF = 1000000;
priority_queue<pair<pair<int, int>, int> > q;
bool vis[100010];
set<pair<int, int> > used;
pair<pair<int, int>, int> all[100010];
int main() {
  cin >> n >> m;
  x = 0;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    c = 1 - c;
    e[a].push_back(b);
    e[b].push_back(a);
    bad[a].push_back(c);
    bad[b].push_back(c);
    if (c) x++;
    if (a < b) swap(a, b);
    all[i] = make_pair(make_pair(a, b), c);
  }
  q.push(make_pair(make_pair(0, -(m - x)), 0));
  for (int i = 0; i < n; i++) dist[i] = INF;
  dist[0] = 0;
  len[0] = m - x;
  vis[0] = 1;
  while (!q.empty()) {
    int p = q.top().second;
    int tmp1 = -q.top().first.first;
    int tmp2 = -q.top().first.second;
    q.pop();
    vis[p] = 0;
    if (dist[p] != tmp1 || len[p] != tmp2) continue;
    if (p == n - 1) break;
    for (int i = 0; i < e[p].size(); i++) {
      int y = e[p][i];
      if (dist[y] > dist[p] + 1) {
        dist[y] = dist[p] + 1;
        if (bad[p][i]) {
          len[y] = len[p] + 1;
        } else {
          len[y] = len[p] - 1;
        }
        pre[y] = p;
        if (!vis[y]) {
          q.push(make_pair(make_pair(-dist[y], -len[y]), y));
          vis[y] = 1;
        }
      } else if (dist[y] == dist[p] + 1) {
        int tmp;
        if (bad[p][i]) {
          tmp = len[p] + 1;
        } else {
          tmp = len[p] - 1;
        }
        if (len[y] > tmp) {
          len[y] = tmp;
          pre[y] = p;
          if (!vis[y]) {
            q.push(make_pair(make_pair(-dist[y], -len[y]), y));
            vis[y] = 1;
          }
        }
      }
    }
  }
  cout << len[n - 1] << endl;
  int p = n - 1;
  while (p) {
    int a = p;
    int b = pre[p];
    if (a < b) swap(a, b);
    used.insert(make_pair(a, b));
    p = pre[p];
  }
  for (int i = 0; i < m; i++) {
    if (all[i].second) {
      if (used.find(all[i].first) != used.end()) {
        cout << all[i].first.first + 1 << " " << all[i].first.second + 1 << " "
             << 1 << endl;
      }
    } else {
      if (used.find(all[i].first) == used.end()) {
        cout << all[i].first.first + 1 << " " << all[i].first.second + 1 << " "
             << 0 << endl;
      }
    }
  }
  return 0;
}
