#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, -1, 0, 1, -1, 1, 1, -1};
const int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
const long double EPS = 1e-9;
const long double PI = 3.1415926535897932384L;
const long long int INF = 9223372036854775807;
const int MOD = 1000000000 + 7;
const int MAX = 10000000 + 7;
int main() {
  int N, K;
  cin >> N >> K;
  int seg = 0;
  for (int i = 0; i < N; i++) {
    int u, v;
    cin >> u >> v;
    seg += (v - u + 1);
  }
  return cout << (K - seg % K) % K << endl, 0;
}
