#include <bits/stdc++.h>
using namespace std;
struct edge {
  edge(){};
  edge(int aa, int ww, int iidd, bool bb) : a(aa), w(ww), id(iidd), b(bb){};
  int a, w, id;
  bool b;
};
int n, m, val[200010], ans[200010];
vector<edge> adj[200010];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int x, y, w;
    scanf("%d %d %d", &x, &y, &w);
    --x, --y;
    adj[x].emplace_back(y, w, i, false);
    adj[y].emplace_back(x, w, i, true);
    val[x] += w;
    val[y] += w;
  }
  for_each(val, val + n, [](int& x) { x /= 2; });
  fill_n(ans, m, -1);
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int x = que.front();
    que.pop();
    for (int i = 0; i < (int)adj[x].size(); ++i) {
      if (ans[adj[x][i].id] == -1) {
        ans[adj[x][i].id] = adj[x][i].b;
        val[adj[x][i].a] -= adj[x][i].w;
        if (adj[x][i].a != n - 1 && !val[adj[x][i].a]) que.push(adj[x][i].a);
      }
    }
  }
  for_each(ans, ans + m, [](int x) { printf("%d\n", x); });
}
