#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
const int M = 1000000007;
const double eps = 1e-8;
const double pi = acos(-1.0);
int sum[maxn];
int ans[maxn];
int now[maxn];
struct edge {
  int e, id, inv, c;
};
vector<edge> E[maxn];
bool vis[maxn];
int main() {
  int n, m;
  while (~scanf("%d %d", &n, &m)) {
    for (int i = 1; i <= n; i++) E[i].clear();
    memset(sum, 0, sizeof(sum));
    for (int i = 1; i <= m; i++) {
      int s, e, c;
      scanf("%d %d %d", &s, &e, &c);
      sum[s] += c;
      sum[e] += c;
      edge temp;
      temp.c = c;
      temp.e = e;
      temp.id = i;
      temp.inv = 0;
      E[s].push_back(temp);
      temp.e = s;
      temp.id = i;
      temp.inv = 1;
      E[e].push_back(temp);
    }
    queue<int> Q;
    memset(vis, 0, sizeof(vis));
    memset(now, 0, sizeof(now));
    Q.push(1);
    while (!Q.empty()) {
      int x = Q.front();
      Q.pop();
      if (x == n) continue;
      for (int i = 0; i < E[x].size(); i++) {
        if (!vis[E[x][i].id]) {
          vis[E[x][i].id] = 1;
          now[E[x][i].e] += E[x][i].c;
          if (now[E[x][i].e] * 2 == sum[E[x][i].e]) Q.push(E[x][i].e);
          ans[E[x][i].id] = E[x][i].inv;
        }
      }
    }
    for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  }
}
