#include <bits/stdc++.h>
using namespace std;
vector<int> neigh[200009];
int ans[200009];
int a[200009];
int b[200009];
int c[200009];
int f[200009];
int Q[200009];
int L, R;
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    f[a[i]] += c[i];
    f[b[i]] += c[i];
    neigh[a[i]].push_back(i);
    neigh[b[i]].push_back(i);
  }
  f[1] = f[n] = 0;
  for (int i = 2; i <= n - 1; i++) f[i] /= 2;
  Q[R++] = 1;
  while (L < R) {
    int cur = Q[L++];
    for (int i = 0; i < neigh[cur].size(); i++) {
      int u;
      if (cur == a[neigh[cur][i]])
        u = b[neigh[cur][i]];
      else
        u = a[neigh[cur][i]];
      if (u != n && f[u] == 0) continue;
      if (u == a[neigh[cur][i]]) ans[neigh[cur][i]] = 1;
      f[u] -= c[neigh[cur][i]];
      if (u != n && f[u] == 0) Q[R++] = u;
    }
  }
  for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
}
