#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
using pii = tuple<lli, lli>;
using piii = tuple<lli, lli, lli>;
using vi = vector<lli>;
using vii = vector<pii>;
using viii = vector<piii>;
using vvi = vector<vi>;
using vvii = vector<vii>;
using vviii = vector<viii>;
using vb = vector<bool>;
using vvb = vector<vb>;
const lli inf = 1000 * 1000 * 1000;
struct Mon {
  lli delta, a_c, ab, abc, b, bc;
  Mon(lli x) {
    delta = x;
    a_c = x;
    b = -2 * x;
    ab = abc = bc = -inf;
  }
  Mon() : Mon(0){};
};
Mon operator+(const Mon& x, Mon y) {
  Mon res;
  y.delta += x.delta;
  y.a_c += x.delta;
  y.ab -= x.delta;
  y.abc += 0;
  y.b -= 2 * x.delta;
  y.bc -= x.delta;
  res.delta = y.delta;
  res.a_c = max(x.a_c, y.a_c);
  res.ab = max(x.a_c + y.b, max(x.ab, y.ab));
  res.b = max(x.b, y.b);
  res.bc = max(x.b + y.a_c, max(x.bc, y.bc));
  res.abc = max(max(x.a_c + y.bc, x.ab + y.a_c), max(x.abc, y.abc));
  return res;
}
template <class T>
struct SegTree {
  lli n;
  vector<T> t;
  SegTree(const vector<T>& v) : n(v.size()), t(2 * n, T()) {
    copy(v.begin(), v.end(), t.begin() + n);
    for (lli i = n - 1; i > 0; --i) t[i] = t[2 * i] + t[2 * i + 1];
  }
  void modify(lli p, T value) {
    for (t[p += n] = value; p /= 2;) t[p] = t[2 * p] + t[2 * p + 1];
  }
  void inc(lli p, T value) {
    p += n;
    for (t[p] = t[p] + value; p /= 2;) t[p] = t[2 * p] + t[2 * p + 1];
  }
  T query(lli l, lli r) {
    T resl = T(), resr = T();
    for (l += n, r += n; l < r; l /= 2, r /= 2) {
      if (l & 1) resl = resl + t[l++];
      if (r & 1) resr = t[--r] + resr;
    }
    return resl + resr;
  }
};
char buf[200 * 1000 + 42];
int main() {
  lli n, q;
  scanf("%lld%lld", &n, &q);
  scanf(" %s", buf);
  lli N = 2 * (n - 1);
  vector<Mon> vmon(N);
  for (lli i = 0; i < (lli)(N); ++i) {
    vmon[i] = Mon(buf[i] == '(' ? 1 : -1);
  }
  SegTree<Mon> st(vmon);
  printf("%lld\n", st.query(0, N).abc);
  for (lli _ = 0; _ < (lli)(q); ++_) {
    lli i, j;
    scanf("%lld%lld", &i, &j);
    --i;
    --j;
    swap(vmon[i], vmon[j]);
    st.modify(i, vmon[i]);
    st.modify(j, vmon[j]);
    printf("%lld\n", st.query(0, N).abc);
  }
  return 0;
}
