#include <bits/stdc++.h>
using namespace std;
struct Link {
  int v, id, orient, w;
  Link() {}
  Link(int _id, int _v, int _w, int _orient) {
    id = _id;
    v = _v;
    w = _w;
    orient = _orient;
  }
};
int n, m;
vector<Link> g[200005];
int sum[200005];
bool used[200005];
vector<int> ord;
int ans[200005];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    g[a].push_back(Link(i, b, c, 0));
    g[b].push_back(Link(i, a, c, 1));
    sum[a] += c;
    sum[b] += c;
  }
  for (int i = 1; i <= n; i++) sum[i] /= 2;
  ord.push_back(1);
  used[1] = true;
  for (int i = 0; i < ord.size(); i++) {
    int v = ord[i];
    for (int j = 0; j < g[v].size(); j++) {
      Link to = g[v][j];
      if (used[to.v]) continue;
      ans[to.id] = to.orient;
      sum[to.v] -= to.w;
      if (sum[to.v] == 0 && to.v != n) {
        used[to.v] = true;
        ord.push_back(to.v);
      }
    }
  }
  for (int i = 0; i < m; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
