#include <bits/stdc++.h>
using namespace std;
template <typename TH>
void _dbg(const char* sdbg, TH h) {
  cerr << sdbg << " = " << h << "\n";
}
template <typename TH, typename... TA>
void _dbg(const char* sdbg, TH h, TA... t) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << " = " << h << ",";
  _dbg(sdbg + 1, t...);
}
const int N = 1e5 + 5, M = 1e5 + 5, INF = 1e9;
struct Edge {
  int u, v, type;
  void Read() { cin >> u >> v >> type; }
  int Adj(int x) { return u ^ v ^ x; }
};
int n, m;
vector<int> g[N];
Edge edgeList[M];
int d[N], f[N], trace[N];
bool mark[N], markEdge[M];
void BFS() {
  queue<int> q;
  q.push(1);
  mark[1] = true;
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (int id : g[u]) {
      int v = edgeList[id].Adj(u);
      if (!mark[v]) {
        mark[v] = true;
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }
}
bool Chkmin(int& a, int b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int DP(int u) {
  if (mark[u]) {
    return f[u];
  }
  mark[u] = true;
  if (u == 1) {
    return f[u] = 0;
  }
  f[u] = INF;
  for (int id : g[u]) {
    int v = edgeList[id].Adj(u);
    if (d[v] + 1 == d[u]) {
      if (Chkmin(f[u], DP(v) + 1 - edgeList[id].type)) {
        trace[u] = id;
      }
    }
  }
  return f[u];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    edgeList[i].Read();
    g[edgeList[i].u].push_back(i);
    g[edgeList[i].v].push_back(i);
  }
  BFS();
  for (int i = 1; i <= n; ++i) {
    mark[i] = false;
  }
  DP(n);
  int u = n;
  vector<int> vec;
  while (true) {
    int id = trace[u];
    if (id == 0) {
      break;
    }
    markEdge[id] = true;
    if (edgeList[id].type == 0) {
      vec.push_back(id);
    }
    u = edgeList[id].Adj(u);
  }
  for (int i = 1; i <= m; ++i) {
    if (!markEdge[i] && edgeList[i].type == 1) {
      vec.push_back(i);
    }
  }
  cout << vec.size() << '\n';
  for (int id : vec) {
    cout << edgeList[id].u << ' ' << edgeList[id].v << ' '
         << 1 - edgeList[id].type << '\n';
  }
  return 0;
}
