#include <bits/stdc++.h>
using namespace std;
int N, M;
int E1[200002], E2[200002], F[200002];
vector<pair<int, int> > V[200002];
set<pair<int, int> > S;
int bal[200002];
bool is[200002];
queue<int> Q;
int main() {
  cin >> N >> M;
  for (int i = 1; i <= M; ++i) {
    cin >> E1[i] >> E2[i] >> F[i];
    V[E1[i]].push_back(make_pair(E2[i], F[i]));
    V[E2[i]].push_back(make_pair(E1[i], F[i]));
    bal[E1[i]] -= F[i];
    bal[E2[i]] -= F[i];
  }
  Q.push(1);
  is[1] = true;
  bal[1] = 0;
  while (!Q.empty()) {
    int now = Q.front();
    Q.pop();
    for (vector<pair<int, int> >::iterator it = V[now].begin();
         it != V[now].end(); ++it)
      if (!is[it->first]) {
        S.insert(make_pair(now, it->first));
        if (it->first != N) {
          bal[it->first] += 2 * it->second;
          if (bal[it->first] == 0) {
            Q.push(it->first);
            is[it->first] = true;
          }
        }
      }
  }
  for (int i = 1; i <= M; ++i)
    if (S.find(make_pair(E1[i], E2[i])) != S.end())
      cout << 0 << '\n';
    else
      cout << 1 << '\n';
}
