#include <bits/stdc++.h>
using namespace std;
struct abc {
  int x, y, z, u;
} c[200010];
vector<int> e[200010];
int d[200010];
int l[200010], w, r;
int main() {
  int n, m, i, x;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d%d", &c[i].x, &c[i].y, &c[i].z);
    c[i].u = -1;
    e[c[i].x].push_back(c[i].y);
    e[c[i].x].push_back(c[i].z);
    e[c[i].x].push_back(i);
    e[c[i].y].push_back(c[i].x);
    e[c[i].y].push_back(c[i].z);
    e[c[i].y].push_back(i);
    d[c[i].x] += c[i].z;
    d[c[i].y] += c[i].z;
  }
  for (i = 2; i < n; i++) d[i] /= 2;
  d[n] = ((1 << 30) - 1) * 2;
  l[w++] = 1;
  while (r < w) {
    x = l[r];
    for (i = 0; i < (int)e[x].size(); i += 3) {
      int y = e[x][i], z = e[x][i + 1], id = e[x][i + 2];
      if (c[id].u < 0) {
        if (c[id].x == x)
          c[id].u = 0;
        else
          c[id].u = 1;
        d[y] -= z;
        if (d[y] == 0) {
          l[w++] = y;
        }
      }
    }
    r++;
  }
  for (i = 1; i <= m; i++) printf("%d\n", c[i].u);
  return 0;
}
