#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 10;
const int maxm = 1 << 20;
int tn, n, Q;
int rp[maxm], rm[maxm], lp[maxm], lm[maxm], aa[maxm], bb[maxm], ans[maxm];
char str[maxn];
void pushup(int curPos) {
  int ls = curPos << 1, rs = curPos << 1 | 1;
  aa[curPos] = aa[ls] + max(0, aa[rs] - bb[ls]);
  bb[curPos] = bb[rs] + max(0, bb[ls] - aa[rs]);
  rp[curPos] =
      max(rp[rs], max(rp[ls] - aa[rs] + bb[rs], rm[ls] + aa[rs] + bb[rs]));
  rm[curPos] = max(rm[rs], rm[ls] + aa[rs] - bb[rs]);
  lp[curPos] =
      max(lp[ls], max(lp[rs] - bb[ls] + aa[ls], lm[rs] + aa[ls] + bb[ls]));
  lm[curPos] = max(lm[ls], lm[rs] - aa[ls] + bb[ls]);
  ans[curPos] =
      max(max(ans[ls], ans[rs]), max(rm[ls] + lp[rs], rp[ls] + lm[rs]));
}
void getLeaf(int curPos, char ch) {
  if (ch == '(') {
    aa[curPos] = 0, bb[curPos] = 1;
    rp[curPos] = 0, rm[curPos] = 0;
    lp[curPos] = 0, lm[curPos] = 0;
  } else {
    aa[curPos] = 1, bb[curPos] = 0;
    rp[curPos] = 0, rm[curPos] = 0;
    lp[curPos] = 0, lm[curPos] = 0;
  }
  ans[curPos] = 0;
}
void update(int pos, char ch, int curL = 0, int curR = tn, int curPos = 1) {
  if (curL == curR) {
    getLeaf(curPos, ch);
    return;
  }
  int mid = (curL + curR) >> 1;
  if (pos <= mid)
    update(pos, ch, curL, mid, curPos << 1);
  else
    update(pos, ch, mid + 1, curR, curPos << 1 | 1);
  pushup(curPos);
}
void build(int curL = 0, int curR = tn, int curPos = 1) {
  if (curL == curR) {
    getLeaf(curPos, str[curL]);
    return;
  }
  int mid = (curL + curR) >> 1;
  build(curL, mid, curPos << 1);
  build(mid + 1, curR, curPos << 1 | 1);
  pushup(curPos);
}
int main() {
  scanf("%d%d", &n, &Q);
  scanf("%s", str + 1);
  tn = n * 2 - 1;
  str[0] = '(', str[tn] = ')';
  build();
  printf("%d\n", ans[1]);
  while (Q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (str[a] == str[b]) {
      printf("%d\n", ans[1]);
      continue;
    }
    update(a, str[b]);
    update(b, str[a]);
    printf("%d\n", ans[1]);
    swap(str[a], str[b]);
  }
  return 0;
}
