#include <bits/stdc++.h>
using namespace std;
const int C = 200001;
vector<vector<int> > gr(C), E(C), Ep(C);
int ij[C], a, b, n, m, F[C], sw[C], check[C], pre[C], ip = 1, v[C];
int main() {
  int i, iF, jF, j;
  long long w;
  scanf("%d %d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d %d %lld", &a, &b, &w);
    gr[a].push_back(b), gr[b].push_back(a);
    E[a].push_back(w), E[b].push_back(w);
    Ep[a].push_back(i), Ep[b].push_back(-1);
    sw[a] += w, sw[b] += w;
    ij[a]++, ij[b]++;
  }
  F[0] = 1, jF = 1, check[1] = 1;
  for (iF = 0; iF < jF; iF++) {
    a = F[iF];
    pre[a] = ip, ip++;
    for (j = 0; j < ij[a]; j++) {
      b = gr[a][j];
      if (check[b] == 0) {
        sw[b] -= 2 * E[a][j];
        if (sw[b] == 0 && b != n) check[b] = 1, F[jF] = b, jF++;
      }
    }
  }
  pre[n] = n;
  for (i = 1; i <= n; i++) {
    for (j = 0; j < ij[i]; j++) {
      if (Ep[i][j] == -1) continue;
      a = gr[i][j];
      if (pre[i] < pre[a])
        v[Ep[i][j]] = 0;
      else
        v[Ep[i][j]] = 1;
    }
  }
  for (i = 0; i < m; i++) printf("%d\n", v[i]);
  return 0;
}
