#include <bits/stdc++.h>
using namespace std;
int good_ones = 0;
struct Graph {
  struct Edge;
  struct Vertex {
    bool visited;
    Edge *edge_to_par;
    int val;
    int no;
    int dist;
    Edge *edges;
    Vertex() : visited(false), edges(NULL), edge_to_par(NULL), dist(0) {}
  };
  struct Edge {
    Vertex *v;
    Edge *next;
    int z;
    int start;
    int status;
    int pair_ind;
    Edge() : next(NULL), status(0) {}
  };
  static const int MAX_N = 400000;
  static const int MAX_M = 400000;
  int M, N;
  Vertex V[MAX_N];
  Edge E[MAX_M];
  Graph() : N(0), M(0) {}
  void add_edge(int x, int y, int z, int pi) {
    E[M].v = &V[y];
    E[M].next = V[x].edges;
    E[M].z = z;
    E[M].start = x;
    E[M].pair_ind = pi;
    V[x].edges = &E[M++];
  }
  void read() {
    int final_M;
    scanf("%d%d", &N, &final_M);
    for (int i = 0; i < final_M; i++) {
      int x, y, z;
      scanf("%d%d%d", &x, &y, &z);
      x--;
      y--;
      if (z) good_ones++;
      add_edge(x, y, z, M + 1);
      add_edge(y, x, z, M - 1);
    }
  }
};
Graph G;
queue<int> Q;
int main() {
  G.read();
  for (int i = 0; i < G.N; i++) G.V[i].no = i;
  G.V[0].visited = true;
  Q.push(0);
  while (1) {
    int v = Q.front();
    Q.pop();
    if (v == G.N - 1) break;
    Graph::Edge *e = G.V[v].edges;
    while (e != NULL) {
      if (!e->v->visited) {
        Q.push(e->v->no);
        e->v->visited = true;
        e->v->dist = G.V[v].dist + 1;
        e->v->val = G.V[v].val + (1 - 2 * e->z);
        e->v->edge_to_par = e;
      } else if (e->v->dist == G.V[v].dist + 1 &&
                 e->v->val > G.V[v].val + (1 - 2 * e->z)) {
        e->v->val = G.V[v].val + (1 - 2 * e->z);
        e->v->edge_to_par = e;
      }
      e = e->next;
    }
  }
  int curr_v = G.N - 1;
  while (curr_v != 0) {
    G.V[curr_v].edge_to_par->status = 1;
    G.E[G.V[curr_v].edge_to_par->pair_ind].status = 2;
    curr_v = G.V[curr_v].edge_to_par->start;
  }
  printf("%d\n", good_ones + G.V[G.N - 1].val);
  for (int i = 0; i < G.M; i++) {
    if ((G.E[i].status == 0 && G.E[i].z == 1) ||
        (G.E[i].status == 1 && G.E[i].z == 0)) {
      if (G.E[i].status == 0) G.E[G.E[i].pair_ind].status = 2;
      printf("%d %d %d\n", G.E[i].start + 1, G.E[i].v->no + 1, !G.E[i].z);
    }
  }
  return 0;
}
