#include <bits/stdc++.h>
using namespace std;
const int MX = 200005;
struct Edge {
  int to, c, id;
};
int N, M, ans[MX], sum[MX], done[MX];
pair<int, int> edge[MX];
vector<Edge> E[MX];
inline void set_edge(int u, int v, int i) {
  assert((edge[i].first == u and edge[i].second == v) or
         (edge[i].first == v and edge[i].second == u));
  if (edge[i].first == u)
    ans[i] = 0;
  else
    ans[i] = 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  cin >> N >> M;
  for (int i = (1); i <= (M); i++) {
    int a, b, c;
    cin >> a >> b >> c;
    E[a].push_back({b, c, i});
    E[b].push_back({a, c, i});
    edge[i] = {a, b};
    sum[a] += c;
    sum[b] += c;
  }
  for (int i = (1); i <= (N); i++) sum[i] /= 2;
  queue<int> que;
  que.push(1);
  done[1] = 1;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (auto it : E[u]) {
      int v = it.to, c = it.c, id = it.id;
      if (done[v]) continue;
      set_edge(u, v, id);
      sum[v] -= c;
      if (v != 1 and v != N and sum[v] == 0) {
        que.push(v);
        done[v] = 1;
      }
    }
  }
  for (int i = (1); i <= (M); i++) cout << ans[i] << "\n";
  return 0;
}
