#include <bits/stdc++.h>
using namespace std;
const int N = 300300;
int n, m, u, v, w, cr, tp, sum;
int fd[N], sz[N], ans[N];
struct edg {
  int u, v, w, i;
  edg(int _u = 0, int _v = 0, int _w = 0, int _i = 0) {
    u = _u, v = _v, w = _w, i = _i;
  }
  bool operator<(const edg& a) const { return w < a.w; }
} e[N];
vector<edg> ve[N << 2];
edg st[N];
int fi(int x) { return x == fd[x] ? x : fi(fd[x]); }
void ins(int u, int v) {
  u = fi(u);
  v = fi(v);
  if (u == v) return;
  if (sz[u] < sz[v]) swap(u, v);
  if (sz[u] & sz[v] & 1) sum -= 2;
  fd[v] = u;
  sz[u] += sz[v];
  st[++tp] = edg(u, v);
}
void del(int u, int v) {
  fd[v] = v;
  sz[u] -= sz[v];
  if (sz[u] & sz[v] & 1) sum += 2;
}
void add(int u, int l, int r, int x, int y, edg e) {
  if (x <= l && r <= y) return ve[u].push_back(e);
  if (y < l || r < x) return;
  int md = (l + r) >> 1;
  add(u << 1, l, md, x, y, e);
  add(u << 1 | 1, md + 1, r, x, y, e);
}
void wrk(int u, int l, int r) {
  int bg = tp;
  for (edg f : ve[u]) ins(f.u, f.v);
  if (l == r) {
    for (; cr <= m && sum; cr++)
      if (e[cr].i <= l) {
        ins(e[cr].u, e[cr].v);
        if (e[cr].i < l) add(1, 1, m, e[cr].i, l - 1, e[cr]);
      }
    ans[l] = sum ? -1 : e[cr - 1].w;
    for (; tp > bg; tp--) del(st[tp].u, st[tp].v);
    return;
  }
  int md = (l + r) >> 1;
  wrk(u << 1 | 1, md + 1, r);
  wrk(u << 1, l, md);
  for (; tp > bg; tp--) del(st[tp].u, st[tp].v);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &u, &v, &w);
    e[i] = edg(u, v, w, i);
  }
  sort(e + 1, e + m + 1);
  cr = 1;
  sum = n;
  for (int i = 1; i <= n; i++) sz[i] = 1, fd[i] = i;
  wrk(1, 1, m);
  for (int i = 1; i <= m; i++) cout << ans[i] << '\n';
}
