#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
int n, m;
struct info {
  int to, flow, id, dir;
};
const int N = 200005;
vector<info> g[N];
int Flow[N], Dir[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w, i, 0});
    g[v].push_back({u, w, i, 1});
    Flow[u] += w;
    Flow[v] += w;
  }
  for (int i = 1; i <= n; i++) Flow[i] /= 2;
  memset(Dir, -1, sizeof(Dir));
  queue<int> Q;
  Q.push(1);
  while (!Q.empty()) {
    int node = Q.front();
    Q.pop();
    for (int i = 0; i < g[node].size(); i++) {
      int id = g[node][i].id;
      int go = g[node][i].to;
      if (Dir[id] == -1) {
        Dir[id] = g[node][i].dir;
        Flow[go] -= g[node][i].flow;
        if (Flow[go] == 0 and go != n) {
          Q.push(go);
        }
      }
    }
  }
  for (int i = 1; i <= m; i++) cout << Dir[i] << "\n";
}
