#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int MAXM = 300005;
template <typename T>
inline void read(T &AKNOI) {
  T x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  AKNOI = flag * x;
}
int n, m, ans[MAXM];
int odd, f[MAXN], siz[MAXN], stk[MAXM], top;
struct Edge {
  int u, v, w, id;
  bool operator<(const Edge &rhs) const {
    return (w == rhs.w) ? (id < rhs.id) : (w < rhs.w);
  }
} p[MAXM], q[MAXM];
inline int getf(int x) {
  while (f[x] ^ x) x = f[x];
  return x;
}
inline void Merge(int u, int v) {
  u = getf(u), v = getf(v);
  if (u == v) return;
  if (siz[u] < siz[v]) swap(u, v);
  odd -= (siz[u] & 1) + (siz[v] & 1);
  siz[u] += siz[v];
  odd += (siz[u] & 1);
  f[v] = u;
  stk[++top] = v;
}
inline void Split(int v) {
  int u = f[v];
  odd -= (siz[u] & 1);
  siz[u] -= siz[v];
  odd += (siz[u] & 1) + (siz[v] & 1);
  f[v] = v;
}
inline void Cancel(int to) {
  while (top > to) {
    Split(stk[top--]);
  }
}
void Binary(int b, int e, int l, int r) {
  int cur = top;
  int mid = (b + e) >> 1, midp = -1;
  for (int i = b; i <= mid; ++i) {
    if (q[i].w < l) {
      Merge(q[i].u, q[i].v);
    }
  }
  for (int i = l; i <= r; ++i) {
    if (p[i].id <= mid) {
      Merge(p[i].u, p[i].v);
    }
    if (!odd) {
      midp = i;
      break;
    }
  }
  Cancel(cur);
  ans[q[mid].id] = (midp == -1 ? -1 : p[midp].w);
  if (ans[mid] == -1) {
    for (int i = b; i < mid; ++i) {
      ans[i] = -1;
    }
  } else if (b < mid) {
    for (int i = l; i < midp; ++i) {
      if (p[i].id < b) {
        Merge(p[i].u, p[i].v);
      }
    }
    Binary(b, mid - 1, midp, r);
    Cancel(cur);
  }
  if (e > mid) {
    for (int i = b; i <= mid; ++i) {
      if (q[i].w < l) {
        Merge(q[i].u, q[i].v);
      }
    }
    Binary(mid + 1, e, l, (midp == -1 ? r : midp));
    Cancel(cur);
  }
}
void init() {
  read(n);
  read(m);
  for (int i = 1; i <= m; ++i) {
    read(p[i].u);
    read(p[i].v);
    read(p[i].w);
    p[i].id = i;
    q[i] = p[i];
  }
  sort(p + 1, p + m + 1);
  for (int i = 1; i <= m; ++i) {
    q[p[i].id].w = i;
  }
}
void solve() {
  odd = n;
  for (int i = 1; i <= n; ++i) {
    f[i] = i;
    siz[i] = 1;
  }
  Binary(1, m, 1, m);
  for (int i = 1; i <= m; ++i) {
    printf("%d\n", ans[i]);
  }
}
int main() {
  init();
  solve();
  return 0;
}
