Step 1: Divide the number into separate digits:
3871 => 3 8 7 1
Step 2: Calculate the distance between each digit and the next (the last digit with the first):
3 to 8 = 5
8 to 7 = 1
7 to 1 = 6
1 to 3 = 2
Step 3: Combine the filters to a string:
5 1 6 2 = 5162