#include <bits/stdc++.h>
long long mod = 1000000007;
using namespace std;
int n;
struct edge {
  int src, tar, flow, index, dir = -1;
};
vector<edge> G[2 * 100001];
struct node {
  int v;
};
int total[2 * 100001];
int inc[2 * 100001];
int ans[2 * 100001];
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
void bfs(int v) {
  queue<int> Q;
  Q.push(1);
  int t;
  while (!Q.empty()) {
    v = Q.front();
    Q.pop();
    for (int i = 0; i < G[v].size(); i++) {
      edge temp = G[v][i];
      if (ans[temp.index] != -1) {
        continue;
      }
      if (temp.src == v) {
        t = temp.tar;
        temp.dir = 0;
      } else {
        t = temp.src;
        temp.dir = 1;
      }
      ans[temp.index] = temp.dir;
      inc[t] += temp.flow;
      if (inc[t] * 2 == total[t]) {
        if (t != n) Q.push(t);
      }
    }
  }
}
bool cmp(edge x, edge y) { return x.index < y.index; }
int main() {
  int i, j, fl, test, x, y, c, m;
  scanf("%d%d", &n, &m);
  memset(ans, -1, sizeof(ans));
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &x, &y, &c);
    edge temp;
    temp.src = x;
    temp.tar = y;
    temp.flow = c;
    temp.index = i;
    G[x].push_back(temp);
    G[y].push_back(temp);
    total[x] += c;
    total[y] += c;
  }
  bfs(1);
  for (i = 0; i < m; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
