#include <bits/stdc++.h>
using namespace std;
int N, Q;
const int MAXT = 524288;
int BASE;
char ptree[200001];
int depth[MAXT], ma[MAXT], mb[MAXT], mab[MAXT], mbc[MAXT], ans[MAXT];
void summary(int x) {
  int l = x * 2;
  int r = l + 1;
  int &dl = depth[l];
  depth[x] = depth[l] + depth[r];
  ma[x] = max(ma[l], dl + ma[r]);
  mb[x] = min(mb[l], dl + mb[r]);
  mab[x] = max(mab[l], mab[r] - dl);
  mab[x] = max(mab[x], ma[l] - 2 * (mb[r] + dl));
  mbc[x] = max(mbc[l], mbc[r] - dl);
  mbc[x] = max(mbc[x], -2 * mb[l] + (ma[r] + dl));
  ans[x] = max(ans[l], ans[r]);
  ans[x] = max(ans[x], max(mab[l] + ma[r] + dl, ma[l] + mbc[r] - dl));
}
int main() {
  scanf("%d%d", &N, &Q);
  for (BASE = 1; BASE < 2 * N - 2; BASE *= 2)
    ;
  scanf("%s", ptree);
  for (int i = 0; i < BASE; i++) {
    if (i < 2 * N - 2) {
      depth[BASE + i] = ptree[i] == '(' ? 1 : -1;
    } else {
      depth[BASE + i] = 0;
    }
    ma[BASE + i] = mb[BASE + i] = depth[BASE + i];
    mab[BASE + i] = mbc[BASE + i] = ans[BASE + i] = 0;
  }
  for (int i = BASE - 1; i >= 1; i--) {
    summary(i);
  }
  printf("%d\n", ans[1]);
  for (int q = 0; q < Q; q++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    swap(ptree[u], ptree[v]);
    depth[BASE + u] = ptree[u] == '(' ? 1 : -1;
    depth[BASE + v] = ptree[v] == '(' ? 1 : -1;
    u += BASE;
    v += BASE;
    ma[u] = mb[u] = depth[u];
    ma[v] = mb[v] = depth[v];
    u /= 2;
    v /= 2;
    while (u) {
      summary(u);
      u /= 2;
    }
    while (v) {
      summary(v);
      v /= 2;
    }
    printf("%d\n", ans[1]);
  }
  return 0;
}
