#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);
struct edge {
  int v, w, i, d;
  edge(int v, int w, int i, int d) : v(v), w(w), i(i), d(d) {}
};
int n, m, d[200200], first[200200];
vector<edge> g[200200];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for (int i = 0, u, v, w; i < m; ++i) {
    cin >> u >> v >> w;
    u--, v--;
    g[u].push_back(edge(v, w, i, 0));
    g[v].push_back(edge(u, w, i, 1));
    first[u] += w;
    first[v] += w;
  }
  for (int i = 0; i < n; ++i) {
    first[i] /= 2;
  }
  memset(d, -1, sizeof d);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); ++i) {
      int id = g[v][i].i;
      if (d[id] == -1) {
        d[id] = g[v][i].d;
        int to = g[v][i].v;
        first[to] -= g[v][i].w;
        if (to != n - 1 && first[to] == 0) {
          q.push(to);
        }
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    cout << d[i] << "\n";
  }
}
