#include <bits/stdc++.h>
using namespace std;
template <typename tp>
inline void read(tp &x) {
  x = 0;
  char c = getchar();
  int f = 0;
  for (; c < '0' || c > '9'; f |= c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar())
    ;
  if (f) x = -x;
}
const int N = 1e6 + 233, M = N;
struct E {
  int x, y, w, id;
} e[M], r[M];
int n, m, ans[M], pos[M], rnk[M];
inline bool cmp_w(E a, E b) { return a.w < b.w; }
namespace dsu {
int n, odd;
int pre[N], sz[N];
int tot, p[N], q[N];
vector<int> stk;
inline void init(int _n) {
  n = _n;
  for (int i = 1; i <= (n); i++) pre[i] = i, sz[i] = 1;
  odd = n;
}
inline int find(int x) { return pre[x] == x ? x : find(pre[x]); }
inline void make(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  if (sz[x] < sz[y]) swap(x, y);
  odd -= ((sz[x]) & 1);
  odd -= ((sz[y]) & 1);
  sz[x] += sz[y];
  pre[y] = x;
  odd += ((sz[x]) & 1);
  ++tot;
  p[tot] = x;
  q[tot] = y;
}
inline void gao(int i) { make(e[i].x, e[i].y); }
inline void prepare() { stk.push_back(tot); }
inline void back() {
  assert(!stk.empty());
  int t = stk.back();
  stk.pop_back();
  while (tot > t) {
    int x = p[tot], y = q[tot];
    --tot;
    odd -= ((sz[x]) & 1);
    sz[x] -= sz[y];
    pre[y] = y;
    odd += ((sz[x]) & 1);
    odd += ((sz[y]) & 1);
  }
}
}  // namespace dsu
inline void divide(int l, int r, int low, int high) {
  if (l > r) return;
  int c = (l + r) / 2, &ansc = ans[c] = -1;
  dsu::prepare();
  for (int i = l; i <= c; i++)
    if (rnk[i] < low) dsu::gao(i);
  for (int i = low; i <= high; i++) {
    if (pos[i] <= c) dsu::gao(pos[i]);
    if (dsu::odd == 0) {
      ansc = i;
      break;
    }
  }
  dsu::back();
  if (ansc == -1) {
    dsu::prepare();
    for (int i = l; i <= c; i++)
      if (rnk[i] < low) dsu::gao(i);
    divide(c + 1, r, low, high);
    dsu::back();
  } else {
    dsu::prepare();
    for (int i = low; i <= ansc; i++)
      if (pos[i] < l) dsu::gao(pos[i]);
    divide(l, c - 1, ansc, high);
    dsu::back();
    dsu::prepare();
    for (int i = l; i <= c; i++)
      if (rnk[i] < low) dsu::gao(i);
    divide(c + 1, r, low, ansc);
    dsu::back();
  }
}
int main(void) {
  memset(ans, -1, sizeof ans);
  read(n);
  read(m);
  dsu::init(n);
  for (int i = 1; i <= (m); i++) {
    int x, y, w;
    read(x);
    read(y);
    read(w);
    r[i] = e[i] = (E){x, y, w, i};
  }
  sort(r + 1, r + m + 1, cmp_w);
  for (int i = 1; i <= (m); i++) pos[i] = r[i].id, rnk[r[i].id] = i;
  divide(1, m, 1, m);
  for (int c = 1; c <= (m); c++)
    cout << (ans[c] < 0 ? -1 : r[ans[c]].w) << "\n";
}
