#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e8 + 5e4 + 1;
long long int mod2 = 1e9 + 87;
const double error = 1e-8;
const double PI = acos(-1);
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
inline long long int MOD(long long int x, long long int m = mod) {
  long long int y = x % m;
  return (y >= 0) ? y : y + m;
}
const int inf = 0x3f3f3f3f;
const unsigned long long int infl = 1e18 + 1;
const int nmax = 2e5 + 5;
struct node {
  int lmr, lm, mr, m, a, lazy;
  void add() {
    lm -= lazy;
    mr -= lazy;
    m -= lazy + lazy;
    a += lazy;
    lazy = 0;
  }
  void mrge(node &L, node &R) {
    lmr = max({L.lmr, R.lmr, L.lm + R.a, L.a + R.mr});
    lm = max({L.lm, R.lm, L.a + R.m});
    mr = max({L.mr, R.mr, L.m + R.a});
    m = max(L.m, R.m);
    a = max(L.a, R.a);
  }
};
char input[nmax];
int level[nmax];
node tree[4 * nmax];
void build(int id, int l, int r) {
  if (l == r) {
    tree[id] = {0, -level[l], -level[l], -2 * level[l], level[l], 0};
    return;
  }
  int mid = (l + r) >> 1;
  build((id << 1), l, mid);
  build((id << 1) | 1, mid + 1, r);
  tree[id].mrge(tree[(id << 1)], tree[(id << 1) | 1]);
}
inline void propagate(int id, int l, int r) {
  if (l != r) {
    tree[(id << 1)].lazy += tree[id].lazy;
    tree[(id << 1) | 1].lazy += tree[id].lazy;
  }
  tree[id].add();
}
void update(int id, int l, int r, int L, int R, int v) {
  propagate(id, l, r);
  if (r < L || R < l) return;
  if (L <= l && r <= R) {
    tree[id].lazy += v;
    propagate(id, l, r);
    return;
  }
  int mid = (l + r) >> 1;
  update((id << 1), l, mid, L, R, v);
  update((id << 1) | 1, mid + 1, r, L, R, v);
  tree[id].mrge(tree[(id << 1)], tree[(id << 1) | 1]);
  return;
}
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  scanf("%s", input);
  for (int i = 1; i <= 2 * (n - 1); i++)
    level[i] = level[i - 1] + ((input[i - 1] == '(') ? 1 : -1);
  build(1, 0, 2 * (n - 1));
  printf("%d\n", tree[1].lmr);
  for (int i = 0; i < q; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    if (a > b) swap(a, b);
    a--, b--;
    if (input[a] == '(')
      update(1, 0, 2 * (n - 1), a + 1, b, -2);
    else
      update(1, 0, 2 * (n - 1), a + 1, b, 2);
    swap(input[a], input[b]);
    tree[1].add();
    printf("%d\n", tree[1].lmr);
  }
}
