#include <bits/stdc++.h>
using namespace std;
int read() {
  bool f = 0;
  int x = 0;
  char c = getchar();
  while (c < '0' || '9' < c) {
    if (c == '-') f = 1;
    c = getchar();
  }
  while ('0' <= c && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return !f ? x : -x;
}
const int MAXN = 400000;
const int INF = 0x3f3f3f3f;
struct node {
  int l, r, x1, x2, x3, x4, ans;
  friend node operator+(node a, node b) {
    return (node){b.l + max(0, a.l - b.r),
                  a.r + max(0, b.r - a.l),
                  max(b.x1, max(b.l + a.x1 - b.r, b.l + a.x2 + b.r)),
                  max(b.x2, a.x2 + b.r - b.l),
                  max(a.x3, -a.r + a.l + b.x3),
                  max(a.x4, max(a.r + b.x4 - a.l, a.r + b.x3 + a.l)),
                  max(max(a.x1 + b.x3, a.x2 + b.x4), max(a.ans, b.ans))};
  }
} t[5 * MAXN + 5];
int id[MAXN + 5];
void PushUp(int u) {
  t[u] = t[(u << 1)] + t[(u << 1 | 1)];
  return;
}
void Build(int u, int L, int R) {
  if (L == R) {
    char c = getchar();
    int x = (c == '(' ? 1 : -1);
    t[u] = (node){(x == 1), (x == -1), 1, max(-x, 0), max(x, 0), 1, 1};
    id[L] = u;
    return;
  }
  int Mid = (L + R) >> 1;
  Build((u << 1), L, Mid), Build((u << 1 | 1), Mid + 1, R);
  PushUp(u);
  return;
}
void Modify(int x, int y) {
  swap(t[id[x]], t[id[y]]);
  x = id[x], y = id[y];
  while (x != 1) x >>= 1, PushUp(x);
  while (y != 1) y >>= 1, PushUp(y);
  return;
}
int main() {
  int n = read(), m = read();
  Build(1, 1, 2 * n - 2);
  printf("%d\n", t[1].ans);
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read();
    Modify(x, y);
    printf("%d\n", t[1].ans);
  }
  return 0;
}
