#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, z = 0, cnt = 0;
  cin >> n >> k;
  int x, y;
  while (n--) {
    cin >> x >> y;
    z += (y - x + 1);
  }
  while (true) {
    if (z % k == 0) {
      break;
    } else {
      cnt++;
      z++;
    }
  }
  cout << cnt << endl;
  return 0;
}
