#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 100 * 1000 + 100;
vector<pair<pair<int, int>, int> > edge[MAXN];
int in[MAXN], all[MAXN], ans[MAXN];
bool mark[MAXN];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    edge[a].push_back(make_pair(make_pair(b, c), i));
    edge[b].push_back(make_pair(make_pair(a, c), -i));
    all[a] += c;
    all[b] += c;
  }
  deque<int> q;
  mark[1] = true;
  q.push_back(1);
  while (q.size()) {
    int u = q.front();
    mark[u] = true;
    q.pop_front();
    for (int i = 0; i < edge[u].size(); i++) {
      int cnt = edge[u][i].second;
      int v = edge[u][i].first.first;
      int w = edge[u][i].first.second;
      if (!mark[v]) {
        in[v] += w;
        if (all[v] == 2 * in[v] and v != n) q.push_back(v);
        ans[abs(cnt)] = ((cnt < 0) ? 1 : 0);
      }
    }
  }
  for (int i = 1; i <= m; i++) cout << ans[i] << "\n";
  return 0;
}
