#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define fill(a, x) memset(a, x, sizeof(a))

struct Edge {
  int next, from, to, f, c; 
};

const int N = 100;
const int M = 5000 * 2;

int n, m, en, head[N];
Edge e[M];
int S, T, cc = 1, u[N];

void add( int a, int b, int x ) {
  e[en] = {head[a], a, b, 0, x};
  head[a] = en++; 
}

int dfs( int v, int X = (int)1e9 ) {
  if (v == T)     return X;
  if (u[v] == cc) return 0;
  u[v] = cc;
  for (int F, i = head[v]; i != -1; i = e[i].next)
    if (e[i].f < e[i].c && (F = dfs(e[i].to, min(X, e[i].c - e[i].f))) > 0) {
      e[i].f += F, e[i ^ 1].f -= F;
      return F;
    }
  return 0;
}

int main() {
  cin >> n >> m;
  fill(head, -1);
  forn(i, m) {
    int a, b, c;
    cin >> a >> b >> c, a--, b--;
    add(a, b, c); // 2i
    add(b, a, c); // 2i+1
    /** e --> e^1 */
  }
  S = 0, T = n - 1;
  int flow = 0, F;
  while ((F = dfs(S)) > 0)
    cc++, flow += F;
  printf("%d\n", flow);
  forn(i, m)
    printf("%d\n", e[2 * i].f);
}