#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
inline void gn(int &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
inline void gn(long long &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
inline void gn(double &first) { scanf("%lf", &first); }
inline void newline() { putchar('\n'); }
inline void sp() { putchar(' '); }
template <class T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
pair<pair<int, int>, int> E[100010];
bool used[100010];
int dist[100010], dist1[100010], dp[2][100010];
int cnt[100010], pre[100010];
vector<int> ans1, ans0, qq[100010], adj[100010];
queue<int> q;
int main() {
  memset(pre, -1, sizeof(pre));
  int N, M, u, v, w, s;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    gn(u), gn(v), gn(w);
    adj[u].push_back(v);
    adj[v].push_back(u);
    E[i] = make_pair(make_pair(u, v), w);
  }
  memset(dist, 0x3f, sizeof(dist));
  memset(dist1, 0x3f, sizeof(dist1));
  dist[1] = 0;
  dist1[N] = 0;
  q = queue<int>();
  q.push(1);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int i = 0; i < adj[u].size(); i++) {
      v = adj[u][i];
      if (dist[v] < 0x3f3f3f3f) continue;
      dist[v] = dist[u] + 1;
      q.push(v);
    }
  }
  s = dist[N];
  q.push(N);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int i = 0; i < adj[u].size(); i++) {
      v = adj[u][i];
      if (dist1[v] < 0x3f3f3f3f) continue;
      dist1[v] = dist1[u] + 1;
      q.push(v);
    }
  }
  for (int i = 0; i < M; i++) {
    u = E[i].first.first, v = E[i].first.second, w = E[i].second;
    if (dist[u] > dist[v]) swap(u, v);
    if (dist[u] + dist1[v] + 1 != dist[N]) continue;
    qq[dist[u]].push_back(i);
    if (w) cnt[dist[u]]++;
  }
  int last, mn = 0x3f3f3f3f;
  for (int i = 0; i < qq[0].size(); i++) {
    int id = qq[0][i];
    u = E[id].first.first, v = E[id].first.second, w = E[id].second;
    if (dist[u] > dist[v]) swap(u, v);
    if (w)
      dp[0][i] = cnt[0] - 1;
    else
      dp[0][i] = cnt[0] + 1;
    if (dist[N] == 1) {
      if (dp[0][i] < mn) {
        last = id;
        mn = dp[0][i];
      }
    }
  }
  int ok = 0;
  for (int i = 1; i < dist[N]; i++) {
    ok ^= 1;
    for (int j = 0; j < qq[i].size(); j++) {
      dp[ok][j] = 0x3f3f3f3f;
      int id = qq[i][j];
      u = E[id].first.first, v = E[id].first.second, w = E[id].second;
      if (dist[u] > dist[v]) swap(u, v);
      for (int k = 0; k < qq[i - 1].size(); k++) {
        int id1 = qq[i - 1][k];
        int uu = E[id1].first.first, vv = E[id1].first.second;
        if (dist[uu] > dist[vv]) swap(uu, vv);
        if (vv != u) continue;
        if (w) {
          if (dp[ok][j] > dp[1 ^ ok][k] + cnt[i] - 1) {
            pre[id] = id1;
            dp[ok][j] = dp[1 ^ ok][k] + cnt[i] - 1;
          }
        } else {
          if (dp[ok][j] > dp[1 ^ ok][k] + cnt[i] + 1) {
            pre[id] = id1;
            dp[ok][j] = dp[1 ^ ok][k] + cnt[i] + 1;
          }
        }
      }
      if (i == dist[N] - 1) {
        if (dp[ok][j] < mn) {
          last = id;
          mn = dp[ok][j];
        }
      }
    }
  }
  while (true) {
    used[last] = 1;
    if (pre[last] < 0) break;
    last = pre[last];
  }
  int nn = 0;
  for (int i = 0; i < M; i++) {
    w = E[i].second;
    if (!used[i]) {
      if (!w) continue;
      ans0.push_back(i);
      nn++;
    } else {
      if (w) continue;
      ans1.push_back(i);
      nn++;
    }
  }
  printf("%d\n", nn);
  for (int i = 0; i < ans1.size(); i++) {
    u = ans1[i];
    printf("%d %d 1\n", E[u].first.first, E[u].first.second);
  }
  for (int i = 0; i < ans0.size(); i++) {
    u = ans0[i];
    printf("%d %d 0\n", E[u].first.first, E[u].first.second);
  }
  return 0;
}
