#include <bits/stdc++.h>
using namespace std;
long long int n, m, ar[1000005], sum, tut[1000005];
map<pair<long long int, long long int>, long long int> mp, ans;
vector<pair<long long int, long long int> > v[1000005];
pair<long long int, long long int> p[1000005];
queue<int> que;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    tut[a] += c;
    tut[b] += c;
    v[a].push_back(make_pair(b, c));
    v[b].push_back(make_pair(a, c));
    p[i].first = a;
    p[i].second = b;
  }
  for (int i = 2; i <= n - 1; i++) tut[i] /= 2;
  que.push(1);
  while (!que.empty()) {
    long long int x = que.front();
    que.pop();
    for (int i = 0; i < v[x].size(); i++) {
      long long int a = v[x][i].first;
      long long int b = v[x][i].second;
      if (!mp[make_pair(x, a)]) {
        mp[make_pair(x, a)] = 1;
        mp[make_pair(a, x)] = 1;
        ans[make_pair(x, a)] = 0;
        ans[make_pair(a, x)] = 1;
        tut[a] -= b;
        if (tut[a] == 0) que.push(a);
      }
    }
  }
  for (int i = 1; i <= m; i++) cout << ans[p[i]] << "\n";
}
