#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1);
const int mod = 100000000;
long long max(long long a, long long b) { return a > b ? a : b; };
int min(int a, int b) { return a < b ? a : b; };
struct edge {
  int to, c, id, dir;
};
vector<edge> G[200005];
long long deg[200005];
int ans[200005], vis[200005];
int main() {
  int n, m, u, v, c;
  while (~scanf("%d %d", &n, &m)) {
    for (int i = 1; i <= n; i++) G[i].clear();
    memset(deg, 0, sizeof(deg));
    ;
    memset(vis, 0, sizeof(vis));
    ;
    for (int i = 1; i <= m; i++) {
      scanf("%d %d %d", &u, &v, &c);
      G[u].push_back((edge){v, c, i, 0});
      G[v].push_back((edge){u, c, i, 1});
      deg[u] += c;
      deg[v] += c;
    }
    queue<int> q;
    q.push(1);
    while (q.size()) {
      int u = q.front();
      q.pop();
      vis[u] = 1;
      for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i].to;
        int id = G[u][i].id;
        if (vis[v]) continue;
        if (v != n) deg[v] -= 2 * G[u][i].c;
        if (!deg[v]) q.push(v);
        ans[id] = G[u][i].dir;
      }
    }
    for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  }
  return 0;
}
