#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int n, m, ans;
struct node {
  node *l, *r, *p, *m;
  int s, os, v, i, id;
  node() {}
  node(int v, int s, int id)
      : l(), r(), p(), m(this), i(), v(v), os(s), s(s), id(id) {}
} * nd[100005], *ev[300005];
bool is_root(node *x) { return x->p == NULL || x->p->l != x && x->p->r != x; }
node *max(node *x) { return x ? x->m : NULL; }
node *max(node *x, node *y) {
  if (x == NULL) return y;
  if (y == NULL) return x;
  return x->v > y->v ? x : y;
}
bool left(node *x) { return x->p->l == x; }
int sum(node *x) { return x ? x->s : 0; }
void push_up(node *x) {
  x->m = max(max(max(x->l), max(x->r)), x);
  x->s = sum(x->l) + sum(x->r) + x->os;
  return;
}
void push_down(node *x) {
  if (x->i) {
    swap(x->r, x->l);
    if (x->r) x->r->i ^= 1;
    if (x->l) x->l->i ^= 1;
    x->i = 0;
  }
  return;
}
void down(node *x) {
  if (!is_root(x)) down(x->p);
  push_down(x);
}
void connect(node *x, node *y, bool l) {
  (l ? x->l : x->r) = y;
  if (y) y->p = x;
  return;
}
void rot(node *x) {
  node *y = x->p, *z = y->p;
  bool l = left(x);
  connect(y, l ? x->r : x->l, l);
  if (!is_root(y))
    connect(y->p, x, left(y));
  else
    x->p = z;
  connect(x, y, !l);
  push_up(y);
  return;
}
void splay(node *x) {
  down(x);
  while (!is_root(x)) {
    node *p = x->p;
    if (!is_root(p)) rot(left(x) ^ left(p) ? x : p);
    rot(x);
  }
  push_up(x);
  return;
}
void expose(node *x) {
  node *y = NULL;
  for (node *z = x; z; z = z->p) {
    splay(z);
    z->os += sum(z->r);
    z->os -= sum(y);
    z->r = y;
    y = z;
    push_up(y);
  }
  splay(x);
  return;
}
bool con(node *a, node *b) {
  expose(a);
  expose(b);
  return a->p != NULL;
}
void evert(node *x) {
  expose(x);
  x->i ^= 1;
  return;
}
int size(node *x) {
  expose(x);
  return sum(x);
}
void cut(node *x, node *y) {
  evert(x);
  expose(y);
  y->l->p = NULL;
  y->l = NULL;
}
void link(node *x, node *y) {
  evert(x);
  expose(y);
  x->p = y;
  y->os += sum(x);
  return;
}
node *max_path(node *x, node *y) {
  evert(x);
  expose(y);
  return y->m;
}
struct edge {
  int a, b, c, id;
  edge(int a, int b, int c, int id) : a(a), b(b), c(c), id(id) {}
  bool operator<(const edge &a) const { return c != a.c ? c > a.c : id > a.id; }
};
int O;
set<edge> s;
vector<edge> ed;
bool delete_edge(edge x) {
  cut(nd[x.a], ev[x.id]);
  cut(nd[x.b], ev[x.id]);
  if (size(nd[x.a]) & 1 && size(nd[x.b]) & 1) O += 2;
  return !(size(nd[x.a]) & 1);
}
void add_edge(edge x) {
  if (size(nd[x.a]) & 1 && size(nd[x.b]) & 1) O -= 2;
  link(nd[x.a], ev[x.id]);
  link(nd[x.b], ev[x.id]);
  return;
}
void new_edge(int a, int b, int c) {
  if (c >= ans) return;
  int id = ed.size();
  ev[id] = new node(c, 0, id);
  edge e = edge(a, b, c, id);
  ed.push_back(e);
  if (con(nd[a], nd[b])) {
    node *m = max_path(nd[a], nd[b]);
    if (m->v <= c) return;
    delete_edge(ed[m->id]);
    s.erase(ed[m->id]);
  }
  add_edge(e);
  s.insert(e);
  return;
}
int query() {
  if (O) return -1;
  while (delete_edge(*s.begin())) {
    s.erase(s.begin());
  }
  add_edge(*s.begin());
  return s.begin()->c;
}
int main() {
  scanf("%d %d", &n, &m);
  int x, y, z;
  ans = inf;
  O = n;
  for (int i = 1; i <= n; i++) nd[i] = new node(0, 1, 0);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", &x, &y, &z);
    new_edge(x, y, z);
    printf("%d\n", ans = query());
    if (ans < 0) ans = inf;
  }
  return 0;
}
