#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int tmp = 0;
  for (int i = 1; i <= n; i++) {
    int l, r;
    cin >> l >> r;
    tmp += (r - l + 1);
  }
  int p = k - (tmp % k);
  if (p == k) p = 0;
  cout << p;
}
