#include <bits/stdc++.h>
using namespace std;
map<set<int>, int> init_state, final_state;
vector<pair<int, int> > adjlist[100005];
vector<int> parent;
int n, m;
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first == b.first.first) return a.first.second > b.first.second;
  return a.first.first < b.first.first;
}
class mycomparison {
 public:
  bool operator()(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
    if (a.first.first == b.first.first) return a.first.second < b.first.second;
    return a.first.first > b.first.first;
  }
};
void dijkstra(int s) {
  parent.assign(n, -1);
  vector<bool> vis;
  vis.assign(n, 0);
  vector<int> dist;
  dist.assign(n, 100000000);
  vector<int> rep;
  rep.assign(n, 0);
  dist[s] = rep[s] = 0;
  priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int> >,
                 mycomparison>
      pq;
  pq.push(make_pair(pair<int, int>(0, 0), s));
  while (!pq.empty()) {
    int u = pq.top().second;
    pq.pop();
    if (vis[u]) continue;
    vis[u] = true;
    for (int i = 0; i < (int)adjlist[u].size(); ++i) {
      pair<int, int> p = adjlist[u][i];
      if (dist[p.first] > dist[u] + 1 ||
          (dist[p.first] == dist[u] + 1 && rep[p.first] < rep[u] + p.second)) {
        rep[p.first] = rep[u] + p.second;
        dist[p.first] = dist[u] + 1;
        parent[p.first] = u;
        pq.push(
            make_pair(pair<int, int>(dist[p.first], rep[p.first]), p.first));
      }
    }
  }
}
vector<int> gen_path(int s, int d) {
  vector<int> path;
  while (d != s) {
    path.push_back(d);
    d = parent[d];
    if (d == -1) {
      vector<int> v;
      return v;
    }
  }
  path.push_back(s);
  reverse(path.begin(), path.end());
  return path;
}
int main() {
  int u, v, z;
  cin >> n >> m;
  while (m--) {
    cin >> u >> v >> z;
    --u;
    --v;
    adjlist[u].push_back(pair<int, int>(v, z));
    adjlist[v].push_back(pair<int, int>(u, z));
    set<int> s;
    s.insert(u);
    s.insert(v);
    init_state[s] = z;
    final_state[s] = 0;
  }
  dijkstra(0);
  vector<int> path = gen_path(0, n - 1);
  for (int i = 1; i < (int)path.size(); ++i) {
    set<int> s;
    s.insert(path[i]);
    s.insert(path[i - 1]);
    if (init_state[s] == 0) {
      final_state[s] = 1;
    } else {
      final_state[s] = -1;
    }
  }
  vector<pair<pair<int, int>, int> > out;
  for (map<set<int>, int>::iterator it = final_state.begin();
       it != final_state.end(); ++it) {
    if (!(init_state[it->first] == 1 && final_state[it->first] == -1) &&
        !(init_state[it->first] == 0 && final_state[it->first] == 0)) {
      out.push_back(make_pair(
          pair<int, int>(*(it->first.begin()), *(++(it->first.begin()))),
          it->second));
    }
  }
  cout << out.size() << endl;
  for (int i = 0; i < (int)out.size(); ++i)
    cout << out[i].first.first + 1 << " " << out[i].first.second + 1 << " "
         << out[i].second << endl;
  return 0;
}
