#include #include #pragma warning(disable : 4996) using namespace std; int N, M, x, d, p; int main() { scanf("%d %d", &N, &M); int al = N, ar = -1, bl = N, br = -1; for (int i = 0; i < M; i++) { scanf("%d %d", &x, &d); for (int j = 0; j < d; j++) { scanf("%d", &p); if (x > p) { al = min(al, p); ar = max(ar, x); } if(x < p) { bl = min(bl, x); br = max(br, p); } } } if (ar == -1 && br == -1) printf("0\n"); else if (ar == -1) printf("%d\n", br - bl); else if (br == -1) printf("%d\n", ar - al); else printf("%d\n", min(ar + br - 2 * min(al, bl), 2 * max(ar, br) - al - bl)); return 0; }