#include <bits/stdc++.h>
using namespace std;
long double PI = 3.14159265359;
long double DEL = 1e-10;
int mod = 1000000007;
long long fpow(long long x, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void sieve(long long n) {
  bool prime[1000006];
  memset(prime, true, sizeof(prime));
  long long rootn = (long long)sqrt(n);
  for (long long p = 2; p <= rootn; p++)
    if (prime[p] == true)
      for (long long i = p * p; i <= n; i += p) prime[i] = false;
  prime[1] = 0;
}
const long long N = 300050;
long long cnt, sum, mid, mx, mn, ans[N], a[N], b[N];
long long n, m, d, i, j, k, l, p, q, r, t, w, x, y, z;
bool f, f1, f2;
string s;
vector<pair<pair<long long, long long>, pair<long long, long long>>> g[N];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  vector<long long> acc(n + 2);
  for (long long i = 1; i <= m; i++) {
    cin >> x >> y >> w;
    g[x].push_back({{y, w}, {i, 1}});
    g[y].push_back({{x, w}, {i, 0}});
    acc[x] += w;
    acc[y] += w;
  }
  for (long long i = 1; i <= n; i++) {
    acc[i] /= 2;
    sort(g[i].begin(), g[i].end());
  }
  vector<bool> vis(n + 3);
  queue<long long> q;
  vis[1] = 1;
  q.push(1);
  while (!q.empty()) {
    for (auto j : g[q.front()]) {
      if (!vis[j.first.first]) {
        acc[j.first.first] -= j.first.second;
        if (j.second.second == 0) {
          ans[j.second.first] = 1;
        }
        if (acc[j.first.first] == 0 and j.first.first != n) {
          vis[j.first.first] = 1;
          q.push(j.first.first);
        }
      }
    }
    q.pop();
  }
  for (long long i = 1; i <= m; i++) {
    cout << ans[i];
    cout << "\n";
  }
}
