#include <bits/stdc++.h>
using namespace std;
const int INF_MAX = 0x7FFFFFFF;
const int INF_MIN = -(1 << 30);
const double eps = 1e-10;
const double pi = acos(-1.0);
int toInt(string s) {
  istringstream sin(s);
  int t;
  sin >> t;
  return t;
}
template <class T>
string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T>
inline T gcd(T a, T b) {
  if (a < 0) return gcd(-a, b);
  if (b < 0) return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / gcd(a, b));
}
template <class T>
inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q) {
  while (!Q.empty()) Q.pop();
}
inline int random(int l, int r) { return rand() % (r - l + 1) + l; }
int dir_4[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int dir_8[8][2] = {{0, 1},  {-1, 1}, {-1, 0}, {-1, -1},
                   {0, -1}, {1, -1}, {1, 0},  {1, 1}};
map<pair<int, int>, int> mp;
set<pair<int, int> > s;
vector<pair<int, int> > edge[200005];
struct Flow {
  int a, b, c, ans;
} flow[200005];
int f[200005];
queue<int> q;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    flow[i].a = a;
    flow[i].b = b;
    flow[i].c = c;
    edge[a].push_back(make_pair(b, c));
    edge[b].push_back(make_pair(a, c));
    f[a] += c;
    f[b] += c;
  }
  for (int i = 2; i <= n - 1; i++) f[i] >>= 1;
  q.push(1);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    int v2;
    for (int i = 0; i < edge[v].size(); i++) {
      v2 = edge[v][i].first;
      int weight = edge[v][i].second;
      if (s.find(make_pair(v, v2)) == s.end() &&
          s.find(make_pair(v2, v)) == s.end()) {
        f[v2] -= weight;
        s.insert(make_pair(v, v2));
        mp[make_pair(v, v2)] = 1;
      } else
        continue;
      if (f[v2] == 0 && v2 != n) {
        q.push(v2);
      }
    }
  }
  for (int i = 0; i < m; i++) {
    if (mp.find(make_pair(flow[i].a, flow[i].b)) != mp.end())
      cout << 0 << endl;
    else
      cout << 1 << endl;
  }
  return 0;
}
