#include <bits/stdc++.h>
using namespace std;
const int M = 3e5 + 10, N = 1e5 + 10;
const int mod = 1e9 + 7;
int n, m;
struct op {
  int a, b, l, id;
  friend bool operator<(op a, op b) { return a.l < b.l; }
} q[M], p[M];
struct us {
  struct backup {
    int x, y, del;
  } st[N];
  int stn;
  int fa[N], sz[N], cnt_odd;
  inline void set(int n) {
    cnt_odd = n;
    stn = 0;
    for (int i = 1; i <= n; ++i) fa[i] = i, sz[i] = 1;
  }
  inline int getf(int x) { return fa[x] == x ? x : getf(fa[x]); }
  inline void un(int x, int y) {
    x = getf(x), y = getf(y);
    if (x == y) return;
    if (sz[x] < sz[y]) swap(x, y);
    ++stn;
    if ((sz[x] & 1) && (sz[y] & 1))
      cnt_odd -= 2, st[stn].del = 2;
    else
      st[stn].del = 0;
    st[stn].x = x, st[stn].y = y;
    sz[x] += sz[y];
    fa[y] = x;
  }
  inline void re() {
    backup s = st[stn--];
    cnt_odd += s.del;
    sz[s.x] -= sz[s.y];
    fa[s.y] = s.y;
  }
  inline bool check() { return cnt_odd == 0; }
} S;
int ans[M];
inline void solve(int l, int r, int lo, int hi) {
  if (l > r) return;
  int mid = l + r >> 1, ans_mid = -1, lst = S.stn;
  for (int i = l; i <= mid; ++i)
    if (q[i].id < lo) S.un(q[i].a, q[i].b);
  for (int i = lo; i <= hi; ++i) {
    if (p[i].id <= mid) S.un(p[i].a, p[i].b);
    if (S.check()) {
      ans_mid = i;
      break;
    }
  }
  while (S.stn > lst) S.re();
  if (ans_mid == -1) {
    for (int i = l; i <= mid; ++i) ans[i] = -1;
    for (int i = l; i <= mid; ++i)
      if (q[i].id < lo) S.un(q[i].a, q[i].b);
    solve(mid + 1, r, lo, hi);
    while (S.stn > lst) S.re();
    return;
  }
  ans[mid] = p[ans_mid].l;
  for (int i = l; i <= mid; ++i)
    if (q[i].id < lo) S.un(q[i].a, q[i].b);
  solve(mid + 1, r, lo, ans_mid);
  while (S.stn > lst) S.re();
  for (int i = lo; i < ans_mid; ++i)
    if (p[i].id < l) S.un(p[i].a, p[i].b);
  solve(l, mid - 1, ans_mid, hi);
  while (S.stn > lst) S.re();
}
int main() {
  cin >> n >> m;
  S.set(n);
  if (n & 1) {
    while (m--) puts("-1");
    return 0;
  }
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &q[i].a, &q[i].b, &q[i].l);
    p[i] = q[i];
    p[i].id = i;
  }
  sort(p + 1, p + m + 1);
  for (int i = 1; i <= m; ++i) q[p[i].id].id = i;
  solve(1, m, 1, m);
  for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
  return 0;
}
