#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int from, to, val, t;
} E[300005];
int n, m, ze, ans[1200005];
bool cmp(Edge a, Edge b) { return a.val < b.val; }
vector<int> id[1200005];
int z[1200005], zz, f[1200005], odd, sze[1200005];
int find(int x) { return f[x] == x ? x : find(f[x]); }
void Merge(int eid) {
  int u = find(E[eid].from), v = find(E[eid].to);
  if (u == v) return;
  if (sze[u] > sze[v]) swap(u, v);
  if ((sze[u] & 1) && (sze[v] & 1)) odd -= 2;
  f[u] = v;
  z[++zz] = u;
  sze[v] += sze[u];
}
int ql, qr, qv;
void add(int p, int l, int r) {
  if (ql <= l && r <= qr) {
    id[p].push_back(qv);
    return;
  }
  int mid = l + r >> 1;
  if (ql <= mid) add(p << 1, l, mid);
  if (qr > mid) add(p << 1 | 1, mid + 1, r);
}
void solve(int p, int l, int r) {
  int hc = zz;
  for (int i = 0; i < id[p].size(); ++i) Merge(id[p][i]);
  if (l == r) {
    while (ze != m && odd) {
      ++ze;
      if (E[ze].t <= l) {
        Merge(ze);
        if (E[ze].t < l) {
          ql = E[ze].t, qr = l - 1, qv = ze;
          add(1, 1, m);
        }
      }
    }
    if (odd)
      ans[l] = -1;
    else
      ans[l] = E[ze].val;
  } else {
    int mid = l + r >> 1;
    solve(p << 1 | 1, mid + 1, r);
    solve(p << 1, l, mid);
  }
  int x;
  while (hc != zz) {
    x = z[zz--];
    sze[f[x]] -= sze[x];
    if ((sze[f[x]] & 1) && (sze[x] & 1)) odd += 2;
    f[x] = x;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  odd = n;
  for (int i = 1; i <= n; ++i) f[i] = i, sze[i] = 1;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &E[i].from, &E[i].to, &E[i].val);
    E[i].t = i;
  }
  sort(E + 1, E + 1 + m, cmp);
  solve(1, 1, m);
  for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
  return 0;
}
