#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:33554432")
using namespace std;
struct edge {
  int from, to, c, dir;
  edge(int from, int to, int c, int dir) {
    this->from = from;
    this->to = to;
    this->c = c;
    this->dir = dir;
  }
  edge() {}
};
int n, m;
vector<int> G[1 << 18];
edge E[1 << 19];
set<pair<int, int> > S;
int used[1 << 18];
int s[1 << 18];
int res[1 << 18];
int main() {
  scanf("%d%d", &n, &m);
  memset(s, 0, sizeof(s));
  for (int i = 0; i < m; ++i) {
    int from, to, c;
    scanf("%d%d%d", &from, &to, &c);
    from--, to--;
    E[2 * i] = edge(from, to, c, 0);
    E[2 * i + 1] = edge(to, from, c, 1);
    G[from].push_back(2 * i);
    G[to].push_back(2 * i + 1);
    s[from] += c;
    s[to] += c;
  }
  s[0] = 0;
  memset(used, 0, sizeof(used));
  for (int i = 0; i < n - 1; ++i) S.insert(make_pair(s[i], i));
  while (!S.empty()) {
    pair<int, int> x = *S.begin();
    int v = x.second;
    S.erase(S.begin());
    used[v] = 1;
    for (int i = 0; i < G[v].size(); ++i) {
      int to = E[G[v][i]].to;
      int c = E[G[v][i]].c;
      if (used[to]) continue;
      if (to != n - 1) {
        pair<int, int> t(s[to], to);
        S.erase(t);
        s[to] -= 2 * c;
        S.insert(make_pair(s[to], to));
      }
      res[G[v][i] / 2] = E[G[v][i]].dir;
    }
  }
  for (int i = 0; i < m; ++i) printf("%d\n", res[i]);
  return 0;
}
