#include <bits/stdc++.h>
using namespace std;
namespace IO {
const int MAXIOSIZE = 1 << 24 | 1;
unsigned char buf[MAXIOSIZE], *p1, *p2;
template <typename T>
void read(T& x) {
  x = 0;
  char ch =
      (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2)
           ? EOF
           : *p1++);
  bool flg = false;
  for (; ch < '0' || '9' < ch;
       ch = (p1 == p2 &&
                     (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2)
                 ? EOF
                 : *p1++))
    if (ch == '-') flg |= true;
  for (; '0' <= ch && ch <= '9';
       ch = (p1 == p2 &&
                     (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2)
                 ? EOF
                 : *p1++))
    x = x * 10 + ch - '0';
  flg ? x = -x : 0;
}
template <typename T>
void out(const T& x) {
  if (x > 9) out(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
inline void write(const T& x, const char& ed = ' ') {
  if (x < 0)
    putchar('-'), out(-x);
  else
    out(x);
  putchar(ed);
}
}  // namespace IO
const int MAXN = 2e5 + 10;
int n, m, tot[MAXN], vis[MAXN], order[MAXN], order_clock;
vector<pair<int, int> > E[MAXN];
vector<pair<int, int> > tmp;
int main() {
  IO::read(n), IO::read(m);
  for (int i = (1), iend = (m); i <= iend; ++i) {
    int u, v, w;
    IO::read(u), IO::read(v), IO::read(w);
    tot[u] += w, tot[v] += w;
    E[u].push_back(make_pair(v, w));
    E[v].push_back(make_pair(u, w));
    tmp.push_back(make_pair(u, v));
  }
  tot[1] = 0;
  for (int i = (2), iend = (n - 1); i <= iend; ++i) tot[i] >>= 1;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    order[u] = ++order_clock;
    for (pair<int, int> vv : E[u]) {
      tot[vv.first] -= vv.second;
      if (tot[vv.first] == 0 && !vis[vv.first])
        q.push(vv.first), vis[vv.first] = true;
    }
  }
  for (pair<int, int> cur : tmp)
    IO::write(order[cur.first] > order[cur.second], '\n');
  return 0;
}
