#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
struct edge {
  int vertex, direction, index, flow;
  edge(int v, int d, int i, int f) {
    vertex = v;
    direction = d;
    index = i;
    flow = f;
  }
};
int n, m, u, v, direction[MAXN], flow[MAXN];
vector<vector<edge> > g;
int main() {
  scanf("%d %d", &n, &m);
  g = vector<vector<edge> >(n);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    a--;
    b--;
    g[a].push_back(edge(b, 0, i, c));
    g[b].push_back(edge(a, 1, i, c));
    flow[a] = flow[a] + c;
    flow[b] = flow[b] + c;
  }
  for (int i = 0; i < n; i++) {
    flow[i] /= 2;
  }
  memset(direction, -1, sizeof(direction));
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    for (size_t i = 0; i < g[u].size(); i++) {
      int id = g[u][i].index;
      if (direction[id] == -1) {
        direction[id] = g[u][i].direction;
        v = g[u][i].vertex;
        flow[v] -= g[u][i].flow;
        if (v != n - 1 && flow[v] == 0) {
          Q.push(v);
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    printf("%d\n", direction[i]);
  }
}
