#include <bits/stdc++.h>
using namespace std;
struct flow {
  int v, c, i;
};
const int maxN = 200000 + 10;
vector<flow> a[maxN];
int ans[maxN], all[maxN];
int n, m, qu[maxN], top, bot;
int main() {
  scanf("%d%d", &n, &m);
  int u, v, c;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &u, &v, &c);
    a[u].push_back((flow){v, c, i});
    a[v].push_back((flow){u, c, -i});
    all[u] += c;
    all[v] += c;
  }
  all[1] = 0;
  for (int i = 2; i <= n; i++) all[i] /= 2;
  top = bot = qu[1] = 1;
  memset(ans, -1, sizeof ans);
  while (bot <= top) {
    int u = qu[bot++];
    for (typeof(a[u].begin()) it = a[u].begin(); it != a[u].end(); it++)
      if (ans[abs(it->i)] == -1) {
        if (it->i > 0)
          ans[abs(it->i)] = 0;
        else
          ans[abs(it->i)] = 1;
        all[it->v] -= it->c;
        if (all[it->v] == 0 && it->v != n) qu[++top] = it->v;
      }
  }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
