#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 1000 * 100 + 100;
int n, m;
int edge[maxn][3];
int ans[maxn];
int val[maxn];
int val2[maxn];
bool mark[maxn];
vector<pair<int, pair<int, int> > > adj[maxn];
queue<int> Q;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edge[i][0] = a;
    edge[i][1] = b;
    val[a] += c;
    val2[a] += c;
    val[b] += c;
    val2[b] += c;
    adj[a].push_back(make_pair(b, make_pair(c, i)));
    adj[b].push_back(make_pair(a, make_pair(c, i)));
  }
  Q.push(0);
  while (!Q.empty()) {
    int v = Q.front();
    mark[v] = true;
    Q.pop();
    mark[v] = true;
    for (int i = 0; i < adj[v].size(); i++) {
      int Val = adj[v][i].second.first;
      int pos = adj[v][i].second.second;
      int u = adj[v][i].first;
      if (mark[u] == true) continue;
      ans[pos] = v;
      val[u] -= Val;
      if (u + 1 != n && val[u] == val2[u] / 2) {
        Q.push(u);
      }
    }
  }
  for (int i = 0; i < m; i++) {
    int p = ans[i];
    if (p == edge[i][0])
      cout << 0 << endl;
    else
      cout << 1 << endl;
  }
}
