#include <bits/stdc++.h>
using namespace std;
int C[200001], flow_in[200001];
bool visited[200001];
struct two {
  int x, y, z;
};
int main() {
  int n, m, k, p[200001], q[200001];
  bool ans[200001];
  vector<two> A[200001];
  two temp;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> p[i] >> q[i] >> k;
    temp.x = q[i];
    temp.y = k;
    temp.z = i;
    A[p[i]].push_back(temp);
    temp.x = p[i];
    A[q[i]].push_back(temp);
    C[p[i]] += k;
    C[q[i]] += k;
  }
  for (int i = 0; i <= n; i++) C[i] /= 2;
  queue<int> flow;
  flow.push(1);
  visited[1] = true;
  while (!flow.empty()) {
    int temp1 = flow.front();
    flow.pop();
    for (int i = 0; i < A[temp1].size(); i++) {
      if (visited[A[temp1][i].x] == false) {
        flow_in[A[temp1][i].x] += A[temp1][i].y;
        if (p[A[temp1][i].z] == temp1)
          ans[A[temp1][i].z] = true;
        else
          ans[A[temp1][i].z] = false;
        if (flow_in[A[temp1][i].x] == C[A[temp1][i].x] && A[temp1][i].x != n) {
          flow.push(A[temp1][i].x);
          visited[A[temp1][i].x] = true;
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    if (ans[i] == true)
      cout << 0 << endl;
    else
      cout << 1 << endl;
  }
}
