#include <bits/stdc++.h>
using namespace std;
const int N = 400000;
const int B = 500;
int n, q;
int f[N];
string s;
struct node {
  int dp[3][3];
};
node tr[N * 4];
int mark[N * 4];
node merge(const node &a, const node &b) {
  node c{};
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      c.dp[i][j] = max(a.dp[i][j], b.dp[i][j]);
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = i; j < 3; j++) {
      {
        for (int k = j + 1; k < 3; k++) {
          c.dp[i][k] = max(c.dp[i][k], a.dp[i][j] + b.dp[j + 1][k]);
        }
      }
    }
  }
  return c;
}
void build(int v, int l, int r) {
  for (auto &i : tr[v].dp) {
    for (int &j : i) {
      j = -N * 4;
    }
  }
  if (l == r) {
    tr[v].dp[0][0] = f[l];
    tr[v].dp[1][1] = -2 * f[l];
    tr[v].dp[2][2] = f[l];
    tr[v].dp[0][1] = -f[l];
    tr[v].dp[1][2] = -f[l];
    tr[v].dp[0][2] = 0;
    return;
  }
  int mid = (l + r) / 2;
  build(v * 2, l, mid);
  build(v * 2 + 1, mid + 1, r);
  tr[v] = merge(tr[v * 2], tr[v * 2 + 1]);
}
void push(int v) {
  for (int j = 0; j < 2; j++) {
    mark[v * 2 + j] += mark[v];
    tr[v * 2 + j].dp[0][0] += mark[v];
    tr[v * 2 + j].dp[2][2] += mark[v];
    tr[v * 2 + j].dp[1][1] += -2 * mark[v];
    tr[v * 2 + j].dp[0][1] += -mark[v];
    tr[v * 2 + j].dp[1][2] += -mark[v];
  }
  mark[v] = 0;
}
void md(int v, int l, int r, int ll, int rr, int add) {
  if (mark[v] != 0) {
    if (l < r) {
      push(v);
    }
    mark[v] = 0;
  }
  if (ll <= l && r <= rr) {
    mark[v] += add;
    tr[v].dp[0][0] += add;
    tr[v].dp[2][2] += add;
    tr[v].dp[1][1] += -2 * add;
    tr[v].dp[0][1] += -add;
    tr[v].dp[1][2] += -add;
    return;
  }
  int mid = (l + r) / 2;
  if (ll <= mid) {
    md(v * 2, l, mid, ll, rr, add);
  }
  if (rr > mid) {
    md(v * 2 + 1, mid + 1, r, ll, rr, add);
  }
  tr[v] = merge(tr[v * 2], tr[v * 2 + 1]);
}
void upd(int i, int c) {
  if (s[i - 1] == '(') {
    md(1, 1, n, i, n, c * 1);
  }
  if (s[i - 1] == ')') {
    md(1, 1, n, i, n, c * -1);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n >> q;
  cin >> s;
  n = s.size();
  for (int i = 0; i < s.size(); i++) {
    f[i + 1] = f[i];
    if (s[i] == '(') {
      f[i + 1]++;
    } else {
      f[i + 1]--;
    }
  }
  build(1, 1, n);
  cout << tr[1].dp[0][2] << "\n";
  for (; q >= 1; q--) {
    int x, y;
    cin >> x >> y;
    upd(x, -1);
    upd(y, -1);
    swap(s[x - 1], s[y - 1]);
    upd(x, 1);
    upd(y, 1);
    cout << tr[1].dp[0][2] << "\n";
  }
  return 0;
}
