#include <bits/stdc++.h>
using namespace std;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
void solve() {
  int n, k;
  cin >> n >> k;
  long long p = 0;
  for (int i = 0; i < n; i++) {
    int l, r;
    cin >> l >> r;
    p += r - l + 1;
  }
  if (p % k == 0) {
    cout << 0 << endl;
  } else {
    int val = (k - (p % k)) % k;
    cout << val << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
