#include <bits/stdc++.h>
using namespace std;
vector<int> g[200000];
bool directed[200000];
bool dir[200000];
int u[200000];
int v[200000];
int f_n[200000];
int f_e[200000];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    g[i] = vector<int>();
    directed[i] = false;
    f_n[i] = 0;
  }
  for (int i = 0; i < m; i++) {
    int src, dest, flow;
    cin >> src >> dest >> flow;
    src--;
    dest--;
    directed[i] = false;
    u[i] = src;
    v[i] = dest;
    f_e[i] = flow;
    g[src].push_back(i);
    g[dest].push_back(i);
    f_n[src] += flow;
    f_n[dest] += flow;
  }
  for (int i = 1; i < n; i++) {
    f_n[i] /= 2;
  }
  queue<int> q;
  q.push(0);
  while (q.size() > 0) {
    int curr = q.front();
    q.pop();
    for (int i = 0; i < g[curr].size(); i++) {
      int e = g[curr][i];
      if (directed[e] == false) {
        if (u[e] == curr) {
          dir[e] = false;
          f_n[v[e]] -= f_e[e];
          if (v[e] != n - 1 && f_n[v[e]] == 0) {
            q.push(v[e]);
          }
        } else {
          dir[e] = true;
          f_n[u[e]] -= f_e[e];
          if (u[e] != n - 1 && f_n[u[e]] == 0) {
            q.push(u[e]);
          }
        }
        directed[e] = true;
      }
    }
  }
  for (int i = 0; i < m; i++) {
    cout << dir[i] << endl;
  }
  return 0;
}
