#include <bits/stdc++.h>
using namespace std;
string input;
int n, q;
int k = 18;
int itA[(1 << 21)], it2L[(1 << 21)], itA2L[(1 << 21)], it2LA[(1 << 21)],
    itAll[(1 << 21)];
int add[(1 << 21)];
void addP(int v, int l, int r, int ll = 0, int rr = (1 << k), int ind = 1) {
  if (l <= ll && rr <= r) {
    add[ind] += v;
    return;
  }
  if (rr <= l || r <= ll) {
    return;
  }
  addP(v, l, r, ll, (ll + rr) / 2, ind * 2);
  addP(v, l, r, (ll + rr) / 2, rr, ind * 2 + 1);
}
int getC(int x) {
  x += (1 << k);
  int ret = 0;
  for (; x; x /= 2) {
    ret += add[x];
  }
  return ret;
}
void setB(int ind) {
  itA[ind + (1 << k)] = 1;
  it2L[ind + (1 << k)] = -2;
  if (input[ind] == ')') {
    itA[ind + (1 << k)] = -1;
    it2L[ind + (1 << k)] = 2;
  }
  itA2L[ind + (1 << k)] = -1 << 21;
  it2LA[ind + (1 << k)] = -1 << 21;
  itAll[ind + (1 << k)] = -1 << 21;
  int cx = ind + (1 << k);
  int d = 0;
  int sInd = ind;
  for (cx /= 2; cx; cx /= 2) {
    sInd &= ((1 << 21) - 1) - (1 << d);
    int nxtS = getC(sInd + (1 << d) - 1);
    if (sInd) {
      nxtS -= getC(sInd - 1);
    }
    d++;
    if (sInd < 10 && sInd + (1 << d) < 10) {
    }
    itA[cx] = max(itA[cx * 2], itA[cx * 2 + 1] + nxtS);
    it2L[cx] = max(it2L[cx * 2], it2L[cx * 2 + 1] - 2 * nxtS);
    itA2L[cx] = max(itA2L[cx * 2], itA2L[cx * 2 + 1] - nxtS);
    itA2L[cx] = max(itA2L[cx], itA[cx * 2] + it2L[cx * 2 + 1] - 2 * nxtS);
    it2LA[cx] = max(it2LA[cx * 2], it2LA[cx * 2 + 1] - nxtS);
    it2LA[cx] = max(it2LA[cx], it2L[cx * 2] + itA[cx * 2 + 1] + nxtS);
    itAll[cx] = max(itAll[cx * 2], itAll[cx * 2 + 1]);
    itAll[cx] = max(itAll[cx], itA[cx * 2] + it2LA[cx * 2 + 1] - nxtS);
    itAll[cx] = max(itAll[cx], itA2L[cx * 2] + itA[cx * 2 + 1] + nxtS);
    if (sInd < 10 && sInd + (1 << d) < 10) {
    }
  }
}
void rem(int i) {
  if (input[i] == '(') {
    addP(-1, i, 2 * n);
  } else {
    addP(1, i, 2 * n);
  }
}
void a(int i) {
  if (input[i] == '(') {
    addP(1, i, 2 * n);
  } else {
    addP(-1, i, 2 * n);
  }
}
int main() {
  cin >> n >> q;
  n--;
  cin >> input;
  for (int i = 0; i < 2 * n; i++) {
    a(i);
  }
  for (int i = 0; i < 2 * n; i++) {
    setB(i);
  }
  cout << itAll[1] << "\n";
  for (int i = 0; i < q; i++) {
    int i1, i2;
    cin >> i1 >> i2;
    i1--;
    i2--;
    rem(i1);
    rem(i2);
    swap(input[i1], input[i2]);
    a(i1);
    a(i2);
    setB(i1);
    setB(i2);
    cout << itAll[1] << "\n";
  }
  return 0;
}
