#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > a;
vector<int> ex, ey, sum, flag, w;
int visted[200000 + 5];
int near(int k, int x) { return ex[k] + ey[k] - x; }
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  sum.resize(n + 1, 0);
  a.resize(n, vector<int>());
  for (int i = 0, _x, _y, _c; i < m; i++) {
    scanf("%d %d %d", &_x, &_y, &_c);
    ex.push_back(_x - 1);
    ey.push_back(_y - 1);
    w.push_back(_c);
    a[_x - 1].push_back(i);
    a[_y - 1].push_back(i);
    flag.push_back(0);
    sum[_x - 1] += _c;
    sum[_y - 1] += _c;
  }
  visted[0] = 1;
  queue<int> tmp;
  tmp.push(0);
  for (int i = 0; i < m; i++) {
    if (ex[i] == 0)
      flag[i] = 0;
    else if (ey[i] == 0)
      flag[i] = 1;
  }
  while (!tmp.empty()) {
    int x = tmp.front();
    tmp.pop();
    for (int i = 0; i < a[x].size(); i++) {
      int _y = near(a[x][i], x);
      visted[_y] += w[a[x][i]];
      if (_y != 0) {
        if (_y == n - 1 || visted[_y] + visted[_y] <= sum[_y]) {
          flag[a[x][i]] = (_y != ey[a[x][i]]);
        }
      }
      if (_y > 0 && _y < n - 1 && visted[_y] * 2 == sum[_y]) {
        tmp.push(_y);
      }
    }
  }
  for (int i = 0; i < m; i++) printf("%d\n", flag[i]);
  return 0;
}
