#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007LL;
long long large = 2000000000000000000LL;
vector<int> a;
vector<vector<int> > val;
vector<int> sum;
int up, uv;
void update(int o, int l, int r) {
  if (l == r) {
    sum[o] = uv;
    val[o][0] = uv;
    val[o][1] = -2 * uv;
    val[o][2] = -uv;
    val[o][3] = -uv;
    val[o][4] = 0;
  } else {
    int m = (l + r) / 2;
    if (up <= m) update(o * 2, l, m);
    if (up > m) update(o * 2 + 1, m + 1, r);
    sum[o] = sum[o * 2] + sum[o * 2 + 1];
    int L = sum[o * 2];
    val[o][0] = max(val[o * 2][0], L + val[o * 2 + 1][0]);
    val[o][1] = max(val[o * 2][1], -2 * L + val[o * 2 + 1][1]);
    val[o][2] = max(val[o * 2][2], -L + val[o * 2 + 1][2]);
    val[o][2] = max(val[o][2], val[o * 2][0] - 2 * L + val[o * 2 + 1][1]);
    val[o][3] = max(val[o * 2][3], -L + val[o * 2 + 1][3]);
    val[o][3] = max(val[o][3], val[o * 2][1] + L + val[o * 2 + 1][0]);
    val[o][4] = max(val[o * 2][4], val[o * 2 + 1][4]);
    val[o][4] = max(val[o][4], val[o * 2][0] - L + val[o * 2 + 1][3]);
    val[o][4] = max(val[o][4], val[o * 2][2] + L + val[o * 2 + 1][0]);
  }
}
int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  n = (int)s.length();
  a.assign(n, 0);
  for (int i = 0; i < n; i++) {
    if (s[i] == '(')
      a[i]++;
    else
      a[i]--;
  }
  val.assign(4 * n + 10, vector<int>(5, 0));
  sum.assign(4 * n + 10, 0);
  for (int i = 0; i < n; i++) {
    up = i;
    uv = a[i];
    update(1, 0, n - 1);
  }
  printf("%d\n", val[1][4]);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    r--;
    swap(a[l], a[r]);
    up = l;
    uv = a[l];
    update(1, 0, n - 1);
    up = r;
    uv = a[r];
    update(1, 0, n - 1);
    printf("%d\n", val[1][4]);
  }
  return 0;
}
