#include <bits/stdc++.h>
using namespace std;
const int Maxn = 200 * 1000 + 5;
int n, m, num[Maxn], ans[Maxn];
vector<int> adj[Maxn], ind[Maxn];
set<pair<int, int> > mys;
bool mark[Maxn];
struct edge {
  int u, v, c;
  edge(int uu = 0, int vv = 0, int cc = 0) {
    u = uu;
    v = vv;
    c = cc;
  }
} e[Maxn];
int main() {
  scanf("%d%d", &n, &m);
  int u, v, cc;
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &u, &v, &cc);
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
    ind[v].push_back(i);
    ind[u].push_back(i);
    num[u] += cc;
    num[v] += cc;
    e[i] = edge(u, v, cc);
  }
  num[0] = 0;
  for (int i = 1; i < n - 1; i++) num[i] /= 2;
  for (int i = 0; i < n; i++) mys.insert(make_pair(num[i], i));
  for (int i = 0; i < n; i++) {
    v = mys.begin()->second;
    mys.erase(mys.begin());
    for (int j = 0; j < (int)adj[v].size(); j++) {
      if (!mark[ind[v][j]]) {
        mys.erase(make_pair(num[adj[v][j]], adj[v][j]));
        num[adj[v][j]] -= e[ind[v][j]].c;
        mys.insert(make_pair(num[adj[v][j]], adj[v][j]));
        if (e[ind[v][j]].v == v) ans[ind[v][j]] = 1;
        mark[ind[v][j]] = true;
      }
    }
  }
  for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
  return 0;
}
