#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, x = 0;
  cin >> n >> m;
  int a, b;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    x += b - a + 1;
  }
  int f = 0;
  while (x % m != 0) {
    f++;
    x++;
  }
  cout << f << endl;
  return 0;
}
