#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = ((res % p) * (x % p)) % p;
    y = y >> 1;
    x = ((x % p) * (x % p)) % p;
  }
  return res;
}
long long raichu(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = ((res) * (x));
    y = y >> 1;
    x = ((x) * (x));
  }
  return res;
}
bool isprime(long long n) {
  if (n < 2)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;
  else {
    long long z = sqrt(n);
    for (int i = 0; i < z - 1; i++)
      if (n % (i + 2) == 0) return false;
    return true;
  }
}
vector<pair<pair<long long, long long>, long long> > dijkstra(
    vector<vector<pair<long long, long long> > > &g, long long n,
    long long ok) {
  vector<pair<pair<long long, long long>, long long> > ans(n);
  for (int i = 0; i < n; i++) ans[i] = make_pair(make_pair(n, 1000000007LL), 0);
  ans[0] = make_pair(make_pair(0, ok), 0);
  priority_queue<pair<pair<long long, long long>, long long>,
                 vector<pair<pair<long long, long long>, long long> >,
                 greater<pair<pair<long long, long long>, long long> > >
      q;
  q.push(make_pair(make_pair(0, ok), 0));
  while (!q.empty()) {
    pair<pair<long long, long long>, long long> z = q.top();
    q.pop();
    if (z.first > ans[z.second].first)
      continue;
    else {
      for (int i = 0; i < (long long)g[z.second].size(); i++) {
        if (ans[z.second].first.first + 1 <
            ans[g[z.second][i].second].first.first) {
          ans[g[z.second][i].second].first.first =
              ans[z.second].first.first + 1;
          ans[g[z.second][i].second].first.second = ans[z.second].first.second;
          if (g[z.second][i].first == 0)
            ans[g[z.second][i].second].first.second -= 1;
          else
            ans[g[z.second][i].second].first.second += 1;
          ans[g[z.second][i].second].second = z.second;
          q.push(make_pair(make_pair(ans[g[z.second][i].second].first.first,
                                     ans[g[z.second][i].second].first.second),
                           g[z.second][i].second));
        } else if (ans[z.second].first.first + 1 ==
                   ans[g[z.second][i].second].first.first) {
          long long temp = ans[z.second].first.second;
          if (g[z.second][i].first == 0)
            temp -= 1;
          else
            temp += 1;
          if (temp < ans[g[z.second][i].second].first.second) {
            ans[g[z.second][i].second].first.second = temp;
            ans[g[z.second][i].second].second = z.second;
            q.push(make_pair(make_pair(ans[g[z.second][i].second].first.first,
                                       ans[g[z.second][i].second].first.second),
                             g[z.second][i].second));
          }
        }
      }
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m, broken = 0, ok = 0;
  cin >> n >> m;
  vector<vector<pair<long long, long long> > > g(n);
  for (int i = 0; i < m; i++) {
    long long x, y, z;
    cin >> x >> y >> z;
    z = z ^ 1;
    if (z == 0)
      ok++;
    else
      broken++;
    g[--x].push_back(make_pair(z, --y));
    g[y].push_back(make_pair(z, x));
  }
  vector<pair<pair<long long, long long>, long long> > ans = dijkstra(g, n, ok);
  cout << ans[n - 1].first.second << endl;
  long long c = n - 1;
  set<pair<long long, long long> > s;
  while (ans[c].second != c) {
    s.insert(make_pair(ans[c].second, c));
    c = ans[c].second;
  }
  set<pair<long long, long long> > S;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (long long)g[i].size(); j++) {
      if (S.find(make_pair(i, g[i][j].second)) != S.end() ||
          S.find(make_pair(g[i][j].second, i)) != S.end())
        continue;
      else {
        if (s.find(make_pair(i, g[i][j].second)) != s.end() ||
            s.find(make_pair(g[i][j].second, i)) != s.end()) {
          if (g[i][j].first == 1) {
            S.insert(make_pair(i, g[i][j].second));
            S.insert(make_pair(g[i][j].second, i));
            cout << i + 1 << " " << g[i][j].second + 1 << " " << 1 << endl;
          }
        } else {
          if (g[i][j].first == 0) {
            S.insert(make_pair(i, g[i][j].second));
            S.insert(make_pair(g[i][j].second, i));
            cout << i + 1 << " " << g[i][j].second + 1 << " " << 0 << endl;
          }
        }
      }
    }
  }
  return 0;
}
