#include <bits/stdc++.h>
using namespace std;
const int maxn = 222222 << 1;
struct node {
  int b, f, nxt;
  node(int b = 0, int f = 0, int nxt = 0) : b(b), f(f), nxt(nxt) {}
  bool operator<(const node& p) const { return f > p.f; }
} edge[maxn];
int head[maxn], e;
int n, m;
int du[maxn];
bool vis[maxn];
int has[maxn];
int ans[maxn];
void init() {
  e = 0;
  memset(head, -1, sizeof(head));
  memset(du, 0, sizeof(du));
  memset(vis, 0, sizeof(vis));
  memset(has, 0, sizeof(has));
  fill(ans, ans + 2 * m + 1, 1);
}
void addEdge(int a, int b, int f) {
  edge[e] = node(b, f, head[a]);
  head[a] = e++;
}
int main() {
  while (cin >> n >> m) {
    init();
    int a, b, f;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> f;
      addEdge(a, b, f);
      addEdge(b, a, f);
      du[a] += f;
      du[b] += f;
    }
    queue<int> q;
    q.push(1);
    vis[1] = 1;
    while (q.size()) {
      int t = q.front();
      q.pop();
      for (int i = head[t]; ~i; i = edge[i].nxt) {
        b = edge[i].b, f = edge[i].f;
        if (!vis[b]) {
          ans[i] = 0;
          has[b] += f;
          if (b != n && has[b] * 2 == du[b]) {
            vis[b] = 1;
            q.push(b);
          }
        }
      }
    }
    for (int i = 0; i < m; i++) cout << ans[(i << 1)] << endl;
  }
  return 0;
}
