#include <bits/stdc++.h>
using namespace std;
int sum[200010];
struct arc {
  int source;
  int dest;
  int flow;
  int dir;
  int nxt;
  int id;
};
arc s[400020];
int endlist = 0;
int beg[200010];
int ans[200010];
void addArc(int source, int dest, int flow) {
  s[endlist].source = source;
  s[endlist].dest = dest;
  s[endlist].flow = flow;
  s[endlist].id = 8;
  s[endlist].nxt = beg[source];
  beg[source] = endlist++;
  s[endlist].source = dest;
  s[endlist].dest = source;
  s[endlist].flow = flow;
  s[endlist].nxt = beg[dest];
  beg[dest] = endlist++;
}
bool used[200010];
queue<int> q;
int sumBeg[200010];
int main() {
  memset(beg, -1, sizeof beg);
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    addArc(a, b, c);
    sum[a] += c;
    sum[b] += c;
    sumBeg[a] += c;
    sumBeg[b] += c;
  }
  q.push(1);
  used[1] = true;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int nxt = beg[cur]; nxt != -1; nxt = s[nxt].nxt) {
      int dest = s[nxt].dest;
      if (!used[dest]) {
        sum[dest] -= s[nxt].flow;
        if (sum[dest] == sumBeg[dest] / 2 && dest != n) {
          used[dest] = true;
          q.push(dest);
        }
        if ((nxt & 1) == 1) {
          ans[nxt / 2] = 1;
        } else {
          ans[nxt / 2] = 0;
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
